ASP.NET Excel导出编码问题

42

我正在ASP.NET网站上进行一些Excel导出操作。一切都正常,除了编码。当我在Excel中打开它时,它看起来像这样:

Eingabe Kosten je Gerät Gerät: Gerätebezeichnung: Betriebsmittel Heizöl in €: 4 Dieselverbrauch in €: 4

这是我的代码:

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();

有什么问题需要解决吗?

6个回答

134

我发现问题可能在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(); 

作为一个极简主义者,我尝试消除第4行(ContentEncoding)和第5行(BinaryWrite),但惊讶地发现它不起作用。在找到这个解决方案方面做得非常好。尊重! :) - Chiramisu
我花了几个小时试图让它工作,最终放弃了,在我的字符串前面连接了一个空格。完全是一种欺骗行为。非常感谢你找出了这个问题。 - user441058
我的上一个留言听起来不太对。我一直在试图自己找出解决方案,但是在看到你的答案之前一直没能解决。 :-) - user441058

12

你尝试在HTML的meta标签中设置编码了吗?

<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />

Excel 无法查看响应头信息,因此它不知道 Response.Encoding 是什么。而 meta 标签可以帮助 Excel 找到这个信息。


我正在导出一个已渲染的控件,这意味着导出的文件只是一个带有表格的HTML文件。没有正文,没有标题,什么都没有... - theSpyCry
至少添加 <html><head><meta... /></head><body>...</body> 是值得的。 - kͩeͣmͮpͥ ͩ
是的,但我认为这与添加一个带有编码的字节头相同的解决方案一样。无论如何,这是一个要在办公室中打开的文件...你觉得呢? - theSpyCry
你救了我的工作!谢谢,这个与WebApi完美地配合(Asp.net WebApi返回一个IHttpAction结果)。非常感谢!!!!! - luke cross

11

对于需要使用 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);

4

我遇到了与西班牙语字符相同的问题,并通过以下代码解决了它。

        response.ContentEncoding = System.Text.Encoding.Default ;

希望这可以帮助你。

2

add Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());


你应该添加一些不仅是代码的内容,例如在哪里添加你的一行代码。 - EdChum

1
您可以尝试使用“Server.HtmlDecode”来解码这些像“João”这样的单词。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e){
String wrong = "Jo&#227;o";
String corrected = Server.HtmlDecode(wrong);}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接