导出HTML表格到excel文件编码
本文关键字:文件 编码 excel HTML 表格 导出 | 更新日期: 2023-09-27 18:16:43
我得到的任务是编写代码,以导出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文件中正确显示。
我能做些什么呢?
Thanks for help
将代码修改为:
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扩展警告从网站打开Excel工作簿,不幸的是,没有办法解决它。
From the post:
警报提示是"按设计",但交互取消操作和IE尝试再次打开文件是已知问题正在调查未来的修复。
我添加了:
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());
现在它工作正常,除了有一个警告,而打开excel文件,有一个错误,文件有不同的格式比扩展名。可以删掉吗?