Start creating Excel spreadsheets in C#/VB.NET with ExpertXls - The ExcelWorkbook class |
The ExpertXls Excel Spreadsheet Library for .NET can create and edit Excel 97-2003 (.xls) and Excel 2007-2019 or above (.xlsx) documents. Additionally there is an interface for loading data from a CSV (Comma Separated) file or to save the data from a worksheet to a file in this format.
The ExcelWorkbook class represents an Excel workbook in the ExpertXls Excel Library framework. An ExcelWorkbook class can be created from an existing Excel document for modification or an empty workbook can be created.
A workbook contains a collection of worksheets and a worksheet is a collection of cells and ranges of cells. The cells and ranges of cells in a worksheet can be accessed using a reference string in A1 notation or can be accessed using the row and column indexes. When creating an ExcelWorkbook object from an existing Excel document, the existing Excel document is passed to the ExcelWorkbook class constructor as the path of the Excel file on disk or as a stream containing the Excel document image. In either case the resulted ExcelWorkbook object can be further modified using the ExpertXls Excel Library API.
There are also ExcelWorkbook constructors accepting a password when creating an ExcelWorkbook object from a password protected Excel document.
Use any of the following constructors to create an ExcelWorkbook object:
/// Creates a workbook with a single worksheet. The default workbook format is Excel 97-2003. /// To create a workbook for a different Excel format, like Excel 2007-2019, you can use the constructor with the Format parameter. ExcelWorkbook excel = new ExcelWorkbook(); /// Creates a workbook with a single worksheet. The format of the workbook is specified by the workbookFormat parameter. /// The currently supported formats are Excel 97-2003 (xls) format and Excel 2007-2019 or above (xlsx) format. ExcelWorkbook excel = new ExcelWorkbook(workbookFormat); /// Creates a workbook with a single worksheet using some predefined setting. The format of the workbook is specified by the workbookFormat parameter. /// The currently supported formats are Excel 97-2003 (xls) format and Excel 2007-2019 or above (xlsx) format. ExcelWorkbook excel = new ExcelWorkbook(workbookFormat, defaultSettings); /// Opens a workbook from the specified Excel file ExcelWorkbook excel = new ExcelWorkbook(excelFileName); /// Opens a workbook from the specified Excel file with the specified password ExcelWorkbook excel = new ExcelWorkbook(excelFileName, openPassword); /// Opens a workbook from the specified Excel file with the specified password using some predefined setting ExcelWorkbook excel = new ExcelWorkbook(excelFileName, openPassword, defaultSettings); /// Opens a workbook from the specified Excel stream ExcelWorkbook excel = new ExcelWorkbook(excelStream); /// Opens a workbook from the specified Excel stream with the specified password ExcelWorkbook excel = new ExcelWorkbook(excelStream, openPassword); /// Opens a workbook from the specified Excel stream with the specified password using some predefined setting ExcelWorkbook excel = new ExcelWorkbook(excelStream, openPassword, defaultSettings);
The code below shows how to create a workbook in the Excel 97-2003 format (.xls format):
The code below shows how to create a workbook in the Excel 2007-2019 format (.xlsx format):
The ExcelWorkbook class offers access to the Worksheets collection, global styles collection, named ranges collection, security features, document properties, etc. For example you can add a new worksheet to the workbook using the AddWorksheet() method of the ExcelWorkbook.Worksheets collection. The worksheets from collection can be accessed by index or by name.
The workbook properties like author name, subject, comments or the last modification data can be specified using the DocumentProperties property of the ExcelWorkbook class. This property returns an object of type ExcelDocumentProperties whose properties can be set with desired values: