需要自定义日期转换器,代码如下。
里面用来Hutool的日期处理工具,你也可以自己写日期转换工具类来处理。
@Data
@ExcelIgnoreUnannotated
public class OfficialAccountImportDTO {
@ExcelProperty("真实姓名")
private String name;
@ExcelProperty("手机号")
private String phone;
@ExcelProperty(value = "生日",converter = EasyExcelLocalDateConverter.class)
@JsonFormat(pattern = "yyyy/MM/dd")
private LocalDate birthday;
@ExcelProperty("公司")
private String company;
@ExcelProperty(value = "注册时间",converter = EasyExcelLocalDateTimeConverter.class)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@ExcelProperty("审核状态")
private String auditStatus;
@ExcelProperty("审核不通过原因")
private String failReasons;
@ExcelProperty(value = "审核时间",converter = EasyExcelLocalDateTimeConverter.class)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
}
import cn.hutool.core.date.DatePattern;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class EasyExcelLocalDateConverter implements Converter<LocalDate> {
@Override
public Class supportJavaTypeKey() {
return LocalDate.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDate convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN));
}
@Override
public CellData convertToExcelData(LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new CellData<>(value.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
}
}
import cn.hutool.core.date.DatePattern;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class EasyExcelLocalDateTimeConverter implements Converter<LocalDateTime> {
@Override
public Class supportJavaTypeKey() {
return LocalDateTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
}
@Override
public CellData convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new CellData<>(value.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
}
}