使用数据查找 Excelsheet 中的最后一行 - Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7906498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find last row in Excelsheet with data - Java
提问by name_masked
All,
全部,
How can I find the last row in an excelsheet with data populated in the cell. i.e.
如何在单元格中填充数据的 Excel 表格中找到最后一行。IE
| C1 | C2 | C3 | C4 |
---------------------------
R1 | 1 2 3 4
R2 | 5 6 7 8
R3 |
R4 | 9
So in the example above, I wish to retrieve the total count of rows as 4 since Row number 4 has something populated.
因此,在上面的示例中,我希望将总行数检索为 4,因为第 4 行已填充了一些内容。
Is there any such library function available? Also, what is the best Java library for handling Microsoft Office document operations?
有没有这样的库函数可用?另外,处理 Microsoft Office 文档操作的最佳 Java 库是什么?
回答by JMax
With Apache POI, you can use getLastRowNum()
.
使用 Apache POI,您可以使用getLastRowNum()
.
Something like this example:
像这样的例子:
InputStream myxls = new FileInputStream("test.xls");
Workbook book = new HSSFWorkbook(myxls);
Sheet sheet = book.getSheetAt(0);
System.out.println(sheet.getLastRowNum());
From this SO thread: Finding the last row in an Excel spreadsheet
来自这个 SO 线程:在 Excel 电子表格中查找最后一行
回答by thait84
The most common Java library for dealing with Excel documents is Apache POI.
用于处理 Excel 文档的最常见的 Java 库是 Apache POI。
They will have functions to iterate rows in Excel like what you need. Check out the examples, they are extremely straightforward.
他们将具有像您需要的那样在 Excel 中迭代行的函数。查看示例,它们非常简单。
Once you get it working, use your HSSFSheet object and call: http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html#getLastRowNum%28%29
一旦你开始工作,使用你的 HSSFSheet 对象并调用:http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html#getLastRowNum%28%29
One you have the LastRowNum, then iterate through all the rows and then if the row contains a value, you want to say that it is the last row. If the only thing you need to do is fine the last row, you might want to start from the LastRowNum and work your way backwards.
一个你有 LastRowNum,然后遍历所有行,然后如果该行包含一个值,你想说它是最后一行。如果您唯一需要做的就是最后一行没问题,您可能希望从 LastRowNum 开始并倒退。