如何使用 C# 和 MVC 2 从 SQL 数据库生成 PDF/Excel 文件?
我需要允许用户将数据库中的信息下载为 PDF 或 Excel 电子表格(两者都有效),从而使数据库中的信息可用。
我已经查看了一堆选项,但我真的无法决定应该使用哪一个,更不用说这些选项中的任何一个实际上有用了。我发现的大多数选项都是围绕将现有的 HTML 文件转换为 PDF 进行的,这不是我所需要的。此外,它必须是免费的。我的老板没有给我预算用于此方面,
我不确定我还应该在此处包含哪些其他信息。
嗯,非常感谢任何帮助。如果您对缺失信息有疑问,我会尽快发布。我整天都在这里,所以我能够很快回复任何评论。
编辑:哦哇!非常感谢大家的热烈响应!我有很多想法。这非常有帮助。谢谢!
I need to make the information in the database usable by allowing the user to download it as a PDF or Excel spreadsheet (either one works, both is perfect).
I've looked around at a bunch of options, but I really can't decide which one I should use, let alone if any of those options are actually useful. Most of the options I've found revolve around converting already existing HTML files into PDFs which is not what I need. Also, it needs to be free. My bosses haven't given me a budget to spend on this
I'm not sure what other information I should include here.
Well, any help is greatly appreciated. If you have questions about missing information, I'll get it posted ASAP. I'm here all day, so I'll be able to respond to any comments very quickly.
EDIT: Oh wow! Huge thanks, guys, for the massive response! I got a ton of ideas. This is super-helpful. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想生成 Excel(或 Word),您可以使用 openXml。您可以完全按照您想要的方式从纯代码创建新文档。
OpenXml SDK 页面
if you want to generate an Excel (or also a Word) you can use openXml. You can create a new document exactly the way you want from pure code.
OpenXml SDK page
在这种情况下,我通常向客户建议的解决方案是使用 Sql Server Reporting Services (SSRS)。您可以使用其中包含的 ReportViewer 控件来生成PDF、Excel 电子表格、XML 文件、CSV 文件等。如果您需要临时报告,可以使用报告生成器< /a> 也可用。
除此之外,您可以使用 OpenXml 生成 Excel 电子表格,并且有一个大量可用的 PDF 工具包。
The solution I usually propose to my clients in this situation is to use Sql Server Reporting Services (SSRS). You can use the ReportViewer control included with it in order to generate PDF's, Excel spreadsheets, XML files, CSV files, and others. If you need ad hoc reporting, there is a Report Builder available as well.
Barring that, you can use OpenXml to generate Excel spreadsheets and there are a host of PDF toolkits available.
您是否研究过 ReportViewer 控件,它是 Visual Studio 的一部分?
它允许您以 PDF 或 Excel 格式导出报告。
Have you looked into the reportviewer control, which is part of Visual Studio?
It allows you to export the report in PDF or Excel format.
http://www.carlosag.net/tools/excelxmlwriter/sample
检查这可能是对你有用
http://www.carlosag.net/tools/excelxmlwriter/sample
check this might be useful for you
有很多报告解决方案,例如 SQL Server Reporting Services(您可能已经拥有其许可证)。查看报告(免费 || 开源) Winforms 中 Crystal Reports 的替代品,可以通过一些序列化应用于 Web。
我建议根据情况考虑自己推出。您可以使用 pdfsharp 进行 pdf 导出,并 EPPlus 用于 Excel。它们都非常易于使用,而且我很确定,只需单击几下即可在 nuget 中使用它们。
There are lots of reporting solutions out there such as SQL Server Reporting Services(for which you might already have a license). Take a look at Reporting (free || open source) Alternatives to Crystal Reports in Winforms which can likely be applied to the web with a bit of serialization.
I would suggest thinking about rolling your own depending on the situation. You could use pdfsharp for the pdf export and EPPlus for excel. They are both very easy to use and, I'm pretty sure, available in nuget with a couple of clicks.
如果您想走 Excel 路线,我推荐 Stephen Walther 的这篇文章,标题为 ASP.NET MVC 技巧 #2 - 创建返回 Microsoft Excel 的自定义操作结果文档。这使用了使用 Excel mime 类型编写 HTML 文档的老技巧。这与流式传输本机 Excel 文件不同。如果您想将其剥离并使其成为更通用的文件,则可以很容易地更改为呈现 CSV 文件。如果可能出现逗号,请记住对所有字段加双引号。
If you want to go the Excel route, i'd recommend this article from Stephen Walther entitled ASP.NET MVC Tip #2 - Create a custom Action Result that returns Microsoft Excel Documents. This uses an old trick of writing an HTML document with an Excel mime type. This is different than streaming a native Excel file. And it's fairly easy to change the to rendering a CSV file if you want to strip it down, and make it a more universal file. Just remember to double-quote all the fields if there's a possibility of commas showing up.
如果您所做的事情不太复杂,您可以使用 CSV 文件。 CSV 代表逗号分隔值,顾名思义。您可以使用逗号创建简单的表和列。例如,将以下行粘贴到文本文件中:
将文本文件另存为 .csv 文件,瞧,这是一个 Excel 电子表格。显然,构建这些循环对象集合非常容易。请注意,如果您需要任何复杂的文本格式等,那么它并不是真正的最佳选择。
If what your doing isn't too complicated you can use CSV files. CSV stands for comma separated values, and it is what it sounds like. You can create simple tables and columns using commas. For example paste the following lines into a text file:
Save the text file as a .csv file and voila - an excel spreadsheet. Obviously it is extremely easy to build these looping object collections. Mind you if you need any complicated text formatting etc then it is not really the best option.