如何使用C#和MVC 2从SQL数据库生成PDF / Excel文件?

时间:2021-03-05 09:58:58

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).

我需要通过允许用户将其作为PDF或Excel电子表格下载来使数据库中的信息可用(两者都有效,两者都很完美)。

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

我看了很多选项,但我真的无法决定应该使用哪一个,更不用说这些选项中的任何一个实际上是有用的了。我发现的大多数选项都是围绕将现有的HTML文件转换为PDF而不是我需要的。此外,它需要是免费的。我的老板没有给我预算花在这上面

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!

编辑:哦哇!非常感谢,伙计们,大家的回应!我有很多想法。这非常有帮助。谢谢!

7 个解决方案

#1


1  

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.

在这种情况下,我通常向客户提出的解决方案是使用Sql Server Reporting Services(SSRS)。您可以使用其中包含的ReportViewer控件来生成PDF,Excel电子表格,XML文件,CSV文件等。如果您需要临时报告,也可以使用报告生成器。

Barring that, you can use OpenXml to generate Excel spreadsheets and there are a host of PDF toolkits available.

除此之外,您可以使用OpenXml生成Excel电子表格,并且有许多PDF工具包可用。

#2


5  

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.

如果要生成Excel(或Word),可以使用openXml。您可以完全按照纯代码的方式创建新文档。

OpenXml SDK page

OpenXml SDK页面

#3


0  

Have you looked into the reportviewer control, which is part of Visual Studio?

您是否查看过reportviewer控件,它是Visual Studio的一部分?

It allows you to export the report in PDF or Excel format.

它允许您以PDF或Excel格式导出报告。

#4


0  

http://www.carlosag.net/tools/excelxmlwriter/sample

check this might be useful for you

检查这可能对你有用

#5


0  

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.

有很多报告解决方案,例如SQL Server Reporting Services(您可能已经拥有许可证)。看看Winforms中Crystal Reports的报告(免费||开源)替代方案,它可能会通过一些序列化应用于Web。

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.

我建议根据情况考虑自己动手。您可以使用pdfsharp作为pdf导出,使用EPPlus作为excel。它们都非常易于使用,而且我很确定,只需点击几下即可在nuget中使用。

#6


0  

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.

如果您想要使用Excel路线,我建议使用Stephen Walther撰写的题为ASP.NET MVC技巧#2的文章 - 创建一个返回Microsoft Excel文档的自定义操作结果。这使用了一种使用Excel mime类型编写HTML文档的旧技巧。这与流式传输本机Excel文件不同。如果要将其删除,则将其更改为渲染CSV文件相当容易,并使其成为更通用的文件。如果有可能出现逗号,请记住重复引用所有字段。

#7


0  

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:

如果你做的不是太复杂,你可以使用CSV文件。 CSV代表逗号分隔值,它听起来像。您可以使用逗号创建简单的表和列。例如,将以下行粘贴到文本文件中:

heading1,heading2,heading3
info1,info2,info3
info1,info2,info3

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.

将文本文件另存为.csv文件和voila - 一个Excel电子表格。显然,构建这些循环对象集合非常容易。请注意,如果您需要任何复杂的文本格式等,那么它不是真正的最佳选择。

#1


1  

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.

在这种情况下,我通常向客户提出的解决方案是使用Sql Server Reporting Services(SSRS)。您可以使用其中包含的ReportViewer控件来生成PDF,Excel电子表格,XML文件,CSV文件等。如果您需要临时报告,也可以使用报告生成器。

Barring that, you can use OpenXml to generate Excel spreadsheets and there are a host of PDF toolkits available.

除此之外,您可以使用OpenXml生成Excel电子表格,并且有许多PDF工具包可用。

#2


5  

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.

如果要生成Excel(或Word),可以使用openXml。您可以完全按照纯代码的方式创建新文档。

OpenXml SDK page

OpenXml SDK页面

#3


0  

Have you looked into the reportviewer control, which is part of Visual Studio?

您是否查看过reportviewer控件,它是Visual Studio的一部分?

It allows you to export the report in PDF or Excel format.

它允许您以PDF或Excel格式导出报告。

#4


0  

http://www.carlosag.net/tools/excelxmlwriter/sample

check this might be useful for you

检查这可能对你有用

#5


0  

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.

有很多报告解决方案,例如SQL Server Reporting Services(您可能已经拥有许可证)。看看Winforms中Crystal Reports的报告(免费||开源)替代方案,它可能会通过一些序列化应用于Web。

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.

我建议根据情况考虑自己动手。您可以使用pdfsharp作为pdf导出,使用EPPlus作为excel。它们都非常易于使用,而且我很确定,只需点击几下即可在nuget中使用。

#6


0  

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.

如果您想要使用Excel路线,我建议使用Stephen Walther撰写的题为ASP.NET MVC技巧#2的文章 - 创建一个返回Microsoft Excel文档的自定义操作结果。这使用了一种使用Excel mime类型编写HTML文档的旧技巧。这与流式传输本机Excel文件不同。如果要将其删除,则将其更改为渲染CSV文件相当容易,并使其成为更通用的文件。如果有可能出现逗号,请记住重复引用所有字段。

#7


0  

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:

如果你做的不是太复杂,你可以使用CSV文件。 CSV代表逗号分隔值,它听起来像。您可以使用逗号创建简单的表和列。例如,将以下行粘贴到文本文件中:

heading1,heading2,heading3
info1,info2,info3
info1,info2,info3

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.

将文本文件另存为.csv文件和voila - 一个Excel电子表格。显然,构建这些循环对象集合非常容易。请注意,如果您需要任何复杂的文本格式等,那么它不是真正的最佳选择。