此文章仅记录用法,不探讨过多其他的。
引入alibaba的excel依赖
com.alibaba easyexcel2.2.10
实现本地时间转换器
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 LocalDateTimeConverter implements Converter{ @Override public Class supportJavaTypeKey() { return LocalDateTime.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception { return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); } @Override public CellData convertToExcelData(LocalDateTime localDateTime, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception { return new CellData<>(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); } }
根据自己业务处理好报表数据,调用赋值
try (InputStream inputStream = this.getClass().getResourceAsStream("/templates/excel/export/feedbackOvertime.xlsx")) {
response.setHeader("Content-disposition", "attachment;filename=feedbackOvertime.xlsx");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
.registerConverter(new LocalDateTimeConverter())
.withTemplate(inputStream)
.build();
WriteSheet feedbackByOvertime = EasyExcel.writerSheet("feedbackOvertime").build();
excelWriter.fill(overtimeRespList, feedbackByOvertime);
excelWriter.finish();
} catch (IOException e) {
log.error("报表导出报错信息", e);
}
以下是图例注释
路径地址对应图例
表格格式图例:
sheet工作表也要一一对应:



