我正在尝试使用以下代码从 Excel 文件中读取列的值:
FileStream stream = File.Open("excelfile.xlsx", FileMode.Open, FileAccess.Read);
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
Console.WriteLine("Results: " +excelReader.ResultsCount);
while (excelReader.Read())
{
object[] values = new object[excelReader.FieldCount];
excelReader.GetValues(values);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
但是它引发了一个异常:
“System.NotSupportedException 未处理 Message="O método especificado não é suportado." Source="Excel" StackTrace: 在 Excel.ExcelOpenXmlReader.GetValues(Object[] values)
我正在使用另一种方式来读取值,但我想稍微清理一下代码......这是上一个读取周期:
FileStream stream = File.Open("excelfile.xlsx", FileMode.Open, FileAccess.Read);
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
Console.WriteLine("Results: " +excelReader.ResultsCount);
while (excelReader.Read())
{
Console.WriteLine("Row:"+excelReader.GetValues())
for (int i = 0; i < excelReader.FieldCount; i++ )
{
Console.Write(excelReader.GetValue(i)+ "|");
}
Console.WriteLine("");
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
我在 getValues 上做错了吗?我以前用它来阅读 SQL 记录......也许这是我正在使用的库的限制。