当前位置:   article > 正文

SpringBoot + Poi-tl实现word模板导出数据表格_poi-tl官网

poi-tl官网

一、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>
  • 1
  • 2
  • 3
  • 4
  • 5

二、单个表格在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();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

4. 效果

在这里插入图片描述

三、多个表格在word里导出

1. 模板必须是docx

2. 模板

  -----需要2个模板-----
  • 1

△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();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
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("---------完成---------");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

4. 效果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/606843
推荐阅读
相关标签
  

闽ICP备14008679号