Click here to Skip to main content
14,336,226 members

C# Export data to Excel, using OpenXML libraries

Rate this:
4.78 (30 votes)
Please Sign up or sign in to vote.
4.78 (30 votes)
17 Jun 2014CPOL
Export your data to a real Excel file, using one line of code!


It's amazing that even now, in 2014, there are so many developers still asking for help on how to write C# and VB.Net code, to export their data to Excel.

Even worse, a lot of them will stumble on articles suggesting that they should write their data to a comma-separated file, but to give the file an .xls extension.

So, today, I'm going to walkthrough how to use my C# "Export to Excel" class, which you can add to your C# WinForms / WPF / ASP.Net application, using one line of code. Simply tell the class where your DataSet, DataTable, or List<> data is stored, and what (Excel) filename you want to write to.

// Step 1: Create a DataSet, and put some sample data in it
DataSet ds = CreateSampleData();
// Step 2: Create the Excel .xlsx file
    string excelFilename = "C:\\Sample.xlsx";
    CreateExcelFile.CreateExcelDocument(ds, excelFilename);
catch (Exception ex)
    MessageBox.Show("Couldn't create Excel file.\r\nException: " + ex.Message);

This article is based on some code I have documented on my blog, MikesKnowledgeBase.

Adding the library to your application

The C# code above shows how easy it is to call the CreateExcelFile class.

DataSet ds = CreateSampleData();
CreateExcelFile.CreateExcelDocument(ds, "C:\\Sample.xlsx");

However, to use this library, you'll need to add two files from the free Microsoft OpenXML SDK:

  • DocumentFormat.OpenXml.dll From the free Microsoft Open XML SDK library
  • WindowsBase.dll From the Microsoft .NET Framework library

Add these two .dlls to your project's References section, and remember to set them to "Copy Local".

Copy Local

Then, just download the CreateExcelFile.cs file, and add it to your application.

And that's it.

Regardless of if your data is stored in a List<>, DataTable or DataSet, you can export it to a "real" Office 2007 Excel .xlsx file using that one line of code.

And because it is created using the OpenXML library, you can run this code on machines which don't have Excel installed.


For ASP.NET developers, I've added three extra functions.

public static bool CreateExcelDocument<T>(List<T> list, string filename, System.Web.HttpResponse Response)
public static bool CreateExcelDocument(DataTable dt, string filename, System.Web.HttpResponse Response)
public static bool CreateExcelDocument(DataSet ds, string filename, System.Web.HttpResponse Response) 

Rather than creating the Excel file in a temporary directory, then having to load in the file and output it to the webpage's HttpResponse, you can get the library to write directly to the HttpResponse.

However, by default, this functionality is disabled (to prevent build issues for the non ASP.NET developers). To enable these three functions, you need to make two changes.

First, uncomment the top line of the CreateExcelFile code, so that it now reads:


Now, you need to add a Reference to the System.Web library:

Adding a reference

Once you've done these two steps, the CreateExcelFile library is ready to go.

For example, in this example, my ASP.Net C# code has a list of Employee records, stored in a List<Employeee>. I add an "Export to Excel" button to my webpage, and when the user clicks on it, I just need one simple call to the CreateExcelFile class.

// In this example, I have a defined a List of my Employee objects.<br />class Employee;
List<Employee> listOfEmployees = new List<Employee>();

// The following ASP.NET code gets run when I click on my "Export to Excel" button.
protected void btnExportToExcel_Click(object sender, EventArgs e)
    // It doesn't get much easier than this...
    CreateExcelFile.CreateExcelDocument(listOfEmployees, "Employees.xlsx", Response);

And that's it. A real Excel file, in one line of code.

Going forward

You'll notice that this library is excellent for one job - writing plain, boring data to an Excel file. I haven't attempted to add any classes to add formatting, colors, pivot tables or anything else.

However, this class is an excellent way to get started (without paying for third-party software to create the Excel file for you), and if you want to take this further, you'll soon find that Googling will easily find you extra source code to add on top of this.

For example, if you wanted to add a background color to some of the cells in the Excel file, simply Google "open xml background color" and you'll have many articles showing you how to do this.

The reason I wrote this article is that I found that it was very hard to find a free, easy to use C# library which actually created the OpenXML Excel file in the first place.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michael Gledhill
Software Developer
Switzerland Switzerland
I'm a Senior Software Developer, working with Visual Studio and Xcode, living, working, and raising Swiss-English children, near Zurich, Switzerland.

Author of the PartnerReSearch iPad app, which was a winner of a "Business Insurance Innovation Award" in 2013, and a TechAward2014 "Innovation of the year" award in 2014.

Objective-C is the 2nd hardest language I've ever learned, after German... Wink | ;-)

Comments and Discussions

Questionresponse parameter Pin
ovisariesdk17-Mar-17 1:34
memberovisariesdk17-Mar-17 1:34 
QuestionExport data to xlsb format Pin
Member 122775207-Mar-17 18:02
memberMember 122775207-Mar-17 18:02 
QuestionExport data to Excel Pin
RMW0130-Aug-16 10:41
memberRMW0130-Aug-16 10:41 
PraiseVery good Pin
Member 1183618910-Dec-15 0:18
memberMember 1183618910-Dec-15 0:18 
GeneralRe: Very good Pin
Michael Gledhill10-Dec-15 1:00
memberMichael Gledhill10-Dec-15 1:00 
QuestionIt's work great Pin
VitorTrindade27-Oct-15 21:08
memberVitorTrindade27-Oct-15 21:08 
QuestionVery useful. How to fill Excel with EF entities? Pin
a.casnati13-Oct-15 0:23
membera.casnati13-Oct-15 0:23 
AnswerRe: Very useful. How to fill Excel with EF entities? Pin
Michael Gledhill14-Oct-15 2:15
memberMichael Gledhill14-Oct-15 2:15 
GeneralMy vote of 5 Pin
D V L23-Aug-15 19:53
professionalD V L23-Aug-15 19:53 
GeneralThanks Pin
pjhunter12-May-15 11:23
memberpjhunter12-May-15 11:23 
GeneralRe: Thanks Pin
Michael Gledhill12-May-15 20:55
memberMichael Gledhill12-May-15 20:55 
QuestionGreat solution Pin
BluEiS25-Feb-15 22:57
memberBluEiS25-Feb-15 22:57 
AnswerRe: Great solution Pin
Michael Gledhill25-Feb-15 22:59
memberMichael Gledhill25-Feb-15 22:59 
QuestionGreaaaat Thaaanks Pin
Dev_Fady...24-Feb-15 20:56
memberDev_Fady...24-Feb-15 20:56 
QuestionThank you Pin
Member 1141299530-Jan-15 5:16
memberMember 1141299530-Jan-15 5:16 
AnswerRe: Thank you Pin
Mike Gledhill30-Jan-15 5:41
memberMike Gledhill30-Jan-15 5:41 
QuestionVery Helpful but what a bout 3 sheets in one Excel file Pin
Dev_Fady...13-Dec-14 22:47
memberDev_Fady...13-Dec-14 22:47 
AnswerRe: Very Helpful but what a bout 3 sheets in one Excel file Pin
Michael Gledhill14-Dec-14 22:49
memberMichael Gledhill14-Dec-14 22:49 
QuestionRe: Very Helpful but what a bout 3 sheets in one Excel file Pin
Dev_Fady...15-Dec-14 0:50
memberDev_Fady...15-Dec-14 0:50 
GeneralMy vote of 5 Pin
canrut4-Dec-14 14:40
membercanrut4-Dec-14 14:40 
SuggestionAdded wrapper for while using Pin
Frank van Bokhoven4-Nov-14 21:55
memberFrank van Bokhoven4-Nov-14 21:55 
Questioni want to put background color on Excel Sheet Header Column Pin
Member 101945591-Sep-14 3:29
professionalMember 101945591-Sep-14 3:29 
AnswerRe: i want to put background color on Excel Sheet Header Column Pin
Mike Gledhill2-Sep-14 3:03
memberMike Gledhill2-Sep-14 3:03 
GeneralRe: i want to put background color on Excel Sheet Header Column Pin
Member 101945598-Sep-14 19:18
professionalMember 101945598-Sep-14 19:18 
QuestionThis may be interesting here: Pin
dietmar paul schoder29-Jul-14 10:31
professionaldietmar paul schoder29-Jul-14 10:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 4 Dec 2013

Tagged as


81 bookmarked