POI 中的CellType类型以及值的对应关系

CellType     类型    
CELL_TYPE_NUMERIC     数值 0
CELL_TYPE_STRING     字符串型 1
CELL_TYPE_FORMULA     公式型 2
CELL_TYPE_BLANK     空值 3
CELL_TYPE_BOOLEAN     布尔型 4
CELL_TYPE_ERROR     错误 5

在解析excel的时候,通常值考虑前四种情况

若想获取单元格的值,推荐以下方式:

public String getCellValue(Cell cell) {
    String temp = "";
    if (cell == null) {
      return temp;
    }
    switch (cell.getCellType()) {
      case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
      case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
          Date date = cell.getDateCellValue();
          DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
          temp = df.format(date);
        } else {
          return String.valueOf(cell.getNumericCellValue());
        }
      case Cell.CELL_TYPE_FORMULA:
        cell.setCellType(Cell.CELL_TYPE_STRING);
        return cell.getStringCellValue();

      default:
        return temp;
    }
  }

猜你喜欢

转载自blog.csdn.net/qq_28411869/article/details/84139260
poi