在C#中使用ExcelDataReader逐行读取Excel文件中的数据,可以按照以下步骤进行:
using ExcelDataReader;
using System.Data;
using System.IO;
public void ReadExcelFile(string filePath)
{
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
// 读取整个Excel文件
DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
});
// 获取第一个工作表
DataTable table = result.Tables[0];
// 逐行读取数据
foreach (DataRow row in table.Rows)
{
// 处理每一行的数据
for (int i = 0; i < table.Columns.Count; i++)
{
var cellValue = row[i].ToString();
// 在这里可以对每个单元格的值进行处理或存储
Console.WriteLine(cellValue);
}
}
}
}
}
string filePath = "path_to_your_excel_file.xlsx";
ReadExcelFile(filePath);
这样,你就可以使用ExcelDataReader逐行读取Excel文件中的数据了。请注意,上述代码仅适用于读取第一个工作表的数据,如果需要读取其他工作表,可以根据需要进行修改。
推荐的腾讯云相关产品:腾讯云对象存储(COS),用于存储和管理Excel文件。产品介绍链接地址:https://cloud.tencent.com/product/cos
领取专属 10元无门槛券
手把手带您无忧上云