ASP.NET Excel导出编码问题
我正在ASP.NET网站上进行Excel导出。 除了编码之外,一切都正常。当我在Excel中打开它时,它如下所示: Eingabe Kosten je GerÃt GerÃt: Ger÷Tebezeichung: Betriebsmittel HeizÃl in–4 柴油机:4 这是我的代码:ASP.NET Excel导出编码问题,asp.net,encoding,character-encoding,Asp.net,Encoding,Character Encoding,我正在ASP.NET网站上进行Excel导出。 除了编码之外,一切都正常。当我在Excel中打开它时,它如下所示: Eingabe Kosten je GerÃt GerÃt: Ger÷Tebezeichung: Betriebsmittel HeizÃl in–4 柴油机:4 这是我的代码: Response.Clear(); Response.ContentType = "application/ms-excel"; Response.AddHeader("Content-Disposit
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();
我已经尝试显式设置编码。。但没有发生变化:
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();
有什么问题吗?您是否尝试过在HTML的元标记中设置编码
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
Excel看不到响应标题,因此它不知道
响应.Encoding
是什么。meta标记允许它查找。我发现问题可能出在excel文件的头中,它不包含BOM字节序列(在文件开头表示所用编码)
所以我这样做了,它对我来说很有用:
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();
加
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble())代码>用于需要UTF8的实例
FileInfo dataExportFile = new FileInfo(dsExport.Tables[0].Rows[0]["DataExportFile"].ToString());
Response.Clear();
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + dataExportFile.Name);
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
Response.TransmitFile(dataExportFile.FullName);
我用西班牙语字符遇到了同样的问题,并用这行代码解决了这个问题
response.ContentEncoding = System.Text.Encoding.Default ;
希望这有帮助您可以尝试使用“Server.HtmlDecode”来解码这些单词,如“João”
我正在导出一个呈现的控件,这意味着导出的文件是仅包含一个表的html。没有主体,就没有标题。没有..添加至少。。。是的,但我认为这和添加带编码的字节头是一样的解决方案。。无论如何,这是一个在办公室里打开的文件。。你怎么看?作为一个极简主义者,我尝试删除第4行(ContentEncoding
)和第5行(BinaryWrite
),但惊讶地发现它不起作用。在找到这个解决方案方面做得很好。尊敬!:)我花了几个小时试图让它工作,最后放弃了,并在我的字符串前面连接了“”。一个彻底的黑客。非常感谢你弄明白这一点。我最后的评论听起来不太对。我试图自己找出一个解决方案,但在找到你的答案之前,我无法将其修复。:-)你救了我一天!!您应该添加更多的代码,例如在何处添加一行代码。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e){
String wrong = "João";
String corrected = Server.HtmlDecode(wrong);}