Simplest code needed to create an Excel 2010 file with Open XML SDK 2.0

Open XML can be very frustrating. In order to help myself understand the package, parts and relationships I have tried to find the bare minimum amount of C# code needed to generate a valid empty excel file. The inline constructors help to show the hierarchy.

Please let me know if you have found a simpler and less verbose way.

The bare minimum code required:

// By default AutoSave will be true and Editable = true, and Type = xlsx
using (var doc = SpreadsheetDocument.Create("Empty.xlsx", SpreadsheetDocumentType.Workbook))
  // Creates 4 things: WorkBookPart, WorkSheetPart, WorkSheet, SheetData
  doc.AddWorkbookPart().AddNewPart<WorksheetPart>().Worksheet = new Worksheet(new SheetData());

  doc.WorkbookPart.Workbook =
    new Workbook(
      new Sheets(
        new Sheet
            // Id is used to create Sheet to WorksheetPart relationship
            Id = doc.WorkbookPart.GetIdOfPart(doc.WorkbookPart.WorksheetParts.First()),
            // SheetId and Name are both required
            SheetId = 1,
            Name = "Empty Sheet"

This will create a file called Empty.xlsx in the directory you ran the code from. If you wanted to work from this code you could start by injecting your own data into the SheetData() constructor.

Other Open XML sources: