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;
}
}