今天研究easyexcel,导入依赖后,发现之前的导入excel数据代码出现错误,判断单元格为空的方法爆红了。原来是easyexcel用的高板本poi jar包覆盖了旧的poi版本。而以前的一些方法被废除了。
版本4.0以上废除以下判断单元格为空的方法
if(cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
nullCellNum ++;
}
新的判断空单元格方法如下:
if (null == cell || cell.getCellType().equals(CellType.BLANK)) {
nullCellNum ++;
continue;
}
附判断excel空行的方法(整行都为空时,停止遍历取值)
//判断row是否为空
public static boolean isRowEmpty(Row row) {
if (null == row) {
return true;
}
int firstCellNum = row.getFirstCellNum(); //第一个列位置
int lastCellNum = row.getLastCellNum(); //最后一列位置
int nullCellNum = 0; //空列数量
for (int c = firstCellNum; c < lastCellNum; c++) {
Cell cell = row.getCell(c);
if (null == cell || cell.getCellType().equals(CellType.BLANK)) {
nullCellNum ++;
continue;
}
cell.setCellType(CellType.STRING);
String cellValue = cell.getStringCellValue().trim();
if (StringUtils.isEmpty(cellValue)) {
nullCellNum ++;
}
}
//所有列都为空
if (nullCellNum == (lastCellNum - firstCellNum)) {
return true;
}
return false;
}