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

posted @   肖恩雷  阅读(5812)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示