I have the same problem, it's not due to Apache POI, but it's an editing with LibreOffice ! When you delete a row with LibreOffice, the last row index is set to max rows available (1048575).
If you use Excel, you don't have the problem ...
For the moment, I don't have the solution ...
In my case, I have exclude my check of number of row when the getLastRowNum() return 1048575 ... (I suppose the file is editing with LibreOffice and my check is not possible !).