场景
SpringBoot中使用POI实现Excel导入到数据库(图文教程已实践):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/88660466
需求
在进行导入时,在导入数据库之前需要进行时间格式的验证,确保不会乱输入数据。
实现
//日期格式加校验
Cell deliveryTimeCell = row2.getCell(3);
if(deliveryTimeCell!=null){
//如果是数值类型
if(deliveryTimeCell.getCellType()==0){
if(HSSFDateUtil.isCellDateFormatted(deliveryTimeCell)){
//获取送货日期
Date deliveryTime =deliveryTimeCell.getDateCellValue();
receiveOrder.setDeliveryTime(deliveryTime);
}else{
//设置送货时间为红色
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
deliveryTimeCell.setCellStyle(style);
isValidatePass=false;
}
}else{
//设置送货时间为红色
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
deliveryTimeCell.setCellStyle(style);
isValidatePass=false;
}
}
注:
deliveryTimeCell.getCellType()==0是因为0代表是数值类型。
而数值类型又包括时间和数字。
NUMERIC | 数值型 | 0 |
STRING | 字符串型 | 1 |
FORMULA | 公式型 | 2 |
BLANK | 空值 | 3 |
BOOLEAN | 布尔型 | 4 |
ERROR | 错误 | 5 |