我正在使用 apache poi 来解析 Excel 电子表格。根据poi iterator guide ,行迭代器将跳过空行。我想迭代所有行,无论是否为空。所以我改用了 for 循环。
Row row0 = sheet.getRow(0);
for (Row row : sheet)
{
rowIndex ++;
Cell cell = row.getCell(0);
if (cell != null) {
System.out.println(rowIndex);
}
}
当我在 Eclipse 中调试它时,对于以下电子表格,row0 为空,但是 for 循环的第一次迭代中的行不为空。输出(第一个非空单元格的行索引)应该是 2,但实际上打印的是 1。
为什么 for 循环会跳过第一个空行?
最佳答案
For 循环(在本例中为 for-each 循环)使用迭代器(请参阅 Iterable 接口(interface) https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html )来迭代行,因此正如您所写,它将跳过空值。
在迭代中包含空值的方法是使用传统的 for 循环(请参阅 http://poi.apache.org/spreadsheet/quick-guide.html#Iterator 中的迭代单元格,控制缺失/空白单元格部分)。
关于java - 如何迭代电子表格中的所有行(包括空行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34625682/