赞
踩
如果未加处理,实体类属性为Date,那么导出的数据 时间默认时分秒赋值为00:00:00,如下图:
如果只想展示年月日,需要添加一个转换器DateConverter,下面是参考代码:
- import com.alibaba.excel.converters.Converter;
- import com.alibaba.excel.converters.WriteConverterContext;
- import com.alibaba.excel.metadata.data.WriteCellData;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- /**
- * 将时间格式转化为yyyy-MM-dd
- */
- public class DateConverter implements Converter<Date> {
-
- private static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
-
-
- @Override
- public Class<Date> supportJavaTypeKey() {
- return Date.class;
- }
-
-
- @Override
- public WriteCellData<String> convertToExcelData(WriteConverterContext<Date> context) throws Exception {
- Date date = context.getValue();
- if (date == null) {
- return null;
- }
- SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YYYY_MM_DD);
- return new WriteCellData<>(sdf.format(date));
- }
- }

然后在导出的实体类对应日期属性中加上下面的注解@ExcelProperty并在括号内引入刚刚添加的转换器。
- @ExcelProperty(converter = DateConverter.class)
- private Date dataTime;
这样处理后,导出的时间就转换为年月日了,
注意,我使用的easyexcel版本为3.0.2,如果easyexcel为3以下的版本,可参考这篇文章处理使用 EasyExcel 转换器自定义时间类型转换_使用 EasyExcel 转换器自定义时间类型转换_easyexcel 时间格式_404技术社区
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。