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

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

码农世界 2024-06-07 后端 98 次浏览 0个评论

一、poi-tl官网

1. 地址:

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

2. 版本:1.5 版本

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

3. 引入Maven


    com.deepoove
    >poi-tl
    1.5.1

二、单个表格在word导出

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

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

2. 模板

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

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 mapp = new HashMap<>();
        List list = new ArrayList<>();
        List> mapList = ***Service.test1();
        for (Map 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. 效果

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

三、多个表格在word里导出

1. 模板必须是docx

2. 模板

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

△1122.docx△

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

△5566.docx△

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

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> list = new ArrayList<>();
        List> mapList = ***Service.test();
        // 根据某值分组;可以根据具体业务情况,自行修改,下面代码可有可无。
        Map>> groupedMap =
                mapList.stream()
                        .collect(Collectors.groupingBy(map -> map.get("tname").toString()));
        for (String key : groupedMap.keySet()) {
            List> maps = groupedMap.get(key);
            Map mapp = new HashMap<>();
            List listOne = new ArrayList<>();
            for (Map 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> list) throws IOException {
        Map 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. 效果

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

转载请注明来自码农世界,本文标题:《SpringBoot + Poi-tl实现word模板导出数据表格》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,98人围观)参与讨论

还没有评论,来说两句吧...

Top