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.
 

sje

Joined
Aug 17, 2006
Messages
1
Reaction score
0
Workaround for Excel

I had this prolbem too and found a workaround for it.

I put the encodeing into the html that is sent to excel

Look at string a1 almost at the end of this code example.

---

if (Request.Params.Get("excel") == "true")
{
//System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("is");
Culture = "is-IS";

Response.Clear();
Response.Charset = "";
//set the response mime type for excel
//Response.ContentType = "application/vnd.ms-excel";
Response.ContentType = "application/text";

Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "utf-8";


//Response.Charset = System.Text.Encoding.UTF8;
//Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("content-disposition", "attachment; filename=QueryResults.xls");

DataGrid dgQuery = new DataGrid();
dgQuery.DataSource = dsQuery;
dgQuery.DataBind();

//create a string writer
System.IO.StringWriter stringWrite = new StringWriter(System.Globalization.CultureInfo.CreateSpecificCulture("is-IS"));
//stringWrite.FormatProvider
// Ceate an htmltextwriter whic1 uses the stringwriter
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(stringWrite);

//htmlWriter.FormatProvider = System.Globalization.CultureInfo.GetCultureInfo("is").get

//TMWebGrid01.RenderControl(htmlWriter);
dgQuery.RenderControl(htmlWriter);

// Output table as HTML
string a1 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Datos</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<style>\n</style>\n</head>\n<body>\n";

string a2 = "\n</body>\n</html>";

Response.Write(a1+stringWrite.ToString()+a2);

Response.End();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top