List<TableExportVo> data1List = new ArrayList<>();
List<TableExportVo> data2List = new ArrayList<>();
List<TableExportVo> data3List = new ArrayList<>();
List<TableExportVo> data4List = new ArrayList<>();

for (YxfsYearPlanDetail yearPlanDetail : yearPlanDetailList) {
    YxfsEquipment yxfsEquipment = equipmentList.stream().filter(item -> item.getId().equals(yearPlanDetail.getEquipmentId())).findFirst().orElse(null);
    if (null != yxfsEquipment) {
        TableExportVo tableExportVo = new TableExportVo();
        BeanUtils.copyProperties(yearPlanDetail, tableExportVo);
        tableExportVo.setEquipmentName(yxfsEquipment.getName());
        tableExportVo.setStartDate(DateUtil.formatDate(yearPlanDetail.getStartDate()));
        tableExportVo.setEndDate(DateUtil.formatDate(yearPlanDetail.getEndDate()));
        if (GenerateConstant.POSITION_LIST.contains(yxfsEquipment.getType())) {
            if (GenerateConstant.LEFT_BANK_LIST.contains(yxfsEquipment.getCategory())) {
                data1List.add(tableExportVo);
            } else {
                data2List.add(tableExportVo);
            }
        } else if (GenerateConstant.LINE_500KV_LIST.contains(yxfsEquipment.getType())) {
            data3List.add(tableExportVo);
        } else if (GenerateConstant.FLOODWATER_LIST.contains(yxfsEquipment.getType())) {
            data4List.add(tableExportVo);
        }
    }
}


try {
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
    String fileName = null;
    fileName = URLEncoder.encode(year + "-" + (year + 1) + "年度检修计划一览表导出", "UTF-8").replaceAll("\\+", "%20");
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");


    try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(TABLE_EXPORT_TEMPLATE).build()) {
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        // 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹
        excelWriter.fill(new FillWrapper("data1", data1List), writeSheet);
        excelWriter.fill(new FillWrapper("data2", data2List), writeSheet);
        excelWriter.fill(new FillWrapper("data3", data3List), writeSheet);
        excelWriter.fill(new FillWrapper("data4", data4List), writeSheet);

        Map<String, Object> map = new HashMap<>();
        //map.put("date", "2019年10月9日13:28:28");
        map.put("title", year + "-" + (year + 1) + "年度检修计划一览表");

        excelWriter.fill(map, writeSheet);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
}
@Data
public class TableExportVo {
    private String equipmentName;

    private String repairLevel;

    private String startDate;

    private String endDate;

    private Integer duration;

    private String remark;
}

WARNING

注意,模板设定的字段名需要和实体的字段名一致

# To Be Continued!😎

Last Updated: 7/22/2023, 10:45:35 AM