EasyExcel导出Date类型格式处理
EasyExcel导出Date类型格式处理
如果在导出的excel中有date时间类型的字段,直接导出会报错org.apache.poi.ss.usermodel.Cell.setCellValue(Ljava/time/LocalDateTime;)
,因此需要做类型转换,需要实现Converter<Date>
接口;
package com.pms.rcm.reports.manager;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author : sean
* @date Date : 2022-10-29 23:21
* @Description:
*/
public class DateConverter implements Converter<Date> {
private static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd hh:mm:hh";
@Override
public Class<?> supportJavaTypeKey() {
return Converter.super.supportJavaTypeKey();
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return Converter.super.supportExcelTypeKey();
}
@Override
public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YYYY_MM_DD);
String dateValue = sdf.format(value);
return new WriteCellData<>(dateValue);
}
}
如果excel中不需要导出date时间类型的字段,那么可以在字段上使用注解@ExcelIgnore
或在实体类上使用注解@ExcelIgnoreUnannotated
参考连接:https://blog.csdn.net/weixin_48437239/article/details/125875164