我需要讀取.xlsx文件而不使用第三方庫。移到右邊一行讀取c#中的Excel文件
我做了這種方式:
private void Upload(string filename)
{
FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read);
// Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//DataSet - The result of each spreadsheet will be created in the result.Tables
excelReader.IsFirstRowAsColumnNames = false;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
string value = GetValue(0, 0, excelReader);
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
}
我不知道如何在正確的單元讀取。問題不列位置(I可以使用,但該行位置
public string GetValue(int row, int col, IExcelDataReader excelReader)
{
string s;
// ??? how to positionate on the right row?
s = excelReader(column_value);
return s;
}
我已經找到了正確的解決方案,在這個討論。http://stackoverflow.com/questions/33389393/reading-excel-in-c-sharp-where-some -columns-are-empty – Gioce90