简体   繁体   English

如何使用 ExcelDataReader 从 C# 中的 excel 文件中逐行(按行)获取数据

[英]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.我想从 excel 文件中逐行获取数据。

Please find Image for Sample data.请查找示例数据的图像。

I want to fetch this data row wise using ExcelDataReader.我想使用 ExcelDataReader 明智地获取此数据行。 Is there a way of doing it?有没有办法做到这一点?

Sample Image ack.imgur.com/jCSnx.png示例图像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[0]将第一个工作表作为 DataTable 获取,然后循环遍历 DataTable 的行。 result.Tables is a collection of sheets inside your workbook. 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用exceldatareader从excel文件中获取数据并将其粘贴到xml文件中? - Using exceldatareader to take data from excel file and paste it in a xml file? 始终使用ExcelDataReader从Excel文件中以字符串形式获取数据 - Always Get Data as String from Excel file using ExcelDataReader 如何使用C#在Azure函数中添加ExcelDataReader和Excel数据集的依赖项 - How to add the dependencies of ExcelDataReader and Excel dataset in Azure functions using C# C# 中的 ExcelDataReader - 如何使用行和列坐标引用单个单元格 - ExcelDataReader in C# - How to reference an individual Cell using row and column cordinates 使用 ExcelDataReader 从特定单元格开始读取 Excel 数据 - Using ExcelDataReader to read Excel data starting from a particular cell 使用ExcelDataReader时如何将多个Excel工作表作为数据表传递? - How to pass multiple Excel sheets as data table when using ExcelDataReader? 如何使用C#逐行解析.xls文件? - How to parse .xls file line by line using C#? 如何在 PowerShell 中逐行使用 C# 从 URI 读取文件? - How to use read file from URI using C# in PowerShell line by line? 使用c#如何打印从解析文件中随机选择的行的行号 - using c# how to Print Line number of randomly selected line from parsed file 如何使用c#在Excel中创建点图,折线图? - How to create dot chart, line chart in excel using c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM