如何在不安装 Microsoft Office 的情况下在 C# 中创建 Excel(.XLS 和 .XLSX)文件? [英] How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

查看:31
本文介绍了如何在不安装 Microsoft Office 的情况下在 C# 中创建 Excel(.XLS 和 .XLSX)文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 C# 创建 Excel 电子表格而不需要在运行代码的机器上安装 Excel?

How can I create an Excel spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?

推荐答案

您可以使用名为 ExcelLibrary 的库.这是一个发布在 Google Code 上的免费开源库:

You can use a library called ExcelLibrary. It's a free, open source library posted on Google Code:

ExcelLibrary

这看起来是你上面提到的 PHP ExcelWriter 的一个端口.它还不会写入新的 .xlsx 格式,但他们正在努力添加该功能.

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.

它非常简单、小巧且易于使用.此外,它还有一个 DataSetHelper,可让您使用 DataSets 和 DataTables 轻松处理 Excel 数据.

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.

ExcelLibrary 似乎仍然只适用于较旧的 Excel 格式(.xls 文件),但将来可能会增加对较新的 2007/2010 格式的支持.

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.

您还可以使用 EPPlus,它仅适用于 Excel 2007/2010 格式文件(.xlsx 文件).还有 NPOI 两者都适用.

You can also use EPPlus, which works only for Excel 2007/2010 format files (.xlsx files). There's also NPOI which works with both.

如评论中所述,每个库都有一些已知的错误.总而言之,随着时间的推移,EPPlus 似乎是最好的选择.它似乎也得到了更积极的更新和记录.

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 支持数据透视表,ExcelLibrary 可能有一些支持(ExcelLibrary 中的数据透视表问题)

Also, as noted by @АртёмЦарионов below, EPPlus has support for Pivot Tables and ExcelLibrary may have some support (Pivot table issue in ExcelLibrary)

这里有几个链接供快速参考:
ExcelLibrary - GNU 宽松 GPL
EPPlus - GNU (LGPL) - 不再维护
EPPlus 5 - Polyform Noncommercial - 从 2020 年 5 月开始
NPOI - Apache 许可证

Here are a couple links for quick reference:
ExcelLibrary - GNU Lesser GPL
EPPlus - GNU (LGPL) - No longer maintained
EPPlus 5 - Polyform Noncommercial - Starting May 2020
NPOI - Apache License

这里有一些 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:

//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);

创建 Excel 文件就是这么简单.您也可以手动创建 Excel 文件,但上述功能给我留下了深刻的印象.

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.

这篇关于如何在不安装 Microsoft Office 的情况下在 C# 中创建 Excel(.XLS 和 .XLSX)文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆