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;
相关推荐
小鑫记得努力7 分钟前
Java类和对象(下篇)
java
binishuaio11 分钟前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
小奥超人12 分钟前
PPT文件设置了修改权限,如何取消权?
windows·经验分享·microsoft·ppt·办公技巧
zz.YE13 分钟前
【Java SE】StringBuffer
java·开发语言
老友@13 分钟前
aspose如何获取PPT放映页“切换”的“持续时间”值
java·powerpoint·aspose
wrx繁星点点28 分钟前
状态模式(State Pattern)详解
java·开发语言·ui·设计模式·状态模式
Upaaui31 分钟前
Aop+自定义注解实现数据字典映射
java
zzzgd81631 分钟前
easyexcel实现自定义的策略类, 最后追加错误提示列, 自适应列宽,自动合并重复单元格, 美化表头
java·excel·表格·easyexcel·导入导出
友善的鸡蛋33 分钟前
解决:使用EasyExcel导入Excel模板时出现数据导入不进去的问题
java·easyexcel·excel导入
星沁城33 分钟前
240. 搜索二维矩阵 II
java·线性代数·算法·leetcode·矩阵