gpt4 book ai didi

ASP.NET Excel导出编码问题

转载 作者:行者123 更新时间:2023-12-03 07:21:42 24 4
gpt4 key购买 nike

我正在 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();

请问出了什么问题?

最佳答案

我发现问题可能出在 Excel 文件的 header 中,它不包含 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();

关于ASP.NET Excel导出编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1679656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com