We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

com.aspose.cells.Cells Cells.getRows().getCount() not give the total number of row in a excel worksheet

my Excel worksheet just has six rows, but I use

cells.getRows().getCount(),

it return 9, why?

@tong123123,

Please note, RowCollection/ColumnCollection.getCount() would give you total number of rows/columns which are initialized, so it won’t be always reliable. We think you should use Cells.getMaxRow/getMaxDataRow and Cells.getMaxColumn/getMaxDataColumns methods to get the farthest (last) row/column indices, see the sample code segment for your reference:
e.g
Sample code:

 Workbook workbook = new Workbook("Book1.xls", new LoadOptions());
 //Get the farthest (last) row's index (zero-based) which contains data.
 int lastRowIndex = workbook.getWorksheets().get(0).getCells().getMaxDataRow();
 //Get the farthest (last) column's index (zero-based) which contains data.
 int lastColIndex = workbook.getWorksheets().get(0).getCells().getMaxDataColumn();

Let us know if you still have any issue or confusion.

Then when to use RowCollection/ColumnCollection.getCount()?

@tong123123,

Well, RowCollection/ColumnCollection.getCount() would give you rows/cols count which are initialized in the worksheet, so it may include cells even if they are empty or with no values.

then for my initial question, why 9 rows is initialized? what is the logic behind for how many rows/columns in a worksheet is initialized?

@tong123123,

Sometimes the rows/columns are instantiated when we set (extend or decrease) rows height and columns’ width in the worksheet a bit or we have used them or we apply some sort of formatting to them. So, even the cells in those rows/columns are null/empty but they are initialized in the worksheet would add to getCount() method.

Please note, if you need to get displayed range of cells having data in the worksheet, you should only use Cells.getMaxRow/getMaxDataRow and Cells.getMaxColumn/getMaxDataColumns methods.