- 当我们在读取excel表格数据时,经常会遇到单元格的格式问题,不同的单元格格式取值方式也不同.获取到单元格之后首先要对单元格格式进行判断:
//获取cell的类型
int type = cell.getCellType();
public enum CellType {
@Internal(
since = "POI 3.15 beta 3"
)
_NONE(-1),
NUMERIC(0), //数值型
STRING(1), //字符串型
FORMULA(2), //公式型
BLANK(3), //空值
BOOLEAN(4), //布尔型
ERROR(5); //错误
cell.setCellType(CellType.STRING);
cell.getStringCellValue().trim();
- 如图所示: 当单元格是日期格式时:

//获取日期
String date = new SimpleDateFormat("yyyy-MM").format(cell.getDateCellValue());
HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
String number = dataFormatter.formatCellValue(cell);