使用java导入Excel时需要对单元格的格式进行判断:
int type = cell.getCellType(); //获取cell的类型
CellType是一个枚举类,主要有以下类型:
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();
对时间格式的判断要多注意,分享一下我的方法:
if(cell.getCellTypeEnum() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)){
XxxBean.setXxxTime(cell.getDateCellValue());
}else{
XXXXXXX;
}
如果还需要对时间格式进行转换,可以这样:
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);
sdf.format(cell.getDateCellValue());
参考文章:
https://blog.csdn.net/weixin_42256811/article/details/81949588
https://blog.csdn.net/f1204270443/article/details/54019754
https://jingyan.baidu.com/article/456c463b3629620a583144fb.html
https://blog.csdn.net/Master_Shifu_/article/details/87439893