如何确定空行?
2022-09-01 01:49:24
如何使用Apache POI确定.xls文档中的空行?
我在我的POI项目中使用以下方法,它运行良好。它是zeller解决方案的变体。
public static boolean isRowEmpty(Row row) {
for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK)
return false;
}
return true;
}
行迭代器仅返回包含数据的行,但如果它们完全为空,则按行索引迭代,返回getRow(index)
null
溶液:
直到POI版本3.14(感谢Sergii Lisnychyi):
private boolean checkIfRowIsEmpty(Row row) {
if (row == null) {
return true;
}
if (row.getLastCellNum() <= 0) {
return true;
}
for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK && StringUtils.isNotBlank(cell.toString())) {
return false;
}
}
return true;
}
从 POI 版本 3.15 到 4.2(已弃用):int getCellType()
private boolean checkIfRowIsEmpty(Row row) {
if (row == null) {
return true;
}
if (row.getLastCellNum() <= 0) {
return true;
}
for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell != null && cell.getCellTypeEnum() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
return false;
}
}
return true;
}
从 POI 版本 4 (将返回枚举而不是 int):CellTypeEnum getCellTypeEnum()
private boolean checkIfRowIsEmpty(Row row) {
if (row == null) {
return true;
}
if (row.getLastCellNum() <= 0) {
return true;
}
for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell != null && cell.getCellTypeEnum() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
return false;
}
}
return true;
}
从 POI 版本 5.1 (CellTypeEnum getCellTypeEnum() 重命名为 getCellType()):
private boolean checkIfRowIsEmpty(Row row) {
if (row == null) {
return true;
}
if (row.getLastCellNum() <= 0) {
return true;
}
for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell != null && cell.getCellType() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
return false;
}
}
return true;
}