Categories
.net c# excel file-io

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

2092

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

2

  • 60

    @Mike The “without requiring Excel to be installed” piece has nothing to do with being professional. It’s about dependencies. The original text of the question was worded as: “Ideally, I would like open source so I don’t have to add any third party dependencies to my code, and I would like to avoid using Excel directly to create the file (using OLE Automation.)” It’s unfortunate the question was drastically simplified.

    – Tony

    Apr 2, 2019 at 15:02


  • 7

    Assuming you were trying to do something sans library or external code, I can’t speak for xls file, but for xlsx files, why not start by taking an existing one, renaming it to a zip file and exploring the contents? A little bit of reverse engineering will tell you quite a bit. There are several different xml files and rels files in the various folders and subfolders. Try exploring that and see if it’s something you can replicate or see if you can find documentation on the various xml namespaces/schemas.

    Aug 13, 2019 at 21:37


1149

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.

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.

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

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, as noted by @АртёмЦарионов below, 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:
ExcelLibraryGNU Lesser GPL
EPPlusGNU (LGPL) – No longer maintained
EPPlus 5Polyform Noncommercial – Starting May 2020
NPOIApache License

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.

10

636

If you are happy with the xlsx format, try my library, EPPlus. It started with the source from ExcelPackage, but since became a total rewrite.

It supports ranges, cell styling, charts, shapes, pictures, named ranges, AutoFilter, and a lot of other stuff.

You have two options:

  • EPPlus 4, licensed under LGPL (original branch, developed until 2020)

  • EPPlus 5, licensed under Polyform Noncommercial 1.0.0 (since 2020).

From the EPPlus 5 readme.md:

With the new license EPPlus is still free to use in some cases, but will require a commercial license to be used in a commercial business.

EPPlus website: https://www.epplussoftware.com/

4

  • 16

    The examples were helpful. I was able to change my code from using Microsoft interop library (horribly slow) to this library (version 4.x) in a couple hours. My benchmark writes a file with two tabs and about 750,000 cells. Using MS interop it took 13 minutes. Using EPPlus it took 10 seconds, a roughly 80x speedup. Very happy!

    Feb 10, 2015 at 18:55

  • @JanKällman You should update your CodePlex page to show you’ve got these methods available: LoadFromCollection<T>, LoadFromDataTable etc. (found via here)

    – PeterX

    Feb 24, 2015 at 3:39

  • 3

    For clarity in this thread, the LGPL allows the software to be linked to without the infective part of the GPL occuring. You only need to open source changes you make to ClosedXml or if you directly put the source code (as opposed to referencing the ClosedXml assemblies) inside of your application then you need to open source your application.

    Aug 12, 2015 at 16:10

  • 9

    @Paul Chernoch: We populate large Excel sheets with interop very quickly. The secret is to do a bulk update. Create a object [,] block, populate that, then write that matrix to Excel at one time: excelWorksheet.get_Range(range).Value2 = block;

    Feb 15, 2018 at 22:53

212

And what about using Open XML SDK 2.0 for Microsoft Office?

A few benefits:

  • Doesn’t require Office installed
  • Made by Microsoft = decent MSDN documentation
  • Just one .Net dll to use in project
  • SDK comes with many tools like diff, validator, etc

Links:

8

  • 4

    Important to note that the DLL for this is just over 5 MB and limited to Office 2007 formats. But certainly the easiest and fastest solution which works for me.

    Sep 20, 2011 at 13:03

  • 19

    Just a heads up that v2.5 is out and can be downloaded here.

    Jan 4, 2013 at 16:47

  • 12

    The SDK models the XML into classes, so that each XML tag is mapped to a tag, and then you have to build the class hierarchy (each instance has a collection of child instances/tags) correctly. This means you have to know the XML structure of an Excel file, which is very complicated. It’s much easier to use a wrapper such as EPPlus, mentioned above, which simplifies things.

    Dec 24, 2014 at 16:27

  • 2

    A great sample of Microsoft Open XML SDK – Open XML Writer can be found at polymathprogrammer.com/2012/08/06/… Or see Stack Overflow solution stackoverflow.com/questions/11370672/…

    – Greg

    Feb 17, 2017 at 17:51

  • 4

    I found Microsoft Open XML SDK’s Open XML Writer to be great. Using the solutions above, (Especially Vincent Tom’s sample (Poly Math)), it’s easy to build a writer that streams through big sets of data, and writes records in a manner similiar and not too much more complex to what you’d do for CSV; but that you’re instead writing xml. Open XML is the mindset that Microsoft considers it’s new Office formats in. And you can always rename them from .xslx to .zip files if you feel like poking at their XML contents.

    – Greg

    Feb 17, 2017 at 17:54