Apache POI解析并处理空单元格

时间:2016-12-07 00:51:20

标签: java excel csv apache-poi

我正在使用java servlet来解析上传的excel文件,

我得到文件并输入流并制作工作簿,然后我想循环所有单元格,包括空单元格,现在这就是我正在做的,这处理数据中间的空单元格,但是如果有一行完全为空,那么在它失败之后会有一行数据

for (int i = 0; i < mySheet.getPhysicalNumberOfRows(); i++) {
    Row row = mySheet.getRow(i);
    for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
          Cell cell = row.getCell(j);
    }
}

所以这些是我的行

item item item
               //this row is empty
item item item

失败了怎么办?

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

如果行fectching并返回null,则该行的文件中没有存储数据 - 它完全空白。

for (int i = 0; i < mySheet.getPhysicalNumberOfRows(); i++) {
    Row row = mySheet.getRow(i);
    for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
           Cell cell = row.getCell(c);
        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK)
        .....
    }
}

并查看poi示例POI Example