在 ASP.NET 中将数据集导出到具有多个工作表的 Excel 文件

发布于 2024-08-27 16:50:11 字数 500 浏览 4 评论 0原文

在 C# ASP.NET 3.5 Web 应用程序中,我需要将多个数据表(或数据集)导出到具有多个工作表的 Excel 2007 文件,然后为用户提供“打开/保存”对话框,而不将 Excel 文件保存在网络服务器。

我以前使用过 Excel Interop。我一直在读到它效率不高,也不是实现这一目标的最佳方法,还有更多方法可以做到这一点,其中 2 种是:1)将数据表中的数据转换为 Excel 可以理解的 XML 字符串 2)使用 OPEN XML SDK 2.0。

看起来 OPEN XML SDK 2.0 更好,请告诉我。还有其他方法吗?我不想使用任何第三方工具。

如果我使用 OPEN XML SDK,它会创建一个 excel 文件,对吧?我不想将其保存在(Windows 2003)服务器硬盘上(我不想使用 Server.MapPath,这些 Excel 文件是动态创建的,一旦客户端获取它们,服务器上就不需要它们) 。我直接想提示用户打开/保存它。我知道使用“XML 字符串”方法时该怎么做。

请帮忙。 谢谢。

In C# ASP.NET 3.5 web application, I need to export multiple datatables (or a dataset) to an Excel 2007 file with multiple sheets, and then provide the user with 'Open/Save' dialog box, WITHOUT saving the Excel file on the web server.

I have used Excel Interop before. I have been reading that it's not efficient and is not the best approach to achieve this and there are more ways to do it, 2 of them being: 1) Converting data in datatables to an XML string that Excel understands 2) Using OPEN XML SDK 2.0.

It looks like OPEN XML SDK 2.0 is better, please let me know. Are there any other ways to do it? I don't want to use any third-party tools.

If I use OPEN XML SDK, it creates an excel file, right? I don't want to save it on the (Windows 2003) server hard drive (I don't want to use Server.MapPath, these Excel files are dynamically created, and they are not required on the server, once client gets them). I directly want to prompt the user to open/save it. I know how to do it when the 'XML string' approach is used.

Please help.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

黑寡妇 2024-09-03 16:50:11

是否绝对需要 Excel 2007 支持?

我们使用 NPOI 取得了巨大成功,它支持我们想要的所有功能(多个工作表、格式、公式)。它也相当稳定。

但它生成的文件是 Excel 2003 格式,因此它们是二进制的,而不是 OOXML。

这个问题之前已经被问过,请参阅此处以获取更好的信息讨论。

Is Excel 2007 support absolutely required?

We have used NPOI with great success, and it supports all the features we want (multiple worksheets, formatting, formulas). It is also pretty stable.

The files it produces are in Excel 2003 format though, so they are binary, not OOXML.

This question has been asked before, see here for a better discussion.

预谋 2024-09-03 16:50:11

您可以轻松地将 xml 响应作为 XML Excel 文件传输给用户。

任何页面:

<a href="report.aspx" target="_blank"> Open excel Report</a>

Report.aspx:

 Response.Clear();
 Response.ContentType = "application/vnd.ms-excel";
 Response.AddHeader("Content-Disposition"
                     , "attachment;filename=" & _fileName & ".xml");
 Response.Write("<?xml version=""1.0""?>");
 Response.Write(excelXML);

You can easily stream the xml response to the user as an XML Excel file.

Any Page:

<a href="report.aspx" target="_blank"> Open excel Report</a>

Report.aspx:

 Response.Clear();
 Response.ContentType = "application/vnd.ms-excel";
 Response.AddHeader("Content-Disposition"
                     , "attachment;filename=" & _fileName & ".xml");
 Response.Write("<?xml version=""1.0""?>");
 Response.Write(excelXML);
冬天的雪花 2024-09-03 16:50:11

我也遇到过类似的将数据集导出到 Excel 的要求。我使用了这个开源库。它基于 Open XML 标准,不使用 Office Interop。它满足了我的要求。但是,我的要求是基本的。因此,请检查它是否满足您的要求。

I too had come across similar requirement to export dataset into excel. I used this open source library. It's based on Open XML standards and doesn't use Office Interop. It met my requirement. But, my requirement was basic. So, check if it fulfills your requirement.

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