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" };
            sheets.Append(sheet);

            workbookpart.Workbook.Save();

            // Close the document.
            spreadsheetDocument.Close();
        }

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

 CreateSpreadsheetWorkbook("C:\\Test\\Test.xlsx");

The entire method will look like this:

static void Main(string[] args)
        {
            CreateSpreadsheetWorkbook("C:\\Test\\Test.xlsx");
        }

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" };
sheets.Append(sheet);

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 -  https://msdn.microsoft.com/en-us/library/office/hh180830(v=office.14).aspx