Java 使用Apache POI查找包含数据的最后一行

Java 使用Apache POI查找包含数据的最后一行,java,excel,apache-poi,Java,Excel,Apache Poi,我正在读取excel文件并打印到控制台(现在)。问题是我读的excel文件被几个人编辑了,为了方便起见,他们将表格向下扩展了数百行,因为它每天更新15次,Apache POI将此视为数据,结果打印了数百行。下面是我的代码。我认为if current.Row.iterator()==null将结束while循环 while (iterator.hasNext()) { Row currentRow = iterator.next(); Iterator<Cel

我正在读取excel文件并打印到控制台(现在)。问题是我读的excel文件被几个人编辑了,为了方便起见,他们将表格向下扩展了数百行,因为它每天更新15次,Apache POI将此视为数据,结果打印了数百行。下面是我的代码。我认为if current.Row.iterator()==null将结束while循环

while (iterator.hasNext()) {
        Row currentRow = iterator.next();
        Iterator<Cell> cellIterator = currentRow.iterator();

        if (currentRow.iterator() == null) { // i thought this would exit the while loop?
            return;
        }
        while (cellIterator.hasNext()) {
            Cell currentCell = cellIterator.next();

            if (currentCell.getCellTypeEnum() == CellType.STRING) { // todo: probably don't need the if/else if?
                System.out.print(currentCell.getStringCellValue() + "  ");
            } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                String currentCellAsString = dataFormatter.formatCellValue(currentCell);
                System.out.print(currentCellAsString + "  ");
            }
        }
        System.out.println();
    }
while(iterator.hasNext()){
Row currentRow=iterator.next();
迭代器cellIterator=currentRow.Iterator();
如果(currentRow.iterator()==null){//我想这会退出while循环吗?
返回;
}
while(cellIterator.hasNext()){
Cell currentCell=cellIterator.next();
if(currentCell.getCellTypeEnum()==CellType.STRING){//todo:可能不需要if/else if?
System.out.print(currentCell.getStringCellValue()+);
}else if(currentCell.getCellTypeEnum()==CellType.NUMERIC){
字符串currentCellAsString=dataFormatter.formatCellValue(currentCell);
系统输出打印(currentCellAsString+“”);
}
}
System.out.println();
}

我假设迭代器永远不会返回null,您必须依赖外部的
迭代器。hasNext
您应该更正第一个while循环本身,使其采用
Row currentRow=iterator.next()
@N00bPr0grammer对不起,你是什么意思?你试过吗?是的,我试过(Row:sheet){if(Row==null);return;}但这会在程序打印任何内容之前结束程序