fileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
I've used the above code to read an Excel document, You use no1 or no2 depending on whether it is xls or xlsx, I've tried it with xlsx (using number 2 above and it works fine - however I'm using another plug in to export the document again- and this only works with xls, so I wanted to keep the excel data reader to 'xls' when I use this code (number 1 instead of number 2-(numbered above) I get an error when trying to read in the file:
index was outside the bounds of the array
Any idea as to why?