[每日翻译]如何在c#里面创造一个excel文件

来源:互联网 发布:油皮保湿水 知乎 编辑:程序博客网 时间:2024/05/16 04:50

本人翻译水平不怎么样,如果有错误请各位大大提出来

来源:https://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp

如果你认同他的看法或者有所帮助,可以到网站为他点个赞

-------------------------------------------------------------------------


提问

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安装在机器上

回答


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

ExcelLibrary


你可以使用一个叫做 ExcelLibrary库文件,它放在googlecode 上是免费开源的。

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 让你用dataset和datatable轻松处理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看起来只处理xls格式,但是在未来会加入支持更加新的格式

You can also use EPPlus, which works only for Excel 2007/2010 format files (.xlsx files).

你也可以使用 EPPlus ,这个只能处理 xlsx文件

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.

在评论里每个库的小bug都留言了。总而言之,随着时间推移,EPPlus看起来是个最佳选择。因为它升级和修订文档像是很活跃的样子

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


同样的,如АртёмЦарионов用户所言,EPPlus也支持Pivot Tables,ExcelLibrary 对Pivot Tables也有某些支持。

Here are a couple links for quick reference:

下面是它们的链接。


ExcelLibrary - GNU Lesser GPL
EPPlus - GNU Library General Public License (LGPL)



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 setds.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文件真的很容易,你也可以自己创造一个文件。这些功能真的使我印象深刻



阅读全文
0 0
原创粉丝点击