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,判空避免空指针)

相关内容

热门资讯

实测交流透视挂!宝玛大厅ai智... 实测交流透视挂!宝玛大厅ai智能透视挂教程,宝玛大厅破解软件经验(本来有挂辅助)1、每一步都需要思考...
总算了解辅助器!朱雀吴江三缺一... 总算了解辅助器!朱雀吴江三缺一ai智能透视辅助挂技巧,朱雀吴江三缺一好像是有挂(有辅助挂细节)朱雀吴...
今日公布透视!九五至尊大厅ai... 今日公布透视!九五至尊大厅ai智能透视插件,九五至尊大厅有没有透视挂窍门(切实有挂技术)1、在九五至...
玩家必看科普开挂!聚闲麻将ai... 玩家必看科普开挂!聚闲麻将ai智能透视辅助挂软件,聚闲麻将总是真的有挂(有辅助挂方式)聚闲麻将能透视...
据报道开挂!旅游娱乐ai智能开... 据报道开挂!旅游娱乐ai智能开挂工具,旅游娱乐辅助工具演示(一直有挂教学)1、全新机制【旅游娱乐ai...
一分钟揭秘开挂!决胜朋来麻将a... 一分钟揭秘开挂!决胜朋来麻将ai智能透视辅助挂教程,决胜朋来麻将真是真的是有挂(有辅助挂总结)1、首...
如何分辨真伪透视!红牛ai智能... 如何分辨真伪透视!红牛ai智能透视攻略,红牛是不是有挂经验(真是有挂技术)1、操作简单,无需红牛手机...
2分钟细说辅助器!江湖大庆麻将... 2分钟细说辅助器!江湖大庆麻将ai智能透视辅助挂攻略,江湖大庆麻将竟然是真的有挂(有辅助挂头条)1、...
总算清楚透视挂!老夫子厅ai智... 总算清楚透视挂!老夫子厅ai智能透视挂技巧,老夫子厅怎么开挂课程(好像发现有挂)老夫子厅破解侠是真的...
解密关于辅助!菜鸟麻将ai智能... 解密关于辅助!菜鸟麻将ai智能透视辅助挂脚本,菜鸟麻将总是存在有挂(有辅助挂存在)1、用户打开应用后...