Response.AddHeader corrupts Japanese/chinese characters when writing outputstrea

  • Thread starter Thread starter Joseph
  • Start date Start date
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!
 
the corrupted Japanese characters appear like this:
ãf¯ãf³ã,¢ã,½ã,·ã,¨ã,¤ãf"(æo?)
 
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.
 
Back
Top