How to: Use the Open XML SDK 2.0 in Visual Studio 2010 to create a simple Excel Spreadsheet (in C#)

There are plenty of examples out on the internet to use the Open XML SDK 2.0 and Visual Studio 2010 to create Excel workbooks. I wanted to create my own how-to because the other examples I found seemed overly complex and some lacked basic information such as what references and namespaces need to be included.

1. Install the Open XML SDK 2.0 from here. You will want to download and install both the OpenXMLSDKTool.msi and OpenXMLSDKv2.msi

2. Once installed, Launch Visual Studio 2010. Create a new Windows Console Application under C#. Give your Console Application a name and click OK

3. The project is then created. Now you need to add references. Click Project > Add Reference and under the .NET tab look for "DocumentFormat.OpenXML". Click this reference and click OK. Again, Click Project > Add Reference and under the .NET tab look for "WindowsBase". Select this reference and click OK.

4. In your code, add the following namespaces:

using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

5. Under your "program" class, find "Static void Main (string[] args)". After the closing terminator add the following method:

public static void CreateSpreadsheetWorkbook(string filepath)
            // Create a spreadsheet document by supplying the filepath.
            // By default, AutoSave = true, Editable = true, and Type = xlsx.
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);

            // Add a WorkbookPart to the document.
            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();

            // Add a WorksheetPart to the WorkbookPart.
            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add Sheets to the Workbook.
            Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

            // Append a new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };


            // Close the document.

6. In your Main method, add the following line of code:


The entire method will look like this:

static void Main(string[] args)

What we are doing here is calling the  CreateSpreadsheetWorkbook method and telling it to create our basic spreadsheet in a directory on the C:\ drive called Test. We are also assigning a name to the workbook of "Text.XLSX" (if we didn't assign a name you would see an "Access Denied" error when the code is run"). Also, you will want to make sure the directory exists or else you will get a "DirectoryNotFoundException" during runtime.

At this point you should save the project and run it. If you have a directory C:\Test you should see a workbook called "Text.XLSX" appear in that folder. This is a very basic spreadsheet. It contains no data and only a single sheet called "mySheet". This is done by the two lines of code in the CreateSpreadsheetWorkbook method.


Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };

If you want to know more about customizing the workbook, check out the following reference: 

Generating Excel 2010 Workbooks by using the Open XML SDK 2.0 -

Comments (5)

  1. Gaurav Verma says:

    Thanks you so much… THis is really good to understand the basic…

    Thanks alot. 🙂

  2. Mohit says:

    Thanks for nice explaination – got few clarifications to be made.

    1) Unable to compile code as  it not been able to fine using "DocumentFormat.OpenXml.Spreadsheet"; I tried in 3.5 and 4.0 aswell. Not sure whats missing.

    2) Is this 64 bit compatible?  I havent tried yet, but curious to know.


  3. Supriya says:

    How to add colors to the cell based on cell values using Open xml.(my data is in data table)
    One column can have more than three to four colors.

  4. Pritam says:

    Thanks it worked for XML SDK 2.5 too.. 🙂

  5. Lukas says:

    Thak you, it helped.

Skip to main content