从 ASP.NET 导出到 Excel 会生成 Excel 查看器无法读取的文件

发布于 2024-09-03 19:48:53 字数 1211 浏览 8 评论 0原文

我想将一些动态数据从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脱离于你 2024-09-10 19:48:53

您永远不会想在服务器上安装 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.

梦晓ヶ微光ヅ倾城 2024-09-10 19:48:53

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文