By tracing the ASP. NET Server code, there is no garbled, however, after exporting Excel to the browser, it appears garbled when opened.
The workaround is to add the prefix bytecode in the encoded format:
Response.BinaryWrite (System.Text.Encoding.Unicode.GetPreamble ());
?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Response.Clear(); Response.AddHeader(
"content-disposition"
,
"attachment;filename=Test.xls"
); Response.ContentType =
"application/ms-excel"
; Response.ContentEncoding = System.Text.Encoding.Unicode; Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
System.IO.StringWriter sw =
new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw =
new HtmlTextWriter(sw);
FormView1.RenderControl(hw);
Response.Write(sw.ToString()); Response.End();
|
The preamble of the
For example Utf-8 is 3 bytes: 239,187,191. These three bytes are located in the header of the file, indicating that our file is in utf-8 format.