C# read Excel table

1. Nuget installation package ExcelDataReader

2. Use the following code to get the result

string excelPath="Excel表地址";
using (var stream = File.Open(excelPath, FileMode.Open, FileAccess.Read))
{
      using (var reader = ExcelReaderFactory.CreateReader(stream))
      {
              do
              {
                    while (reader.Read())
                    {
                    }
              } while (reader.NextResult());

              var configuration = new ExcelDataSetConfiguration { ConfigureDataTable = tableReader => new ExcelDataTableConfiguration { UseHeaderRow = true } };
              var result = reader.AsDataSet(configuration);
      }
}

Three, result analysis

result is an object of type DataSet, we can use result.Tables[subscript] or result.Tables[table name] to access a table in the Excel table.

make

DataTable table1=result.Tables[0];

Then we can use the Rows attribute of table1 to access each row in the table, and each row has a Column attribute to access a column of the row

Guess you like

Origin blog.csdn.net/qq_36694133/article/details/128104246