【问题标题】:Apache POI parse and handle empty cellsApache POI 解析和处理空单元格
【发布时间】:2016-12-07 00:51:19
【问题描述】:

我正在使用 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

失败了怎么办?

感谢您的帮助

【问题讨论】:

  • 你能不能不使用getFirstRowNumgetLastRowNum(列相同)
  • @ScaryWombat 这对我有帮助,谢谢!

标签: java excel csv apache-poi


【解决方案1】:

如果行 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多