EasyExcel如何在一个sheet页中导出多张表以及动态表头

需求说明

业务要求将多张表导出数据到一个excel中,并在同一个sheet中,效果如下图:

在这里插入图片描述
easyexcel版本:2.2.8

代码:

    /**
     * 导出
     *
     * @param response 响应
     * @throws IOException ioexception
     */
    public void export(HttpServletResponse response) throws IOException {
        String fileName = System.currentTimeMillis() + "";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
        // 提前构建输出流
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        // 构建sheet页--表示不加表头
        WriteSheet writeSheet = EasyExcel.writerSheet("sheet").needHead(Boolean.FALSE).build();
        // 表头的数量
        int num = 0;
        // 模拟写5张表
        for (int i = 0; i < 5; i++) {
            // 获取第二行表头
            List<List<String>> contentHeader = this.buildHeader();
            // 生成第一行表头--这样两行表头长宽一致
            List<List<String>> nameHeader = contentHeader.stream().map(item -> Collections.singletonList("城市表")).collect(Collectors.toList());
            // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
            // 第一次写入会创建头
            WriteTable writeTable0 = EasyExcel.writerTable(num).needHead(Boolean.TRUE).head(nameHeader).build();
            excelWriter.write(null, writeSheet, writeTable0);

            WriteTable writeTable1 = EasyExcel.writerTable(num + 1).needHead(Boolean.TRUE).head(contentHeader).build();
            List<List<String>> body = this.buildBody();
            excelWriter.write(body, writeSheet, writeTable1);
            // 插入两次表头加2
            num = num + 2;
        }
        excelWriter.finish();
    }

    /**
     * 模拟表头
     *
     * @return {@link List}<{@link List}<{@link String}>>
     */
    public List<List<String>> buildHeader() {
        List<List<String>> header = new ArrayList<>();
        header.add(Collections.singletonList("序号"));
        header.add(Collections.singletonList("城市"));
        header.add(Collections.singletonList("省会"));
        header.add(Collections.singletonList("得分"));
        return header;
    }

    /**
     * 模拟表内容
     *
     * @return {@link List}<{@link List}<{@link String}>>
     */
    public List<List<String>> buildBody() {
        List<List<String>> result = new ArrayList<>();
        result.add(Lists.newArrayList("1", "南京", "江苏", "100"));
        result.add(Lists.newArrayList("2", "无锡", "江苏", "100"));
        result.add(Lists.newArrayList("3", "苏州", "江苏", "100"));
        result.add(Lists.newArrayList("4", "常州", "江苏", "100"));
        // 插入一个空行
        result.add(Collections.emptyList());
        return result;
    }

这种方式适合动态表头的生成,不依靠定义实体类,使用较灵活。

欢迎指正。

GitHub 加速计划 / ea / easyexcel
31.64 K
7.47 K
下载
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:3 个月前 )
c42183df Bugfix 3 个月前
efa7dff6 * 重新加回 `commons-io` 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐