I had previously blogged about the reading data from excel. I know many of you guys are using that.
Recently Excel Data Reader has updated to v3. So our old code won’t work. So you need to change two things
- Install the nuget – Excel Data reader.dataset in addition to Excel Data Reader
- Change the ExcelToDataTable method in the code. PFB – the updated code.
private static DataTable ExcelToDataTable(string fileName, string sheetName) {
// Open file and return as Stream
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
using (var reader = ExcelReaderFactory.CreateReader(stream)) {
var result = reader.AsDataSet(new ExcelDataSetConfiguration() {
ConfigureDataTable = (data) => new ExcelDataTableConfiguration() {
UseHeaderRow = true
}
});
//Get all the tables
var table = result.Tables;
// store it in data table
var resultTable = table[sheetName];
return resultTable;
}
}
}