简体   繁体   中英

How to fetch line by line (row wise) data from excel file in c# using ExcelDataReader

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 Image ack.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.

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM