请问我把DataGrid中的数据导入到Excel为什么会乱码?
//将DataGrid1构成的html代码写进StringWriter
this.DataGrid1.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
this.DataGrid1.RenderControl(hw);
string HtmlInfo = tw.ToString().Trim();
string DocFileName = "aa.xls";
string FilePathName = Request.PhysicalPath;
FilePathName = FilePathName.Substring(0,FilePathName.LastIndexOf("\\"));
//得到Excel文件的物理地址
FilePathName = FilePathName +"\\" + DocFileName;
System.IO.File.Delete(FilePathName);
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
BinaryWriter BWriter= new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("gb2312"));
//将DataGrid的信息写入Excel文件
BWriter.Write(HtmlInfo);
BWriter.Close();
Fs.Close();
导入成功后打开Excel所有中文都显示为类似韩文的文字.GetEncoding后我什么都试过了,包括GB18030,UTF-7;UTF-8.其中UTF-8和上述一样,UTF-7全成了英文乱码.
其实用写字板,EDITPLUS这些打开导入后的EXCEL文件查看里面的中文都是正常的,就是用EXCEL进去不正常.请有经验的朋友指教如何解决,不甚感激.