EPPlus is a .net library that reads and writes Excel 2007/2010 files using the Open Office Xml format (xlsx). I use it when I need to read or create Excel files.
Reading an Excel file:
using (var ms = new MemoryStream(File.ReadAllBytes(filePath))) { using (var package = new ExcelPackage(ms)) { var ws = package.Workbook.Worksheets[1]; var startRow = ws.Dimension.Start.Row + 1; // first row contains column name/title var endRow = ws.Dimension.End.Row; for (var i = startRow; i <= endRow; i++) { if (ws.Cells[i, 1].Value != null) { personList.Add(new Person() { FirstName = ws.Cells[i, 1].Value != null ? Convert.ToInt32(ws.Cells[i, 1].Value) : "N/A", LastName = ws.Cells[i, 2].Value != null ? Convert.ToInt32(ws.Cells[i, 2].Value) : "N/A", Age = ws.Cells[i, 3].Value != null ? Convert.ToInt32(ws.Cells[i, 3].Value) : 0; }); } else { break; } } } }
Just very basic example code. Reading the file in a MemoryStream, which is not really needed. You could also just pass a FileInfo object to the Epplus constructor.
var file = new FileInfo(filePath); using (var package = new ExcelPackage(file)) { // do magic here }