Java的poi的excel导入怎么判断日期格式的单元格

场景


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
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用POICellValueFormatter类来获取Excel单元格的数值格式。具体的操作如下: 1. 首先,使用POI读取Excel文件并获取一个单元格对象; 2. 然后,使用这个单元格对象的getCellType方法获取该单元格的数据类型; 3. 倘若该单元格的数据类型为数值型的话,可以使用POIDateUtil类来判断单元格的数值格式,并进行相应的处理。 例如,以下是一个使用POI获取Excel单元格数值格式的示例代码: ```java // 导入POI相关的库 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.CellValue.CellTypeEnum; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormatter; public class Example { public static void main(String[] args) throws Exception { // 读取Excel文件 Workbook workbook = WorkbookFactory.create(new File("example.xlsx")); Sheet sheet = workbook.getSheetAt(0); // 获取某个单元格 Cell cell = sheet.getRow(0).getCell(0); // 判断单元格的数据类型 if (cell.getCellType() == CellType.NUMERIC) { // 判断单元格的数值格式 if (DateUtil.isCellDateFormatted(cell)) { // 处理日期型数据 Date date = cell.getDateCellValue(); String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date); System.out.println(formattedDate); } else { // 处理普通数值型数据 Double value = cell.getNumericCellValue(); System.out.println(value); } } else { // 处理其他类型的数据 DataFormatter formatter = new DataFormatter(); String text = formatter.formatCellValue(cell); System.out.println(text); } } } ``` 以上代码使用POI读取了一个Excel文件中的单元格,并根据该单元格的数据类型和数值格式进行相应的处理。具体可以根据实际需要进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值