2014年7月13日 星期日

[RESOLVED] Error when exporting data to excel with special character - asp.net(vb)


Hi, 


I want to export data from SQL using StoredProcedure and write into excel.

Everything is working well except the unicode. Some of my data is include unicode but the unicode seems to be export as a string to excel.  


Example:












Title
Name 名字

Under  


Response.AddHeader("Content-Disposition",
"attachment;filename=ABC.xls")

Response.ContentType =
"application/vnd.ms-excel"


I tried to insert


Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")

Response.ContentEncoding = System.Text.Encoding.Default

Response.ContentEncoding = System.Text.Encoding.
UTF8


but none of it work ..


Hope someone can help me.. Thanks in advance!






You also have to write the "preamble" to tell the client operating system it's a UTF8 file.


Here's the code:


Response.AddHeader("Content-Disposition", "attachment;filename=ABC.xls")
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite( System.Text.Encoding.Unicode.GetPreamble() );
// write rest of content here

Find out more about the GetPreamble() method here: http://msdn.microsoft.com/en-us/library/system.text.encoding.getpreamble.aspx





沒有留言:

張貼留言