gpt4 book ai didi

c# - 如何使用 getValues 从 Excel 中获取值?

转载 作者:行者123 更新时间:2023-11-30 20:57:39 25 4
gpt4 key购买 nike

我正在尝试使用以下代码从 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 was unhandled Message="O método especificado não é suportado." Source="Excel" StackTrace: in 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 记录...也许这是我正在使用的库的限制。

最佳答案

这确实是一个实现问题。希望有足够多的人会发现此功能对 Ian 有用,可以在未来的版本中实现它。

this particular method of IDataReader is not currently supported by ExcelDataReader. I'll leave this issue open though so we can see if other people want it as well

--Excel Reader的Ian1971开发者

似乎有一个实现问题,所以我将使用第一种方法。

关于c# - 如何使用 getValues 从 Excel 中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16656083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com