I need to read and write data from an Excel spreadsheet. Is there a method to finding out how many rows/columns a certain worksheet has using ExcelPackage? I have the following
I was using sheet.UsedRange on its own but noticed that some cells at the end of the range were blank but still included in the range.
This works, however for efficiency you might be better staring from the last row in the range and counting back to see where your data ends (as opposed to this snippet which starts from the first row!)
int count = 0;
E.Range excelRange = sheet.UsedRange;
object[,] valueArray = (object[,])excelRange.get_Value(E.XlRangeValueDataType.xlRangeValueDefault);
if (valueArray.GetUpperBound(0) > 1)
{
for (int i = 0; i < valueArray.GetUpperBound(0) + 2; i++)
{
if (valueArray[i + 2, 1] == null)
break;
else
count++;
}
}