|
当使用java的easyExcel进行excel导入和导出时,如何接收表格中的各种自定义时间格式字符串转成LocalDateTime及导出时如何将LocalDateTime转为各种时间格式字符串。一、新建自定义时间格式转换类 packagecom.logwing.wyadmin.framework.excel;importcom.alibaba.excel.converters.Converter;importcom.alibaba.excel.enums.CellDataTypeEnum;importcom.alibaba.excel.metadata.GlobalConfiguration;importcom.alibaba.excel.metadata.data.ReadCellData;importcom.alibaba.excel.metadata.data.WriteCellData;importcom.alibaba.excel.metadata.property.ExcelContentProperty;importorg.apache.poi.ss.usermodel.DateUtil;importjava.time.LocalDateTime;importjava.time.ZoneId;importjava.time.format.DateTimeFormatter;importjava.time.format.DateTimeFormatterBuilder;importjava.time.temporal.ChronoField;importjava.util.Date;publicclassLocalDateTimeConverterimplementsConverter{@OverridepublicClasssupportJavaTypeKey(){returnLocalDateTime.class;}@OverridepublicCellDataTypeEnumsupportExcelTypeKey(){returnCellDataTypeEnum.STRING;}@OverridepublicLocalDateTimeconvertToJavaData(ReadCellDatacellData,ExcelContentPropertycontentProperty,GlobalConfigurationglobalConfiguration){if(null==cellData){returnnull;}LocalDateTimeresult=null;if(cellData.getType()==CellDataTypeEnum.NUMBER){if(contentProperty==null||contentProperty.getDateTimeFormatProperty()==null){Datedate=DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),globalConfiguration.getUse1904windowing(),null);result=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();}else{Datedate=DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),contentProperty.getDateTimeFormatProperty().getUse1904windowing(),null);result=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();}}if(cellData.getType()==CellDataTypeEnum.STRING){Stringvalue=cellData.getStringValue();if(value.contains("-")){DateTimeFormatterformatter=newDateTimeFormatterBuilder().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);}elseif(value.contains("/")){DateTimeFormatterformatter=newDateTimeFormatterBuilder().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);}}returnresult;}@OverridepublicWriteCellDataconvertToExcelData(LocalDateTimevalue,ExcelContentPropertycontentProperty,GlobalConfigurationglobalConfiguration){vardateTimeFormatProperty=contentProperty.getDateTimeFormatProperty();varformatter="yyyy-MM-ddHH:mm:ss";if(dateTimeFormatProperty!=null){formatter=dateTimeFormatProperty.getFormat();}returnnewWriteCellData(value.format(DateTimeFormatter.ofPattern(formatter)));}}二、实体类中使用在为easyExcel定义的实体类中心添加自定义注解,例如@ExcelProperty(value="维运开航日期",converter=LocalDateTimeConverter.class)@DateTimeFormat("yyyy/MM/dd")//指定导出格式privateLocalDateTimesailingDateFormatter;在converter中引入自定义的接收和转换规则@DateTimeFormat的作用是定义导出的格式,在contentProperty方法中的contentProperty.getDateTimeFormatProperty()的getFormat方法可以取到这边定义的格式,从而在写excel时转为对应格式(注意判断contentProperty.getDateTimeFormatProperty()是否为null,没定义DateTimeFormat时就会为null,判空避免空指针)
|
|