The fastest way to read Excel data in .NET
It's a .NET Standard library for reading data from an Excel .xslx format spreadsheet. It's compatible with .Net Framework 4.6.1+ and .Net Standard 2.0+.
Via Nuget, either the Package Manager Console:
PM> Install-Package LightweightExcelReader
or the .NET CLI:
> dotnet add package LightweightExcelReader
Like this:
//Instatiate a spreadsheet reader by file path:
var excelReader = new ExcelReader("/path/to/workbook.xlsx");
//or from a stream:
var excelReaderFromStream = new ExcelReader(stream);
//Get a worksheet by index:
var sheetReader = excelReader[0];
//or by name:
var sheetNamedMyAwesomeSpreadsheet = excelReader["MyAwesomeSpreadsheet"];
//Use ReadNext() to read the next cell in the spreadsheet:
while (sheetReader.ReadNext()) {
dictionaryOfCellValues.Add(sheetReader.Address, sheetReader.Value);
}
//ReadNext() returns false if the reader has read to the end of the spreadsheet
//Use ReadNextInRow() to read the next cell in the current row:
var dictionaryOfCellValues = new Dictionary<string, object>();
while (sheetReader.ReadNextInRow()) {
dictionaryOfCellValues.Add(sheetReader.Address, sheetReader.Value);
}
//ReadNextInRow() returns if the reader has read to the end of the current row:
//Get data for a specific cell:
object cellA1Value = sheetReader["A1"];
//for a range:
IEnumerable<object> cellsFromA1ToD4 = sheetReader["A1","D4"];
//or a row:
IEnumerable<object> row3 = sheetReader.Row(3);
//or a column:
IEnumerable<object> columnB = sheetReader.Column("B");
You bet. We've aimed to create the fastest Excel reader for .Net, and we think we've succeeded. Included in the repo is a Benchmark Dot Net benchmarking that compares the performance of LightweightExcelReader to OpenXml, ExcelDataReader and a good old fashioned OleDbDataAdapter. The benchmark reads a 500 row spreadsheet to memory. On a 2.7 GHz Quad-Core Intel Core i7 laptop running Windows 10, the results looked like this:
Package | Operation |
---|---|
OpenXml | 8.109 ms |
ExcelDataReader | 6.225 ms |
OleDbDataAdapter | 52.833 ms |
LightweightExcelReader | 2.670 ms |
LightweightExcelReader is right for you if:
- You only want to read Excel data - you don't want to create or edit a spreadsheet.
- You only need to read .xlsx format files (Excel 2007 and later).
- You don't want to map spreadsheet data into a collection of objects.
If you want to map a spreadsheet to a collection of objects, use our sister project, ExcelToEnumerable
If you want to create or edit Excel spreadsheets, we recommend ClosedXml.
If you need to read old-style .xls files, try ExcelDataReader
LightweightExcelReader is currently in beta. If you discover a bug please raise an issue or better still fork the project and raise a Pull Request if you can fix the issue yourself.