This method will create a new Excel Spreadsheet. Pass in the fileName which is a full file path name.
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
....
void Create(string fileName)
{
using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook))
{
var relationshipId = "rId1";
//build Workbook Part
var workbookPart = document.AddWorkbookPart();
var workbook = new Workbook();
var sheets = new Sheets();
var sheet1 = new Sheet() { Name = "First Sheet", SheetId = 1, Id = relationshipId };
sheets.Append(sheet1);
workbook.Append(sheets);
workbookPart.Workbook = workbook;
//build Worksheet Part
var workSheetPart = workbookPart.AddNewPart<WorksheetPart>(relationshipId);
var workSheet = new Worksheet();
workSheet.Append(new SheetData());
workSheetPart.Worksheet = workSheet;
//add document properties
document.PackageProperties.Creator = "Your Name";
document.PackageProperties.Created = DateTime.UtcNow;
}
For this project, make sure you include the reference to DocumentFormat.OpenXml. This is located in the path specified in the Installing OpenXML Example.
The spreadsheet will be created with Your Name as the Author and the first Worksheet named First Sheet.