ASP.NET Excel导出编码问题
发布时间:2020-05-22 11:54:47 所属栏目:asp.Net 来源:互联网
导读:我在ASP.NET网站上做了一些Excel导出。 一切工作除了编码。当我在Excel中打开它,它看起来像这样: Eingabe Kosten je Ger¤t Ger¤t: Ger¤tebezeichnung: Betriebsmittel Heizl in : 4 Dieselverbrauch in : 4 这是我的代码: Response.Clear();Respon
|
我在ASP.NET网站上做了一些Excel导出。
这是我的代码: 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();
我已经尝试明确设置Encoding ..但没有发生变化: 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字节序列(在文件的开头表示使用的编码)。所以我这样做,它适用于我: 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(); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- azure – 获取#error = unsupported_response_typeerror_de
- asp.net – 如何在WebGrid中的列标题使用DisplayName数据注
- asp.net-mvc – MVC视图中的多个表单:ModelState应用于所有
- asp.net-mvc-2 – 在自定义模型绑定器中设置ModelState值
- 单元测试 – 自动测试经典ASP
- asp.net-web-api – 当启用CORS时,ASP.NET Web API中的异常
- 使用ASP.NET MVC3中的jQuery,可点击日期的事件日历
- asp.net – web.config转换未在VS2010中显示
- asp.net-mvc – 为什么在Asp.net MVC 2中的子操作中不允许重
- asp.net-mvc – 使用ASP.NET MVC响应REQUEST_METHOD = HEAD
