-
Notifications
You must be signed in to change notification settings - Fork 30
Examples
Let's begin with a few examples.
Workbook workbook = new Workbook();
workbook.Open("Book1.xlsx");
var sheets = workbook.Sheets;
foreach (Sheet sheet in sheets)
{
Console.WriteLine(sheet.Name);
}
The Sheets property, like most collections in ExcelLibrary, is an IEnumerable.
Sheet sheet = workbook.Sheet("Sheet1");
This is just a shortcut for
Sheet sheet = workbook.Sheets.Where(s => s.Name == "Sheet1").SingleOrDefault();
var rows = sheet.Rows;
foreach (Row row in rows)
{
...
}
Rows and columns are treated identically, so it doesn't matter if you want to loop through your data row by row, or column by column. Use the Columns property to get all columns on a sheet:
var columns = sheet.Columns;
Row row = sheet.Row(5);
Column column = sheet.Column(2);
Again, the Row() and Column() methods are just shortcuts for LINQ queries:
Row row = sheet.Rows.Where(r => r.Index == 5).SingleOrDefault();
Column column = sheet.Columns.Where(c => c.Index == 2).SingleOrDefault();
The number of shortcut methods in the library are kept to a minimum, but since sheets, rows, columns and cells are all returned as generic IEnumerable<T> collections, you still got infinite flexibility to select what you need with LINQ queries.
Cells are the leaf nodes of the library. Apart from storing the cell value, cells also contain a property for the datatype, and references to its parent row and column.
var cellsInRow = row.Cells;
var cellsInColumn = column.Cells;
string text = cell.Value;
Before opening a workbook, you can set a few options. The IncludeHidden property is set to false by default, which means that hidden sheets, rows and columns are ignored by the library. Set it to true to return everything:
Workbook workbook = new Workbook();
WorkbookOptions options = new WorkbookOptions();
options.IncludeHidden = true;
workbook.Open("Book1.xlsx", options);
There is also a LoadSheets property. It defaults to true, meaning that the contents of every sheet is loaded automatically when the workbook is opened. If this feels wasteful, you can set it to false to prevent auto-loading. After the workbook is opened, you can then load sheets individually:
Workbook workbook = new Workbook();
WorkbookOptions options = new WorkbookOptions();
options.LoadSheets = false;
workbook.Open("Book1.xlsx", options);
Sheet sheet = workbook.Sheet("Sheet3");
sheet.Open();
There's a natural hierarchy from the workbook to sheets, to rows and columns, to cells. Each object is aware of its parent, so you can easily find a cell's row, its sheet or even its workbook:
Workbook currentWorkbook = cell.Row.Sheet.Workbook;
The library doesn't do much exception handling; it simply assumes that the Excel file has not been tampered with. The two most common exceptions you'll encounter are FileNotFoundException and IOException, and those should be handled in your own application:
try
{
Workbook workbook = new Workbook();
workbook.Open("Book1.xlsx");
...
}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine("The file cannot be found.");
return;
}
catch (System.IO.IOException)
{
Console.WriteLine("The file is already opened.");
return;
}