0

I try to get last row index in Excel file using apache poi but getLastRowNum() always returns wrong number of rows and the number is always 1048575 .

My xlsx is simple. It has only three rows with content.

Here is the relevant code snippet:

Sheet sheet = workbook.getSheetAt(0);
            int rowNum = sheet.getLastRowNum();
            log.info("LastRowNum " + rowNum);

Apache POI version:

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.2</version>
        </dependency>       

OS: "Arch Linux"

JAVA Version:

openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment Temurin-17.0.5+8 (build 17.0.5+8)
OpenJDK 64-Bit Server VM Temurin-17.0.5+8 (build 17.0.5+8, mixed mode, sharing)

I try upgrading apache poi but this doesn't fix the things.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • 2
    I've never used that tool, but I've faced a similar problem in the past and it turned out the spreadsheet actually had a few thousand empty rows. – Álvaro González Dec 19 '22 at 11:57
  • https://www.wallstreetmojo.com/how-many-rows-and-columns-in-excel/ – xerx593 Dec 19 '22 at 13:42
  • with "wrong number" you probably mean to "get the last row num of non empty row", for which there seems no better solution than: iterating (from 1048575 backwards;( https://stackoverflow.com/a/45230522/592355 + https://stackoverflow.com/q/12217047/592355 – xerx593 Dec 19 '22 at 14:33

0 Answers0