java导出Excel接口

需求:需要实现一个导出的接口,点导出同时导出5个Excel表格,五种表格的数据筛选不同数据

java 复制代码
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常用的注解,注解类的注释

java 复制代码
@ApiModelProperty(value = "序号")
    @TableId(type = IdType.AUTO)
    @ExcelProperty(value = "序号",index = 0)
    private Integer index;

多余不展示字段

java 复制代码
@ApiModelProperty(value = "上报通过开始日期")
    @DateTimeFormat("yyyy-MM-dd")
    @ExcelIgnore
    private Date winApprovalStartDate;
相关推荐
初晴~5 分钟前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
黑胡子大叔的小屋1 小时前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
ThisIsClark1 小时前
【后端面试总结】深入解析进程和线程的区别
java·jvm·面试
可喜~可乐1 小时前
C# WPF开发
microsoft·c#·wpf
微凉的衣柜1 小时前
微软在AI时代的战略布局和挑战
人工智能·深度学习·microsoft
雷神乐乐2 小时前
Spring学习(一)——Sping-XML
java·学习·spring
小林coding2 小时前
阿里云 Java 后端一面,什么难度?
java·后端·mysql·spring·阿里云
V+zmm101342 小时前
基于小程序宿舍报修系统的设计与实现ssm+论文源码调试讲解
java·小程序·毕业设计·mvc·ssm
文大。3 小时前
2024年广西职工职业技能大赛-Spring
java·spring·网络安全
一只小小翠3 小时前
EasyExcel 模板+公式填充
java·easyexcel