[英]get excel sheet row id using exceldatareader into dataset
I have chosen the ExcelDataReader Library for Reading the.XLS and.XLSX files using C#.我选择了 ExcelDataReader 库来使用 C# 读取 .XLS 和 .XLSX 文件。 ExcelDataReader is perfectly working with both file formats with my local and deployment server environment.
ExcelDataReader 在我的本地和部署服务器环境中完美地处理这两种文件格式。
I'm facing issue, how to get all the row id from given Excel files?我遇到了问题,如何从给定的 Excel 文件中获取所有行 ID?
INPUT Excel file:输入 Excel 文件:
And i want output in dataset format like that我想要像这样的数据集格式的 output
Lets explain by code让我们通过代码来解释
using (var stream = System.IO.File.Open(Server.MapPath("yourExcelfileName.xlsx"), FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelDataReader.ExcelReaderFactory.CreateReader(stream))
{
var conf = new ExcelDataSetConfiguration()
{
ConfigureDataTable = a => new ExcelDataTableConfiguration
{
UseHeaderRow = true
}
};
var dataSet = reader.AsDataSet(conf);
var sheet = dataSet.Tables["sheetName"].Rows.Cast<DataRow>(); // instead of sheetName you can use Index of it like 0 ,1 , ...
foreach (var row in sheet)
{
// some code
var rowId = row.Table.Rows.IndexOf(row);
var rowValueByHeaderFieldName = row["HeaderFieldName"]; // you can also use index instead of HeaderFieldName like row[1] , ...
// some code
}
}
}
Hope this help;)希望这有帮助;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.