如果未加处理,实体类属性为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;
这样处理后,导出的时间就转换为年月日了,
文章来源:https://www.toymoban.com/news/detail-559418.html
注意,我使用的easyexcel版本为3.0.2,如果easyexcel为3以下的版本,可参考这篇文章处理使用 EasyExcel 转换器自定义时间类型转换_使用 EasyExcel 转换器自定义时间类型转换_easyexcel 时间格式_404技术社区文章来源地址https://www.toymoban.com/news/detail-559418.html
到了这里,关于easyexcel3以上版本处理导出excel时间格式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!