.net – Create Excel (.XLS and .XLSX) file from C# – Stack Overflow


You can use a library called ExcelLibrary. It\’s a free, open source library posted on Google Code: ExcelLibrary 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. ExcelLibrary

  • 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 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

  • You can also use EPPlus, which works only for Excel 2007/2010 format files (.xlsx files).
  • 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.

 

  • Also, EPPlus has support for Pivot Tables and ExcelLibrary may have some support (Pivot table issue in ExcelLibrary)

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

ExcelLibrary

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.

ExcelLibrary

  • 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 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

  • You can also use EPPlus, which works only for Excel 2007/2010 format files (.xlsx files).
  • 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.

 

  • Also, EPPlus has support for Pivot Tables and ExcelLibrary may have some support (Pivot table issue in ExcelLibrary)

 

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:

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

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.

 

Download links –

 

https://code.google.com/p/excellibrary/downloads/detail?name=ExcelLibrary_20110730.zip&can=2&q=

 

https://code.google.com/p/excellibrary/

 

Happy Coding … J

 

Advertisements

2 thoughts on “.net – Create Excel (.XLS and .XLSX) file from C# – Stack Overflow

    1. Hello Kimmy, First I would like to thank you for visiting the blog. I really appreciate the comment you’ve posted and would definitely consider your link recommendation. I will publish another article with some help and information regarding your link …

      Thank you once again and Happy Coding… 🙂

      Bhavin Patel

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s