Search code examples
javaexcelapache-poipoi-hssf

How to get row count in an Excel file using POI library?


I'm using the Apache POI 3.9 library to work with Excel files. I know of the getLastRowNum() function, which returns a number of rows in an Excel file.

The only problem is getLastRowNum() returns a number with the count starting from 0.

So, if an Excel file uses the first 3 rows, getLastRowNum() returns 2. If an Excel file has just 1 row, getLastRowNum() returns 0.

The problem occurs when the Excel file is completely empty. getLastRowNum() still returns 0, so I cannot determine if the Excel file has 1 row of data, or if its empty.

How can I detect if an Excel file is empty or not?


Solution

  • Try Sheet.getPhysicalNumberOfRows()