从 ASP.NET 导出到 Excel 会生成 Excel 查看器无法读取的文件
我想将一些动态数据从 ASP.NET 网站输出到 Excel。我发现不需要使用 Excel XML 或在服务器计算机上安装 Excel 的最简单方法是 将数据输出为表格并指定application/vnd.ms-excel类型。
问题是,当我这样做并尝试在 Excel Viewer 中打开该文件时,我收到以下错误消息:
Microsoft Excel Viewer 无法打开 这种类型的文件。
然后什么都没有打开。
即使是最简单的代码,例如:
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Example.xls");
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
dataGrid.RenderControl(htmlTextWriter);
Response.Write("<html><body><table><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></table></body></html>");
Response.End();
}
也会产生相同的错误。
什么会导致这种行为?是不是因为Viewer无法读取此类文件,但完整的Excel版本可以?
I want to output some dynamic data from an ASP.NET website to Excel. I found that the easiest way which does not require to use Excel XML or to install Excel on server machine is to output data as a table and specify application/vnd.ms-excel type.
The problem is when I do so and try to open the file in Excel Viewer, I receive the following error message:
Microsoft Excel Viewer cannot open
files of this type.
Then nothing is opened.
Even an easiest code such as:
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Example.xls");
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
dataGrid.RenderControl(htmlTextWriter);
Response.Write("<html><body><table><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></table></body></html>");
Response.End();
}
produces the same error.
What can cause such behavior? Is it because the Viewer cannot read such files, but full Excel version can?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您永远不会想在服务器上安装 Excel。 COM 接口可以在没有 UI 的情况下工作,但它的设计初衷并非如此,如果出现某种错误,您的服务器上可能会出现大量挂起的 EXCEL.EXE 实例。它也显然不适用于 IIS 7.5 的应用程序池标识,也不适用于服务器核心。
理想的解决方案是使用可以构建二进制(或 OOXML)Excel 文件的第三方组件。 SyncFusion 和 Aspose 是此类工具的两个主要供应商。您最终将获得适用于所有版本的 Excel 以及其他软件(例如 OpenOffice、Google Docs、Outlook Web App 等)的文件。
You never, ever want to go down the route of installing Excel on the server. The COM interface can work without the UI, but it's not designed to do, and you can end up with a large number of hung EXCEL.EXE instances on your server if there is an error of some sort. It also plain doesn't work with IIS 7.5's application pool identites, nor does it work with Server Core.
The ideal solution is to use a third-party component that can build binary (or OOXML) Excel files. SyncFusion and Aspose are two major vendors of such tools. You'll end up with files that will work all versions of Excel, as well as in other software such as OpenOffice, Google Docs, Outlook Web App, and so on.
Excel阅读器只能打开Excel文件。您实际上并不是将其导出为 Excel 文件,而是将其导出为 csv 或 html。这不一样。
编辑
您尝试过导出到 Excel 吗?
Excel reader can only open excel files. You really aren't exporting it as an excel file, you are exporting it as a csv or html. It's not the same.
EDIT
Have you tried this Export to Excel?