gpt4 book ai didi

c# - 将html表格导出到excel文件编码

转载 作者:太空狗 更新时间:2023-10-30 00:56:10 25 4
gpt4 key购买 nike

我的任务是编写代码以将带有标题的 html 表导出到 excel。

我想到的是这样的服务器端代码:

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=myexcel.xls");
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
tbPanel.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();

它工作得很好,但有一些 utf-8 字符(俄语)在 excel 文件中显示不正确。

我对此有什么想法吗?

感谢帮助

最佳答案

将您的代码更改为以下内容:

Response.Clear();
Response.AddHeader("content-disposition","attachment;filename=myexcel.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();

编辑

当我发布我的答案时,我看到您已经找到了相同的解决方案。您看到的错误消息在此处解释:Excel 2007 Extension Warning On Opening Excel Workbook from a Web Site不幸的是,没有办法解决它。

来自帖子:

The alert prompt is "by design", but the interaction of the cancel action and IE's attempt to open the file again is a known problem under investigation for a future fix.

关于c# - 将html表格导出到excel文件编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8378815/

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