ASP.net MVC 导出到 Excel
我目前正在使用旧的 HTML 技巧导出到 Excel,其中我将 MIME 类型设置为 application/ms-excel。这带来了表格格式良好的额外好处,但缺点是 Excel 文档不是本机 Excel 格式。
我可以将其导出为 CSV,但这样就不会被格式化。
我读过一些简短的片段,您可以将其导出为 XML 来创建 Excel 文档,但找不到太多相关信息。有人知道任何教程和/或这样做的好处吗?可以用这个方法格式化表格吗?
谢谢。
I am currently exporting to Excel using the old HTML trick, where I set the MIME type to application/ms-excel. This gives the added benefit of nicely formatted tables, however the negative of the excel document not being native Excel format.
I could export it as CSV, but then this would not be formatted.
I have read brief snippets that you can export it as XML to create the Excel document, but cannot find too much information on this. Does anybody know of any tutorials and/or benefits of this? Can it be formatted tables using this method?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看 ExcelXmlWriter。
我们已经使用它有一段时间了,效果很好。然而 xml 格式也有一些缺点。由于您的最终用户不太可能具有与 Excel 关联的 .xml 扩展名,因此您最终必须将文件下载为具有 Excel mime 类型的 .xls。当用户打开以这种方式下载的文件时,他们会收到一条警告,指出该文件不是 xls 格式。如果他们单击它,文件将正常打开。
唯一的选择是使用付费库来生成本机 Excel 文件。这当然是最好的解决方案,但上次我们查看时没有好的免费库(可能已经改变)
Check out ExcelXmlWriter.
We've been using it for some time and it works well. There are some downsides to the xml format however. Since it's unlikely your end users will have the .xml extension associated with Excel, you end up having to download files as .xls with an Excel mime type. When a user opens a file downloaded in this way they get a warning that the file is not in xls format. If they click through it, the file opens normally.
The only alternative is a paid library to generate native Excel files. That's certainly the best solution but last time we looked there were no good, free libraries (may have changed)
Bill Sternberger 在此处发布了一个非常简单的解决方案:
从 asp.net mvc 导出到 excel 或 csv
Bill Sternberger has blogged a very simple solution here:
export to excel or csv from asp.net mvc
就在今天,我必须编写一个例程,将数据导出到 MVC 应用程序中的 Excel。以下是详细信息,以便将来有人可能受益,首先用户必须为报告选择一些日期范围和区域。在回发时,此方法已到位,TheModelTypeList 包含来自 LINQ/Entity Framework/SQL 查询的数据返回强类型:
此方法的唯一问题是打开时文件类型未知,因此系统提示输入应用程序打开它...这是内容是 XML 的结果...我仍在努力解决这个问题。
Just today I had to write a routine that exported data to excel in an MVC application. Here's the details so someone may benefit in the future, first the user had to select some date ranges and areas for the report. On the post back, this method was in place, with TheModelTypeList containing the data from LINQ/Entity Framework/SQL Query returning strong types:
The only catch on this one was the file type was not known when opening so the system prompted for the application to open it... this is a result of the content being XML.... I'm still working on that.
我正在使用 Spreadsheet Light,这是一个开源库,它提供了从 C# 创建、操作和保存 Excel 工作表的极其简单的功能。您可以让 MVC / WebAPI 控制器完成创建文件的工作,然后
http://spreadsheetlight.com/
I am using Spreadsheet Light, an Open-Source library that provides ridiculously easy creation, manipulation and saving of an Excel sheet from C#. You can have an MVC / WebAPI Controller do the work of creating the file and either
http://spreadsheetlight.com/
最简单的方法是,您可以解析表并将其导出为 Excel XML 格式,例如:http://blogs.msdn.com/b/brian_jones/archive/2005/06/27/433152.aspx
它允许您根据需要设置表格格式(边框、字体、颜色,我认为甚至是公式),Excel 会将其识别为原生 Excel 格式。另外,您可以使用其他可以导入 Excel XML 的程序(即 Open Office、Excel 查看器等),并且不需要在服务器上安装 Office 组件。
Easiest way, you could parse your table and export it in Excel XML format, see this for example: http://blogs.msdn.com/b/brian_jones/archive/2005/06/27/433152.aspx
It allows you to format the table as you whish (borders, fonts,colors, I think even formulas), and Excel will recognize it as native excel format. As a plus, you can use other programs that can import Excel XML (ie.Open office, Excel viewer,etc) and you do not need to have Office components installed on the server.