encoding problems (utf-8)

  • Thread starter Guillermo Rosich Capablanca
  • Start date
G

Guillermo Rosich Capablanca

I have a problem with utf-8 enconding and I don't know
what to do in order to make it work.

I want to open a new window with excel data so the user
can choose to save it local. Everything works fine, the
new window, the excel data appears, but, extended
characters are wrong.

I call the window from an aspx page using this:

<script language='javascript'> window.open
('excelviewer.aspx?nif=...&id=.....

then, excelviewer.aspx.cs has this:

private void showExcel (DataSet ds) {
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-
Disposition", "inline;filename=consulta.csv");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "utf-8";
//Response.AppendHeader("Content-Transfer-Encoding",
Response.ContentEncoding.EncodingName);
System.Globalization.CultureInfo culture = new
System.Globalization.CultureInfo("es");

System.IO.StringWriter sw = new System.IO.StringWriter
(culture);
HtmlTextWriter htmlw = new HtmlTextWriter(sw);
htmlw.AddAttribute
(HtmlTextWriterAttribute.Title, "Consulta");

DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htmlw);

Response.Write(htmlw.InnerWriter);
Response.Flush();
Response.Close();
}

That is the C# code, it works fine, the new window
appears with the excel content and the correct columns
and rows, but several extended characters appears badly.
I saw on IE view and there is no AutoSelect and UTF-8 is
selected instead.

Also on the aspx I put this <%@ Page language="c#"
ResponseEncoding="UTF-8" Codebehind="excelviewer.aspx.cs"
AutoEventWireup="false"
Inherits="ROCAMP.Formularis.excelviewer" %> but nothing.

Here you can find some explample of the output, it is
just a part of the columns, and only the first line:

NIF NOM ESTAT CARNET NÃsMERO MOVIMENT BORSA DATA
INICI
38112294Y David Bernal Manero Finalitzat BÃ?SIC
BA100000/03 Expedició No 21/01/2004

it should look like this:

NIF NOM ESTAT CARNET NÚMERO MOVIMENT BORSA DATA
INICI
38112294Y David Bernal Manero Finalitzat BÀSIC
BA100000/03 Expedició No 21/01/2004


If anyone could give me some advice, I'll appreciate it
very much.

thanx in advance,

Guillermo.
 
G

Guest

Yes, utf-8 must work fine for me, I used previously in
xsl trasformations and it was fine (without utf-8 the xsl
transformation was unsuccesful), that's why I tried to
put it everywher, because I thought in somewhere the data
changed. I found that the problem is in the writer, I
mean, the data is ok just before writing, I check it
using watches at debugging.

I also tried another way later. Create a phisical csv
file (previously done on another application with utf-8
too, and works fine), and the same happened: in
StreamWriter.WriteLine(data), data is perfectly fine (as
appears in a debuger watch) but after the WriteLine,
opening the csv file shows some characters badly.

So, I don't think it is because of using utf-8. Anyway
I'll try with others codes. Thanx.
-----Original Message-----
Hi
utf-8 is selected because you say so all over your code!
(well, even if you didn't it's default anyway.)
change the page *and* request/response encoding from utf-
8 to your *local language encoding*.
modify the "globalization" section of the web.config
file appropriately to have it fixed once and for all.
dimitris

Guillermo Rosich Capablanca said:
I have a problem with utf-8 enconding and I don't know
what to do in order to make it work.

I want to open a new window with excel data so the user
can choose to save it local. Everything works fine, the
new window, the excel data appears, but, extended
characters are wrong.

I call the window from an aspx page using this:

<script language='javascript'> window.open
('excelviewer.aspx?nif=...&id=.....

then, excelviewer.aspx.cs has this:

private void showExcel (DataSet ds) {
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-
Disposition", "inline;filename=consulta.csv");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "utf-8";
//Response.AppendHeader("Content-Transfer- Encoding",
Response.ContentEncoding.EncodingName);
System.Globalization.CultureInfo culture = new
System.Globalization.CultureInfo("es");

System.IO.StringWriter sw = new System.IO.StringWriter
(culture);
HtmlTextWriter htmlw = new HtmlTextWriter(sw);
htmlw.AddAttribute
(HtmlTextWriterAttribute.Title, "Consulta");

DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htmlw);

Response.Write(htmlw.InnerWriter);
Response.Flush();
Response.Close();
}

That is the C# code, it works fine, the new window
appears with the excel content and the correct columns
and rows, but several extended characters appears badly.
I saw on IE view and there is no AutoSelect and UTF-8 is
selected instead.

Also on the aspx I put this <%@ Page language="c#"
ResponseEncoding="UTF-8" Codebehind="excelviewer.aspx.cs"
AutoEventWireup="false"
Inherits="ROCAMP.Formularis.excelviewer" %> but nothing.

Here you can find some explample of the output, it is
just a part of the columns, and only the first line:

NIF NOM ESTAT CARNET NÃfsMERO MOVIMENT BORSA DATA
INICI
38112294Y David Bernal Manero Finalitzat BÃf?SIC
BA100000/03 ExpediciÃf³ No 21/01/2004

it should look like this:

NIF NOM ESTAT CARNET NÃsMERO MOVIMENT BORSA DATA
INICI
38112294Y David Bernal Manero Finalitzat BÃ?SIC
BA100000/03 Expedició No 21/01/2004


If anyone could give me some advice, I'll appreciate it
very much.

thanx in advance,

Guillermo.
.
 
J

Joerg Jooss

Guillermo said:
I have a problem with utf-8 enconding and I don't know
what to do in order to make it work.

I want to open a new window with excel data so the user
can choose to save it local. Everything works fine, the
new window, the excel data appears, but, extended
characters are wrong.

A quick test shows that Excel (XP) seems to be the problem. Excel blindly
opens any CSV file using the OS default codepage.
 
Top