poi仅可以识别文本、数字等类型,无法对自定义时间格式yyyy-MM-dd hh:mm:ss 进行具体的识别。
在仅需要读取年月日的情况下(yyyy-MM-dd): poi读取到到的单元格格式为 Cell.CELL_TYPE_NUMERIC: //数字、日期
随后会根据DateUtil.isCellDateFormatted(cell)的返回值判断是日期还是数字。
如果是日期则 cellValue = fmt.format(cell.getDateCellValue()); //日期型
如果是数字则 cellValue = String.valueOf(cell.getNumericCellValue()); //数字 即可返回正确的日期。
具体代码如下:
switch(cellType) {
case Cell.CELL_TYPE_STRING: //文本
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC: //数字、日期
if(DateUtil.isCellDateFormatted(cell)) {
cellValue = fmt.format(cell.getDateCellValue()); //日期型
} else {
cellValue = String.valueOf(cell.getNumericCellValue()); //数字
}
break;
}
但是,当我们需要yyyy-MM-dd hh:mm:ss 格式的时候,则不能满足需求。
当cellType是数字、日期的时候(即为0时)&#x