问题描述
我做的ASP.NET站点的某些Excel的出口。
一切正常,除了编码。当我在Excel中打开它,它看起来是这样的:
I'm doing some Excel Exports on the ASP.NET Site.Everything works except of the Encoding. When I open it in Excel, it looks like this:
Eingabe Kosten JEGerätGerät:
Gerätebezeichnung:
BetriebsmittelHeizöl在â,¬:4
Dieselverbrauch在â,¬:4
这是我的code:
Response.Clear();
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Disposition", "inline;filename=NachkalkGeraete.xls;");
var writer = new HtmlTextWriter(Response.Output);
SomeControl.RenderControl(writer); /* FormView, Table, DataGrid... */
Response.End();
我已经尝试过明确设置编码..但没有发生变化:
I've already tried explicitly set the Encoding.. but no change occured:
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=NachkalkGeraete.xls");
Response.BufferOutput = true;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "UTF-8";
EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
SomeControl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
什么是错的吗?
推荐答案
好吧,我发现,问题可能出在Excel文件的标题,它不包含在 BOM字节序列 (在文件重新presenting使用的编码的开始时)。
Well I found out that the problem could be in the header of the excel file, that it does not contain the BOM byte sequence (at the beginning of the file representing the encoding used).
所以我做了这种方式,它为我的作品:
So I made it this way and it works for me:
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();
这篇关于ASP.NET Excel导出编码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!