Java – find the last row in an Excel spreadsheet

I'm using Apache's Java POI to find the index of the last row in an Excel spreadsheet

I think it should be possible to getlastrownum() or getphysicalnumberofrows(), but they don't seem to give the correct results For example, I have a spreadsheet row, and the return value of these two functions is 1140 The other two rows of the spreadsheet have a value of 1162

Another problem is that I can't just look for the first empty row, because there may be empty rows between valid data rows

Is there any way to find the index of the last row? I think I can ask for no blank lines between data, but I hope to have a better solution

Edit: not helpful for records using iterators It just iterates over 1140 / 1162 lines

Solution

I use poi-3.6-20091214 to get the expected output, a test Xls has two empty rows followed by three occupied rows:

InputStream myxls = new FileInputStream("test.xls");
Workbook book = new HSSFWorkbook(myxls);
Sheet sheet = book.getSheetAt(0);
System.out.println(sheet.getLastRowNum());

Output: 4

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>