当前位置: 首页 > article >正文

java导出Excel接口

需求:需要实现一个导出的接口,点导出同时导出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;


http://www.kler.cn/a/288263.html

相关文章:

  • 《TCP/IP网络编程》学习笔记 | Chapter 10:多进程服务器端
  • 【快捷入门笔记】mysql基本操作大全-SQL表
  • AndroidStudio-常用布局
  • 蓝队知识浅谈(上)
  • 844.比较含退格的字符串
  • 【计算机网络】【传输层】【习题】
  • 32位CPU中,实现32位无符号乘法,返回64位无符号数据。原理解析。
  • oracle expdp/impdp 迁移数据库
  • 数学基础 -- 图像处理之Sobel卷积核推导过程
  • C++STL~~list
  • Day21—爬虫性能优化技巧
  • 自动化仓储系统:知识汇总
  • 将语义分割的标签转换为实例分割(yolo)的标签
  • vim和nano的基础教程(很基础的那种)
  • wordcloud兼figma的词云图片python生成
  • 情感共鸣:数业智能心大陆重塑AI心理交互新纪元
  • Github 2024-09-03 Python开源项目日报 Top10
  • cpu架构:x86和arm
  • 猎板PCB正片与负片工艺:深入解析PCB制造中的光刻技术及其应用
  • 【深度学习 transformer】transformer 训练一个文本分类任务,超简单了
  • 【初出江湖】分布式之什么是分布式存储?
  • 函数式编程(纯函数函数柯里化代码组合)
  • WPF性能优化之UI虚拟化
  • 贪心算法---监控二叉树
  • 综合评价 | 基于层次-熵权-博弈组合法的综合评价模型(Matlab)
  • JavaScript学习文档(12):什么是正则表达式、语法、元字符、修饰符