Click or drag to resize

Start creating Excel spreadsheets in C#/VB.NET with ExpertXls - The ExcelWorkbook class

The ExcelWorkbook class represents an Excel workbook. Create an empty workbook in the Excel 97-2003 (.xls) or Excel 2007 or later (.xlsx) format, or open an existing workbook from a file or a stream, change it with the library and save it.

A workbook has a collection of worksheets, a collection of chart worksheets, named styles, named ranges, document properties and security settings. The cells of a worksheet are accessed as ranges.

Create a Workbook

A new workbook has one empty worksheet. The ExcelWorkbookFormat given to the constructor is also the format of the saved file. The constructor without parameters creates an Excel 97-2003 workbook.

// Excel 97-2003 (.xls)
ExcelWorkbook xlsWorkbook = new ExcelWorkbook(ExcelWorkbookFormat.Xls_2003);

// Excel 2007 or later (.xlsx)
ExcelWorkbook xlsxWorkbook = new ExcelWorkbook(ExcelWorkbookFormat.Xlsx_2007);

// with default settings for the new worksheets: font, row height, column width
ExcelWorkbookDefaultSettings settings = new ExcelWorkbookDefaultSettings();
settings.DefaultFontName = "Calibri";
settings.DefaultFontSize = 11;
ExcelWorkbook customWorkbook = new ExcelWorkbook(ExcelWorkbookFormat.Xlsx_2007, settings);
Open an Existing Workbook

An existing workbook is opened from a file or from a stream. The format of the opened workbook is the format of the file. A workbook protected with a password to open is opened with the constructors that take the password.

// from a file
ExcelWorkbook workbook = new ExcelWorkbook(@"C:\Data\Report.xlsx");

// from a stream
using (FileStream stream = new FileStream(@"C:\Data\Report.xls", FileMode.Open, FileAccess.Read))
{
    ExcelWorkbook workbookFromStream = new ExcelWorkbook(stream);
    workbookFromStream.Close();
}

// a workbook with a password to open
ExcelWorkbook protectedWorkbook = new ExcelWorkbook(@"C:\Data\Protected.xls", "password");
Save and Close

The Save method saves the workbook to a file, to a stream, to a byte array or to an ASP.NET HTTP response. Call Close when the workbook is no longer needed to release its resources.

// to a file
workbook.Save(@"C:\Data\Output.xlsx");

// to a stream, for example a MemoryStream
using (MemoryStream stream = new MemoryStream())
{
    workbook.Save(stream);
}

// to a byte array
byte[] workbookBytes = workbook.Save();

workbook.Close();

In an ASP.NET Web Forms page the workbook can be sent to the browser as a file download:

// the browser receives the workbook as an attachment named Report.xlsx
workbook.Save(Response, "Report.xlsx");
Document Properties

The DocumentProperties are the title, subject, author, company and the other properties shown by Excel in File > Info:

workbook.DocumentProperties.Title = "Sales Report";
workbook.DocumentProperties.Subject = "Quarterly sales";
workbook.DocumentProperties.Author = "Sales Department";
workbook.DocumentProperties.Company = "My Company";
workbook.DocumentProperties.Comments = "Created with ExpertXls Excel Library for .NET";
See Also