问题描述
如何创建一个Excel US preadsheet用C#,而不需要的Excel将是正在运行的code中的计算机上安装?
How can I create an Excel Spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?
推荐答案
您可以使用一个名为ExcelLibrary库。这是张贴在谷歌code的免费,开源的库:
You can use a library called ExcelLibrary. It's a free, open source library posted on Google Code:
这看起来是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的,让您使用数据集和数据表,以方便地与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文件)。
You can also use EPPlus, which works only for Excel 2007/2010 format files (.xlsx files).
有一些已知的bug作为评论指出,每个库。在所有的,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可能有一定的支持,支持(<一href="http://$c$c.google.com/p/excellibrary/issues/detail?id=98&q=pivot&colspec=ID%20Type%20Status%20Priority%20ReportedBy%20Owner%20Summary%20Opened">Pivot在ExcelLibrary 表问题)
Also, as noted by @АртёмЦарионов below, EPPlus has support for Pivot Tables and ExcelLibrary may have some support (Pivot table issue in ExcelLibrary)
下面是几个链接为快速参考:
ExcelLibrary - 的
EPPlus - GNU通用公共许可证(LGPL)
Here are a couple links for quick reference:
ExcelLibrary - GNU Lesser GPL
EPPlus - GNU Library General Public License (LGPL)
这里是一些例子$ C $下ExcelLibrary:
下面是从一个数据库获取数据,并从它建立一个工作簿的一个例子。注意,ExcelLibrary code是在底部的一行:
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文件,但上述功能是真正的IM pressed我。
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中(.XLS和.XLSX)从C#文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!