一、poi-tl官网

1. 地址:

https://deepoove.com/poi-tl/

2. 版本:1.5 版本

在这里插入图片描述

3. 引入Maven

<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.5.1</version>
</dependency>

二、单个表格在word导出

1. 模板必须是docx,也就是:

在这里插入图片描述

2. 模板

在这里插入图片描述

3. 代码

public void test1(HttpServletRequest request, HttpServletResponse response){
        TableStyle tableStyle = new TableStyle();
        tableStyle.setBackgroundColor("F2F2F2");
        tableStyle.setAlign(STJc.CENTER);

        Style style = StyleBuilder.newBuilder().buildBold().buildFontSize(10).build();
        RowRenderData header = RowRenderData.build(
                new TextRenderData("序号", style),
                new TextRenderData("表名", style),
                new TextRenderData("描述", style));
        header.setRowStyle(tableStyle);

        Map<String, Object> mapp = new HashMap<>();
        List<RowRenderData> list = new ArrayList<>();
        List<Map<String, Object>> mapList = ***Service.test1();
        for (Map<String, Object> map : mapList) {
            RowRenderData rowRenderData = RowRenderData.build(
                    map.get("xh").toString(),
                    map.get("name").toString(),
                    map.get("comment") != null ? map.get("comment").toString() : null
            );
            list.add(rowRenderData);
        }
        // tables 就是模板定义的那个
        mapp.put("tables", new MiniTableRenderData(header, list));

        try {
            // 模板
            XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\***\\Desktop\\bbbb.docx").render(mapp);
            // 生成的文件
            FileOutputStream out = new FileOutputStream("C:\\Users\\***\\Desktop\\out_.docx");
            template.write(out);
            out.flush();
            out.close();
            template.close();

            InputStream fis = null;
            OutputStream toClient = null;
            File file = new File("C:\\Users\\***\\Desktop\\out_.docx");
            fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            String newWordName = URLEncoder.encode("单表模板", "utf-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + newWordName);
            response.setContentType("application/octet-stream;charset=utf-8");
            response.addHeader("Content-Length", "" + file.length());

            toClient = new BufferedOutputStream(response.getOutputStream());

            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();

            System.out.println("---------完成---------");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4. 效果

在这里插入图片描述

三、多个表格在word里导出

1. 模板必须是docx

2. 模板

  -----需要2个模板-----

△1122.docx△
在这里插入图片描述

△5566.docx△
在这里插入图片描述

3. 代码

public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 加粗、字体大小为10
        Style style = StyleBuilder.newBuilder().buildBold().buildFontSize(10).build();
        RowRenderData header = RowRenderData.build(
                new TextRenderData("字段名", style),
                new TextRenderData("类型", style),
                new TextRenderData("长度", style),
                new TextRenderData("描述", style));
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> mapList = ***Service.test();
        // 根据某值分组;可以根据具体业务情况,自行修改,下面代码可有可无。
        Map<String, List<Map<String, Object>>> groupedMap =
                mapList.stream()
                        .collect(Collectors.groupingBy(map -> map.get("tname").toString()));

        for (String key : groupedMap.keySet()) {
            List<Map<String, Object>> maps = groupedMap.get(key);

            Map<String, Object> mapp = new HashMap<>();
            List<RowRenderData> listOne = new ArrayList<>();
            for (Map<String, Object> map : maps) {

                RowRenderData rowRenderData = RowRenderData.build(
                        map.get("cname").toString(),
                        map.get("ctype").toString(),
                        map.get("clength") != null ? map.get("clength").toString() : null,
                        map.get("ccomment") != null ? map.get("ccomment").toString() : null
                );
                listOne.add(rowRenderData);
            }
            mapp.put("tname", ObjectUtils.isNotEmpty(maps.get(0).get("tcomment")) ? maps.get(0).get("tcomment") : key);
            MiniTableRenderData miniTable = new MiniTableRenderData(header, listOne);
            miniTable.setWidth(15.22f);
            // 1122.docx模板的单个表格+数据
            mapp.put("table", miniTable);
            list.add(mapp);
        }
        try {
            download(request, response, "模板数据.docx", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public static void download(HttpServletRequest request, HttpServletResponse response, String newWordName, List<Map<String, Object>> list) throws IOException {
        Map<String, Object> map = new HashMap<>();
        DocxRenderData info = new DocxRenderData(new File("C:\\Users\\***\\Desktop\\1122.docx"), list);
        // 将每个单表格放到 5566.docx模板
        map.put("tables", info);
        // 展示多个表格的模板
        XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\***\\Desktop\\5566.docx").render(map);
        //输出文件
        FileOutputStream out = new FileOutputStream("C:\\Users\\***\\Desktop\\out_.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();

        InputStream fis = null;
        OutputStream toClient = null;
        File file = new File("C:\\Users\\***\\Desktop\\out_.docx");
        fis = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        response.reset();
        // 解决中文乱码
        newWordName = URLEncoder.encode(newWordName, "utf-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + newWordName);
        response.setContentType("application/octet-stream;charset=utf-8");
        response.addHeader("Content-Length", "" + file.length());

        toClient = new BufferedOutputStream(response.getOutputStream());

        response.setContentType("application/octet-stream");
        toClient.write(buffer);
        toClient.flush();

        System.out.println("---------完成---------");
    }

4. 效果

在这里插入图片描述

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐