0

I want to fetch data row wise from excel file.

Please find Image for Sample data.

I want to fetch this data row wise using ExcelDataReader. Is there a way of doing it?

Sample Imageack.imgur.com/jCSnx.png

using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
{
    DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
    {
        ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
        {
            UseHeaderRow = true
        }
    });
}

I am trying this code to fetch data. But it gives me 1st row as column header and then prints all the data same as it is.

Dumisani
  • 2,988
  • 1
  • 29
  • 40

1 Answers1

0

You can get the first sheet as a DataTable by using result.Tables[0] then loop through the DataTable's rows. result.Tables is a collection of sheets inside your workbook.

using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
{
    DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
    {
        ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
        {
            UseHeaderRow = true
        }
    });

    DataTable dataTable = result.Tables[0];
    foreach(var row in dataTable.Rows)
    {
        //Your logic
    }
}
Dumisani
  • 2,988
  • 1
  • 29
  • 40