【问题标题】:How to fetch line by line (row wise) data from excel file in c# using ExcelDataReader如何使用 ExcelDataReader 从 c# 中的 excel 文件中逐行(逐行)获取数据
【发布时间】:2020-02-13 13:22:53
【问题描述】:

我想从 excel 文件中逐行获取数据。

请查找示例数据的图像。

我想使用 ExcelDataReader 逐行获取此数据。 有办法吗?

Sample Imageack.imgur.com/jCSnx.png

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

我正在尝试使用此代码来获取数据。但它给了我第一行作为列标题,然后打印所有数据。

【问题讨论】:

  • 有没有试过的代码?
  • 是的,我已经更新了问题

标签: c# exceldatareader


【解决方案1】:

您可以使用result.Tables[0] 将第一个工作表作为DataTable 获取,然后循环遍历DataTable 的行。 result.Tables 是工作簿中的工作表集合。

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

【讨论】:

  • 谢谢杜米萨尼 :)
  • 不客气@KushagraVerma。如果解决了您的问题,请标记为答案,谢谢。
猜你喜欢
  • 2014-09-19
  • 2020-08-26
  • 2018-05-29
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 2021-08-24
  • 2023-01-08
  • 2018-11-25
相关资源
最近更新 更多