查找 Excel 电子表格中的最后一行 [英] Finding the last row in an Excel spreadsheet
问题描述
我正在尝试使用 Apache 的 Java POI 在 Excel 电子表格中查找最后一行的索引.
I'm trying to find the index of the last row in an excel spreadsheet using Apache's POI for Java.
我认为使用 getLastRowNum()
或 getPhysicalNumberOfRows()
应该可以做到这一点,但它们似乎没有给出正确的结果.例如,我有一个单行电子表格,这两个函数返回的值是 1140.另外两行电子表格的值是 1162.
I thought this should be possible with getLastRowNum()
or getPhysicalNumberOfRows()
but they don't seem to give the right results. For example, I have a one line spreadsheet and these two functions return a value of 1140. Another two line spreadsheets gets a value of 1162.
另一个问题是我不能只查找第一个空行,因为在有效数据行之间可能有空行.
The other problem is that I cannot just look for the first empty row, since it may be possible to have empty rows between rows of valid data.
那么有没有办法找到最后一行的索引?我想我可以要求数据之间没有空行,但我希望有更好的解决方案.
So is there a way to find the index of the last row? I suppose I could make it a requirement to not have empty rows between data, but I was hoping for a better solution.
对于使用迭代器的记录没有帮助.它只是迭代了 1140/1162 假定的行.
For the record using an iterator didn't help. It just iterated over the 1140/1162 supposed rows.
推荐答案
我使用 poi-3.6-20091214 和一个 test.xls
得到预期的输出,其中有两个空行和三个被占用的行:
I get the expected output using poi-3.6-20091214 and a test.xls
having 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());
输出:4
这篇关于查找 Excel 电子表格中的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!