Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Am Saving the file in Excel format which contains Chinese letters using C#.NET.

The excel file saved successfully. But the Chinese words are messed up in excel file.

Code Used to Save is :-

C#
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" Test.xls");
Response.Charset = "UTF-8"; 
     //Response.ContentEncoding = System.Text.Encoding.Unicode;
     //Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw); 
comments.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();


It shows the exact output in website (脚宽 ). But after saving the file the excel file shows like this (脚宽) .

Ex: 脚宽 ----> 脚宽

Help me to solve this issue.

Thank you.
Posted
Updated 25-Aug-15 1:45am
v2

What are doing is not actually generating an XLS document. You are really creating an HTML file with a .xls extension. Excel is able to open these, but you do not have the functionality of a native Excel document.

You should look at using OpenXML, https://msdn.microsoft.com/en-us/library/office/bb448854.aspx[^]
 
Share this answer
 
I Found the solution for this. Now it works fine.

C#
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "utf-8";
Response.AddHeader("content-disposition", "attachment; filename=FileName.xls");
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.ContentType = "application/ms-excel";
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
comments.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();


Thank you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900