如何使用Apache POI检查excel单元格是否为空? [英] How to check if an excel cell is empty using Apache POI?
本文介绍了如何使用Apache POI检查excel单元格是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Poi.jar 从 excel 表中获取输入,想知道如何检查单元格是否为空.
I am taking input from an excel sheet using Poi.jar and wanted to know how to check if a cell is empty or not.
现在我正在使用下面的代码.
Right now I m using the below code.
cell = myRow.getCell(3);
if (cell != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
//System.out.print(cell.getStringCellValue() + "\t\t");
if (cell.getStringCellValue() != "")
depend[p] = Integer.parseInt(cell.getStringCellValue());
}
}
推荐答案
如果您使用的是 Apache POI 4.x,您可以使用:
If you're using Apache POI 4.x, you can do that with:
Cell c = row.getCell(3);
if (c == null || c.getCellType() == CellType.Blank) {
// This cell is empty
}
对于在迁移到 CellType
枚举之前的旧 Apache POI 3.x 版本,它是:
For older Apache POI 3.x versions, which predate the move to the CellType
enum, it's:
Cell c = row.getCell(3);
if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
// This cell is empty
}
不要忘记检查 Row
是否为空 - 如果该行从未被使用过,也没有使用过或样式化过的单元格,则该行本身可能为空!
Don't forget to check if the Row
is null though - if the row has never been used with no cells ever used or styled, the row itself might be null!
这篇关于如何使用Apache POI检查excel单元格是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文