Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExcelDataReader.AsDataSet not working

I'm using ExcelDataReader v.2.1. library to read both xls and xlsx files in my C# project. This way:

FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader;

string extension = Path.GetExtension(filePath);

if (extension == ".xls")
{
    excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (extension == ".xlsx")
{
    excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
else
{
    throw new NotSupportedException("Wrong file extension");
}

return excelReader;

Then, I use "AsDataSet" method to get a filled DataSet:

_dataSet = GetDataReader(_options.Filepath).AsDataSet();

It works fine most of the times, but, with some XLSX files, it only reads the first column. I've been looking at excelReader instance with a Watch and I saw that it actually gets all the values of the Excel, but then, when using the AsDataSet method, it only reads the first column.

Do you know what can be going wrong here? Do you think it could be a "AsDataSet" method bug?

like image 822
Mry Avatar asked Sep 29 '15 12:09

Mry


People also ask

What is Excel data reader?

ExcelDataReader is an open source lightweight API written in C# for reading Microsoft Excel Files. Using the API you can read Microsoft XLS, XLSX, and CSV easily. The API supports older versions of XLS files back to Excel 2.0, supports text dates, cached formula values, and empty sheet paths in XLSX.


2 Answers

install ExcelDataReader.DataSet via nugget

like image 82
Jicking Avatar answered Sep 18 '22 11:09

Jicking


Install-Package ExcelDataReader.DataSet -Version 3.6.0

Install DataSet to work with AsDataSet

like image 22
Huy Avatar answered Sep 16 '22 11:09

Huy