如何在不安装 Microsoft Office 的情况下在 C# 中创建 Excel(.XLS 和 .XLSX)文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/151005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?
提问by mistrmark
How can I create an Excel spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?
如何使用 C# 创建 Excel 电子表格而不需要在运行代码的机器上安装 Excel?
采纳答案by Mike Webb
You can use a library called ExcelLibrary. It's a free, open source library posted on Google Code:
您可以使用名为 ExcelLibrary 的库。这是一个发布在 Google Code 上的免费开源库:
This looks to be a port of the PHP ExcelWriter that you mentioned above. It will not write to the new .xlsx format yet, but they are working on adding that functionality in.
这看起来是你上面提到的 PHP ExcelWriter 的一个端口。它还不会写入新的 .xlsx 格式,但他们正在努力添加该功能。
It's very simple, small and easy to use. Plus it has a DataSetHelper that lets you use DataSets and DataTables to easily work with Excel data.
它非常简单、小巧且易于使用。此外,它还有一个 DataSetHelper,可让您使用 DataSets 和 DataTables 轻松处理 Excel 数据。
ExcelLibrary seems to still only work for the older Excel format (.xls files), but may be adding support in the future for newer 2007/2010 formats.
ExcelLibrary 似乎仍然只适用于较旧的 Excel 格式(.xls 文件),但将来可能会增加对较新的 2007/2010 格式的支持。
You can also use EPPlus, which works only for Excel 2007/2010 format files (.xlsx files). There's also NPOIwhich works with both.
您还可以使用EPPlus,它仅适用于 Excel 2007/2010 格式文件(.xlsx 文件)。还有NPOI可以同时使用。
There are a few known bugs with each library as noted in the comments. In all, EPPlus seems to be the best choice as time goes on. It seems to be more actively updated and documented as well.
如评论中所述,每个库都有一些已知的错误。总而言之,随着时间的推移,EPPlus 似乎是最好的选择。它似乎也更积极地更新和记录。
Also, as noted by @АртёмЦарионов below, EPPlus has support for Pivot Tables and ExcelLibrary may have some support (Pivot table issue in ExcelLibrary)
此外,正如下面@АртёмЦарионов 所指出的,EPPlus 支持数据透视表,而 ExcelLibrary 可能有一些支持(ExcelLibrary 中的数据透视表问题)
Here are a couple links for quick reference:
ExcelLibrary- GNU Lesser GPL
EPPlus- GNU Lesser General Public License (LGPL)
NPOI- Apache License
这里有几个链接供快速参考:
ExcelLibrary- GNU Lesser GPL
EPPlus- GNU Lesser General Public License (LGPL)
NPOI- Apache License
Here some example code for ExcelLibrary:
这里有一些 ExcelLibrary 的示例代码:
Here is an example taking data from a database and creating a workbook from it. Note that the ExcelLibrary code is the single line at the bottom:
这是从数据库中获取数据并从中创建工作簿的示例。请注意 ExcelLibrary 代码是底部的单行:
//Create the data set and table
DataSet ds = new DataSet("New_DataSet");
DataTable dt = new DataTable("New_DataTable");
//Set the locale for each
ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
//Open a DB connection (in this example with OleDB)
OleDbConnection con = new OleDbConnection(dbConnectionString);
con.Open();
//Create a query and fill the data table with the data from the DB
string sql = "SELECT Whatever FROM MyDBTable;";
OleDbCommand cmd = new OleDbCommand(sql, con);
OleDbDataAdapter adptr = new OleDbDataAdapter();
adptr.SelectCommand = cmd;
adptr.Fill(dt);
con.Close();
//Add the table to the data set
ds.Tables.Add(dt);
//Here's the easy part. Create the Excel worksheet from the data set
ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds);
Creating the Excel file is as easy as that. You can also manually create Excel files, but the above functionality is what really impressed me.
创建 Excel 文件就是这么简单。您也可以手动创建 Excel 文件,但上述功能给我留下了深刻的印象。
回答by GEOCHET
You actually might want to check out the interop classes available in C# (e.g. Microsoft.Office.Interop.Excel
. You say no OLE (which this isn't), but the interop classes are very easy to use. Check out the C# Documentation here(Interop for Excel starts on page 1072 of the C# PDF).
您实际上可能想查看 C# 中可用的互操作类(例如Microsoft.Office.Interop.Excel
。您说没有 OLE(这不是),但是互操作类非常易于使用。请在此处查看C# 文档(Interop for Excel 开始于C# PDF 的第 1072 页)。
You might be impressed if you haven't tried them.
如果你没有尝试过,你可能会印象深刻。
Please be warned of Microsoft's stanceon this:
请注意微软对此的立场:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
Microsoft 当前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)自动化 Microsoft Office 应用程序,因为 Office 可能表现出不稳定的行为和/或在此环境中运行 Office 时出现死锁。
回答by Forgotten Semicolon
An extremely lightweight option may be to use HTML tables. Just create head, body, and table tags in a file, and save it as a file with an .xls extension. There are Microsoft specific attributes that you can use to style the output, including formulas.
一个非常轻量级的选择可能是使用 HTML 表格。只需在文件中创建 head、body 和 table 标签,并将其保存为扩展名为 .xls 的文件。您可以使用 Microsoft 特定的属性来设置输出的样式,包括公式。
I realize that you may not be coding this in a web application, but here is an exampleof the composition of an Excel file via an HTML table. This technique could be used if you were coding a console app, desktop app, or service.
我意识到您可能不会在 Web 应用程序中对此进行编码,但这里是通过 HTML 表组成 Excel 文件的示例。如果您正在编写控制台应用程序、桌面应用程序或服务,则可以使用此技术。
回答by Panos
You can use OLEDB to create and manipulate Excel files. Check this: Reading and Writing Excel using OLEDB.
您可以使用 OLEDB 来创建和操作 Excel 文件。检查这个:使用 OLEDB 读取和写入 Excel。
Typical example:
典型例子:
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\test.xls;Extended Properties='Excel 8.0;HDR=Yes'"))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("CREATE TABLE [Sheet1] ([Column1] string, [Column2] string)", conn);
cmd.ExecuteNonQuery();
}
EDIT - Some more links:
编辑 - 更多链接:
回答by ManiacZX
You may want to take a look at GemBox.Spreadsheet.
您可能想看看GemBox.Spreadsheet。
They have a free version with all features but limited to 150 rows per sheet and 5 sheets per workbook, if that falls within your needs.
他们有一个具有所有功能的免费版本,但如果符合您的需要,每张纸限制为 150 行,每个工作簿限制为 5 张。
I haven't had need to use it myself yet, but does look interesting.
我还没有需要自己使用它,但看起来确实很有趣。
回答by Nick
The Java open source solution is Apache POI. Maybe there is a way to setup interop here, but I don't know enough about Java to answer that.
Java 开源解决方案是Apache POI。也许有一种方法可以在这里设置互操作,但我对 Java 的了解不够,无法回答这个问题。
When I explored this problem I ended up using the Interop assemblies.
当我探索这个问题时,我最终使用了 Interop 程序集。
回答by Sam Warwick
You could consider creating your files using the XML Spreadsheet 2003format. This is a simple XML format using a well documented schema.
您可以考虑使用XML Spreadsheet 2003格式创建文件。这是一个简单的 XML 格式,使用了一个有据可查的模式。
回答by Ryan Lundy
Here's a way to do it with LINQ to XML, complete with sample code:
这是一种使用 LINQ to XML 完成此操作的方法,并附有示例代码:
Quickly Import and Export Excel Data with LINQ to XML
使用 LINQ to XML 快速导入和导出 Excel 数据
It's a little complex, since you have to import namespaces and so forth, but it does let you avoid any external dependencies.
这有点复杂,因为您必须导入命名空间等,但它确实可以让您避免任何外部依赖项。
(Also, of course, it's VB .NET, not C#, but you can always isolate the VB .NET stuff in its own project to use XML Literals, and do everything else in C#.)
(当然,它是 VB .NET,而不是 C#,但您始终可以在自己的项目中隔离 VB .NET 内容以使用 XML 文字,并在 C# 中执行其他所有操作。)
回答by Aaron Powell
I agree about generating XML Spreadsheets, here's an example on how to do it for C# 3 (everyone just blogs about it in VB 9 :P) http://www.aaron-powell.com/linq-to-xml-to-excel
我同意生成 XML 电子表格,这里有一个关于如何为 C# 3 做的例子(每个人都只是在 VB 9 中写博客:P)http://www.aaron-powell.com/linq-to-xml-to-擅长
回答by Stefan Koelle
Have you ever tried sylk?
你试过 sylk 吗?
We used to generate excelsheets in classic asp as sylk and right now we're searching for an excelgenerater too.
我们曾经在经典 asp 中生成 excelsheets 作为 sylk,现在我们也在寻找一个 excelgenerator。
The advantages for sylk are, you can format the cells.
sylk 的优点是,您可以格式化单元格。