简体   繁体   English

使用 C# 从 ASP.NET 将 DataTable 导出到 Excel

[英]Export DataTable to Excel with C# from ASP.NET

I have an ASP.NET web forms app with a button on it.我有一个 ASP.NET web forms 应用程序,上面有一个按钮。 This button allows a user to download some content to Excel.此按钮允许用户将一些内容下载到 Excel。 However, I'm having problems getting it to work.但是,我在让它工作时遇到了问题。 Everyone has articles that are like export DataGrid or GridView to Excel.每个人都有类似导出DataGrid或GridView到Excel的文章。 My problem is, I have a plain old DataTable.我的问题是,我有一个普通的旧数据表。 I can't put this DataTable in my UI.我不能把这个 DataTable 放在我的 UI 中。 At the same time, I do not want to use the Excel libraries.同时,我不想使用 Excel 库。

Does anyone know a way to export a DataTable to Excel with formulas?有谁知道使用公式将 DataTable 导出到 Excel 的方法?

You can use ClosedXML library (an OpenXML wrapper) to create any Excel you want dynamically.您可以使用ClosedXML库(一个 OpenXML 包装器)动态创建您想要的任何 Excel。

Office Writer is a very powerful and equally expensive solution which I have used in the past. Office Writer是我过去使用过的一个非常强大且同样昂贵的解决方案。 ClosedXML in Peters answer also looks good (and free). Peters 回答中的 ClosedXML 看起来也不错(而且免费)。

If you don't need Formulas this answer to another SO question may help you out.如果您不需要公式,这个对另一个 SO 问题的回答可能会对您有所帮助。 No library required.不需要图书馆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM