需求:需要实现一个导出的接口,点导出同时导出5个Excel表格,五种表格的数据筛选不同数据
public String export(MarketReceiveVo query) {
Date winApprovalStartDate = query.getWinApprovalStartDate();
Date winApprovalEndDate = query.getWinApprovalEndDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String winApprovalStartDateStr = sdf.format(winApprovalStartDate);
String winApprovalEndDateStr = sdf.format(winApprovalEndDate);
// 根据日期获取团队详细信息
List<MarketXxxxListVo> allAchievementExport = marketSysFrameworkService.getAllAchievement(winApprovalStartDate, winApprovalEndDate);//所有的
List<MarketXxxxListVo> gManagerExportVo = marketSysFrameworkService.getGManager(winApprovalStartDate, winApprovalEndDate);// 副总的数据
List<MarketXxxxListVo> leaderExports = marketSysFrameworkService.getLeaderExports(winApprovalStartDate, winApprovalEndDate);// 组长的数据
List<MarketXxxxListVo> TeamMemberExport = marketSysFrameworkService.getTeamMemberExport(winApprovalStartDate, winApprovalEndDate);// 组员的数据
List<MarketXxxxListVo> marketAchievementExport = marketSysFrameworkService.getDetailed(winApprovalStartDate, winApprovalEndDate); // 分配明细
// 数据转换
List<AllAchievementExportVo> exportVos = Optional.ofNullable(allAchievementExport)
.orElse(new ArrayList<>())
.stream()
.map(source -> BeanUtil.convert(source, AllAchievementExportVo.class))
.collect(Collectors.toList());
List<GManagerExportVo> exportVosGManager = Optional.ofNullable(gManagerExportVo)
.orElse(new ArrayList<>())
.stream()
.map(source -> BeanUtil.convert(source, GManagerExportVo.class))
.collect(Collectors.toList()); //副总
List<AllAchievementExportVo> exportVosGroupLeader = Optional.ofNullable(leaderExports)
.orElse(new ArrayList<>())
.stream()
.map(source -> BeanUtil.convert(source, AllAchievementExportVo.class))
.collect(Collectors.toList());
List<AllAchievementExportVo> exportVosStaff = Optional.ofNullable(TeamMemberExport)
.orElse(new ArrayList<>())
.stream()
.map(source -> BeanUtil.convert(source, AllAchievementExportVo.class))
.collect(Collectors.toList());
List<MarketAchievementExportVo> exportVosFive = Optional.ofNullable(marketAchievementExport)
.orElse(new ArrayList<>())
.stream()
.map(source -> BeanUtil.convert(source, MarketAchievementExportVo.class))
.collect(Collectors.toList());
// 排序
List<AllAchievementExportVo> sortedAll = sortData(exportVos);
List<GManagerExportVo> sortedGManager = sortDataGManager(exportVosGManager);
List<AllAchievementExportVo> sortedGroupLeader = sortData(exportVosGroupLeader);
List<AllAchievementExportVo> sortedStaff = sortData(exportVosStaff);
List<MarketAchievementExportVo> sortedDetailed= sortDataDetailed(exportVosFive);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
ExcelWriter writeWorkbook = EasyExcel.write(outputStream).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
// 创建5个Sheet
WriteSheet writeSheetAll = EasyExcel.writerSheet("所有人排名").head(AllAchievementExportVo.class).build();
writeWorkbook.write(sortedAll, writeSheetAll);
WriteSheet writeSheetGManager = EasyExcel.writerSheet("营销副总排名").head(GManagerExportVo.class).build();
writeWorkbook.write(sortedGManager, writeSheetGManager);
WriteSheet writeSheetAllThree = EasyExcel.writerSheet("组长排名").head(AllAchievementExportVo.class).build();
writeWorkbook.write(sortedGroupLeader, writeSheetAllThree);
WriteSheet writeSheetFour = EasyExcel.writerSheet("组员排名").head(AllAchievementExportVo.class).build();
writeWorkbook.write(sortedStaff, writeSheetFour);
WriteSheet writeSheetDetailed = EasyExcel.writerSheet("业绩分配明细").head(MarketAchievementExportVo.class).build();
writeWorkbook.write(sortedDetailed, writeSheetDetailed);
writeWorkbook.finish();
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray())) {
FileInfo fileInfo = whaleClientConfig.getWhalefsClient()
.upload(whaleClientConfig.getBucketName(), "tmp/" + "业绩分配" + winApprovalStartDateStr + "-" + winApprovalEndDateStr + ".xlsx", inputStream, true);
return fileInfo.getUrl();
}
} catch (Exception e) {
e.printStackTrace();
log.error(String.format("业绩分配导出错误, 参数:[%s]", JSONObject.toJSONString(query)));
log.error(String.format("业绩分配导出错误, 错误信息:[%s]", e.getMessage()));
throw new BusinessException("导出Excel错误");
}
}
要注意导出类的实体Vo,对于一些Excel常用的注解,注解类的注释
@ApiModelProperty(value = "序号")
@TableId(type = IdType.AUTO)
@ExcelProperty(value = "序号",index = 0)
private Integer index;
多余不展示字段
@ApiModelProperty(value = "上报通过开始日期")
@DateTimeFormat("yyyy-MM-dd")
@ExcelIgnore
private Date winApprovalStartDate;