I am trying to read the values of a column from an Excel file using this code:
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();
Howver it trows an exception:
"System.NotSupportedException was unhandled Message="O método especificado não é suportado." Source="Excel" StackTrace: in Excel.ExcelOpenXmlReader.GetValues(Object[] values)
I was using another way to read the values, but i wanted to clean up the code a bit... This was the previous read cycle:
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();
Am I doing something wrong with getValues? I have used that before to read SQL Records... Perhaps it's a limitation of the library that i am using.