springboot easyexcel导入/导出excel自定义时间格式
创始人
2024-11-12 08:10:55
0

        当使用java的easyExcel进行excel导入和导出时,如何接收表格中的各种自定义时间格式字符串转成LocalDateTime及导出时如何将LocalDateTime转为各种时间格式字符串。

一、新建自定义时间格式转换

        

package com.logwing.wyadmin.framework.excel;  import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import org.apache.poi.ss.usermodel.DateUtil;  import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; import java.util.Date;  public class LocalDateTimeConverter implements Converter {      @Override     public Class supportJavaTypeKey() {         return LocalDateTime.class;     }      @Override     public CellDataTypeEnum supportExcelTypeKey() {         return CellDataTypeEnum.STRING;     }      @Override     public LocalDateTime convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {         if(null==cellData) {             return null;         }         LocalDateTime result=null;         if(cellData.getType()==CellDataTypeEnum.NUMBER) {             if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {                 Date date= DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),                         globalConfiguration.getUse1904windowing(), null);                 result =date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();             } else {                 Date date=  DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),                         contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null);                 result =date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();             }         }if(cellData.getType()==CellDataTypeEnum.STRING) {             String value=cellData.getStringValue();             if(value.contains("-")) {                 DateTimeFormatter formatter = new DateTimeFormatterBuilder()                         .appendPattern("yyyy-MM-dd[[' 'HH][:mm][:ss]")                         .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)                         .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)                         .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)                         .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)                         .toFormatter();                  result = LocalDateTime.parse(value, formatter);             }             else if(value.contains("/")) {                 DateTimeFormatter formatter = new DateTimeFormatterBuilder()                         .appendPattern("yyyy/MM/dd[[' 'HH][:mm][:ss]")                         .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)                         .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)                         .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)                         .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)                         .toFormatter();                  result = LocalDateTime.parse(value, formatter);             }         }         return result;     }      @Override     public WriteCellData convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,                                                GlobalConfiguration globalConfiguration) {         var dateTimeFormatProperty = contentProperty.getDateTimeFormatProperty();         var formatter = "yyyy-MM-dd HH:mm:ss";         if (dateTimeFormatProperty != null) {             formatter = dateTimeFormatProperty.getFormat();         }          return new WriteCellData<>(value.format(DateTimeFormatter.ofPattern(formatter)));     }  } 

二、实体类中使用

在为easyExcel定义的实体类中心添加自定义注解,例如
@ExcelProperty(value = "维运开航日期", converter = LocalDateTimeConverter.class) @DateTimeFormat("yyyy/MM/dd") // 指定导出格式 private LocalDateTime sailingDateFormatter; 

在converter中引入自定义的接收和转换规则

@DateTimeFormat的作用是定义导出的格式,在contentProperty方法中的contentProperty.getDateTimeFormatProperty()的getFormat方法可以取到这边定义的格式,从而在写excel时转为对应格式(注意判断contentProperty.getDateTimeFormatProperty()是否为null,没定义DateTimeFormat时就会为null,判空避免空指针)

相关内容

热门资讯

现场直击!新道游app下载,h... 现场直击!新道游app下载,hh poker透视器下载(透视)总是真的是有辅助脚本(哔哩哔哩)1、新...
透视辅助"wepok... 透视辅助"wepoker透视功能下载"竞技联盟辅助(确实真的有辅助辅助器)-哔哩哔哩1、玩家可以在w...
最新消息!花花生活圈怎么挂辅助... 最新消息!花花生活圈怎么挂辅助,h5新全游辅助(好像真的是有脚本)-哔哩哔哩1、用户打开应用后不用登...
来临!微乐广西麻辣,wepok... 来临!微乐广西麻辣,wepoker私人局俱乐部辅助(透视)都是真的有辅助插件(哔哩哔哩);微乐广西麻...
此事引发广泛关注"p... 此事引发广泛关注"poker master辅助"wejoker内置辅助(其实存在有辅助平台)-哔哩哔...
据权威媒体报道!财神十三章如何... 据权威媒体报道!财神十三章如何提高运气,微信微乐跑得快游戏辅助脚本(总是有挂工具)-哔哩哔哩亲,关键...
出乎意料的是!福建天天开心辅助... 出乎意料的是!福建天天开心辅助工具下载,来玩德州破解器(透视)果然是真的有辅助技巧(哔哩哔哩)1、每...
推出新举措"hhpo... 推出新举措"hhpoker软件可以玩吗"德州透视竞技联盟(都是是真的辅助工具)-哔哩哔哩1、hhpo...
这一现象值得深思!中至赣牌圈有... 这一现象值得深思!中至赣牌圈有挂吗,九神大厅辅助(都是真的有插件)-哔哩哔哩1、下载好中至赣牌圈有挂...
为了进一步!新畅游互娱科技,德... 为了进一步!新畅游互娱科技,德州真人透视脚本(透视)其实存在有辅助插件(哔哩哔哩)1、金币登录送、破...