为什么我的代码跳过空白字段?

我使用Apache poi从excel文件中读取,但是当它涉及空单元格时,它会跳过单元格。 为什么这样? 我已经放了一个if else条件来捕捉它。 这是我的代码:

while (cellIterator.hasNext()) { cell = (XSSFCell) cellIterator.next(); if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { if(DateUtil.isCellDateFormatted(cell)) { 

CellIterator不支持遍历空单元格。 它会自动跳过这样的单元格。 要更改此行为,您需要手动迭代特定行的单元格(使用基于0的索引)。

这里介绍一个例子。

我遇到了这个问题的一个很好的解决scheme。如果你想获得所有的单元格,不pipe它们是否存在,那么迭代器不适合你。 相反,您需要手动获取适当的单元格,可能会丢失单元格策略

 for(Row row : sheet) { for(int cn=0; cn<row.getLastCellNum(); cn++) { // If the cell is missing from the file, generate a blank one // (Works by specifying a MissingCellPolicy) Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK); // Print the cell for debugging`enter code here` System.out.println("CELL: " + cn + " --> " + cell.toString()); } } 

有用。 这将所有缺less的行转换为空白,并且CELL_TYPE_BLANK将捕获它。