Free Support Forum - aspose.com

Row count seems off by 1

Here is the code we use to load a sheet into a datatable

DataTable storesDataTable = null;
Worksheet sheet = null;
using (MemoryStream memoryStream = new MemoryStream(file))
{
Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook();
excel.Open(memoryStream);
sheet = excel.Worksheets[0];
}

storesDataTable = new DataTable();
storesDataTable = sheet.Cells.ExportDataTable(0, 0, sheet.Cells.Rows.Count, (sheet.Cells.MaxColumn - sheet.Cells.MinColumn) + 1);

When we use this to create a data table on the attached excel doc, it gives a misread on the sheet.Cells.Rows.Count property, which causes the resultant data table to be missing the last row of data.

Most other files seem to work just fine - I can also fake this one out by putting the letter 'x' in column A for the rows that don't have any data (row 3, 5 and 6) and that causes the count to be 9, making me think that it's skipping one of the rows when it determines the count property.

Thanks,
Todd

Hi Todd,

Please ignore Rows.Count property. Please use Cells.MaxDataRow method to get the max data row index.

Warren:

Hi Todd,

Please ignore Rows.Count property. Please use Cells.MaxDataRow method to get the max data row index.

Actually, Cells.MaxDataRow is returning the same value as Cells.Rows.Count - one less than it really should be (8 instead of 9) - is there something wrong with this document? Here's another sheet that exhibits the same behavior (though this one reads as 9 rows instead of 10). They both came from the same source.

Hi,

Sorry for my mistake.

Cells.MaxDataRow returns max data row index(zero-based),not the row count. If you want to get the count of the rows, please use Cells.MaxDataRow + 1.