Response.AddHeader corrupts Japanese/chinese characters when writing outputstrea

J

Joseph

Hello. I have this problem. See I have a transformed XML
file and I checked its contents prior to outputting it to
excel file via responseset. here is the gist of the code:

XmlReader reader = myEsiCommand.ExecuteXmlReader();
reader.MoveToContent();
string myCSV = reader.ReadInnerXml();

//Load the xml fragment into the document
XmlDocument xmlDataDoc = new XmlDocument();
xmlDataDoc.LoadXml(myCSV);

//Load the stylesheet to perform the transformation
string path = ConfigurationSettings.AppSettings.Get
("TransformationsPath");
string xslPath = Server.MapPath(path + "/APExport.xsl");
XslTransform transform = new XslTransform();
transform.Load(xslPath);

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

transform.Transform(xmlDataDoc,null,sw);

Response.ContentType="application/x-msexcel";
Response.Charset = "utf-8";
Response.AddHeader("content-disposition","attachment;
filename=download.csv");
Response.Clear();
Response.Write(sb.ToString());
Response.End();

This code is executed when a user clicks a link to
download a comma separated value (csv) file.

The result of this code is that the string from
StringBuilder "sb" is outputted to an excel file. The
string contains japanese characters and these are then
corrupted/converted into i assume ascii characters.
I tried commenting out Response.AddHeader and tried
writing the output to an ordinary html. the japanese
characters were intact. i also tried inspecting the
stringbuilder data (where the transformed xml was written
to) and the japanese characters were also intact. its only
when i do the Response.AddHeader command that the chars
get jumbled. I do not know of any other way to output this
data as an attachment to an excel file. does anybody have
any ideas? Much thanks in advance!
 
J

Joseph

the corrupted Japanese characters appear like this:
ãf¯ãf³ã,¢ã,½ã,·ã,¨ã,¤ãf"(æo?)
 
N

Nick

I think you problem is you are using utf-8

Response.Charset = "utf-8";

Try using one of the other ones maybe BigEdian ( I don't remember how it is
spelled ). But that one may work for you because it is used for asian
characters if I remember right.
 

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

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top