如何使用ApachePOI停止Java中的迭代
我正在使用ApachePOI阅读Java中的一个特定excel模板。底部有一些不需要的字段(如小指南),我需要跳过。因此,每当POI发现一行完全为空时,我想停止迭代,因此底部的字段将被自动跳过如何使用ApachePOI停止Java中的迭代,java,excel,apache-poi,Java,Excel,Apache Poi,我正在使用ApachePOI阅读Java中的一个特定excel模板。底部有一些不需要的字段(如小指南),我需要跳过。因此,每当POI发现一行完全为空时,我想停止迭代,因此底部的字段将被自动跳过 boolean isEmptyRow = false; int cellCount = 0; int nullCount = 0; Iterator<Cell> cellIterator = row.iterat
boolean isEmptyRow = false;
int cellCount = 0;
int nullCount = 0;
Iterator<Cell> cellIterator = row.iterator();
while (cellIterator.hasNext()) {
cellCount++;
Cell cell = (Cell) cellIterator.next();
if ((cell == null)
|| ((cell != null) && (cell.getCellType() == Cell.CELL_TYPE_BLANK))) {
nullCount++;
}
}
if ((cellCount != 0) && (nullCount != 0)
&& (cellCount == nullCount)) { //If nullCount & cellCouont are same, Row is empty
isEmptyRow = true;
}
我找到了许多处理空白单元格的方法,但我的要求是完整的一行。下面的代码将遍历一行的单元格。它将为遇到的每个单元格保留计数(cellCount)。另一个coont(nullCount)仅在单元格为空时递增。如果这两个计数相等,则行为空
boolean isEmptyRow = false;
int cellCount = 0;
int nullCount = 0;
Iterator<Cell> cellIterator = row.iterator();
while (cellIterator.hasNext()) {
cellCount++;
Cell cell = (Cell) cellIterator.next();
if ((cell == null)
|| ((cell != null) && (cell.getCellType() == Cell.CELL_TYPE_BLANK))) {
nullCount++;
}
}
if ((cellCount != 0) && (nullCount != 0)
&& (cellCount == nullCount)) { //If nullCount & cellCouont are same, Row is empty
isEmptyRow = true;
}
布尔isEmptyRow=false;
int-cellCount=0;
int nullCount=0;
迭代器cellIterator=row.Iterator();
while(cellIterator.hasNext()){
cellCount++;
Cell Cell=(Cell)cellIterator.next();
if((单元格==null)
||((cell!=null)&&(cell.getCellType()==cell.cell\u TYPE\u BLANK))){
nullCount++;
}
}
if((cellCount!=0)和&(nullCount!=0)
&&(cellCount==nullCount)){//如果nullCount和cellCount相同,则行为空
isEmptyRow=真;
}
ApachePOI支持哪一种?我使用的方法与Renju在回答中显示的方法相同。虽然我没有想到要救伯爵。他的代码有效。谢谢你,不过我还有一个疑问。虽然这对我来说是可行的,但现在它也在第一行,这是空白的。意味着它在遇到空白行后停止。是否可以在此之前停止?您想跳过工作表的第一行,该行在此检查中为空?/或者您正在讨论如何避免读取第一行空行?不。就像每当出现空白行时,它都会将该行带入记录并从循环中退出。不,这将很困难。我们无法识别即将出现的行是否为空,除非我们阅读它