I am working in converting ths xls and xlsx to datatable in c#. I have used this code.
public DataTable ReadDataExcel(string filepath)
{
FileStream stream = File.Open(filepath, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
DataTable dt = new DataTable();
dt = result.Tables[0];
return dt;
}
In case of xls, its working fine. Whenever i used xlsx its not working. Its giving 'Object Reference' error.
Whether there are any other way to convert both the xls and xlsx to datatable. I am not interested in using 'Microsoft.Jet.OLEDB'....
This is the answer.
For importing xlsx,
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
For importing xls,
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With