Edit
Based on the answers below, the error I am experiencing may or may not lead to my inability to read my excel file. That is, I am not getting data from a string worksheet.Cells[row,col].Valuein my for loop below.
Problem
I am trying to return a DataTable with information from an excel file. In particular, this is an xlsx file from 2013, I believe. See code below:
private DataTable ImportToDataTable(string Path)
{
DataTable dt = new DataTable();
FileInfo fi = new FileInfo(Path);
if(!fi.Exists)
{
throw new Exception("File " + Path + " Does not exist.");
}
using (ExcelPackage xlPackage = new ExcelPackage(fi))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.First();
ExcelCellAddress startCell = worksheet.Dimension.Start;
ExcelCellAddress endCell = worksheet.Dimension.End;
for(int col = startCell.Column; col <= endCell.Column; col++)
{
dt.Columns.Add(col.ToString());
}
for(int row = startCell.Row; row <= endCell.Row; row++)
{
DataRow dr = dt.NewRow();
int i = 0;
for(int col = startCell.Column; col <= endCell.Column; col++)
{
dr[i++] = worksheet.Cells[row, col].Value.ToString();
}
dt.Rows.Add(dr);
}
}
return dt;
}
Error
Everything is strange here. I see the correct meaning in startCelland endCell. However, when I look worksheet, I look under Cells, and I see something that I do not understand:
worksheet.Cells.Current' threw an exception of type 'System.NullReferenceException
Attempts
- Reformatting my excel using shared fields.
- Make sure there is no field in my excel.
- RTFM'ed epplus documentation. Nothing suggests this error.
- EPPlus stackoverflow. .
, , ? - ? - epplus? , 2013 xlsx eeplus, excel . - , , . . , .