【POI-导出word-One】poi-tl根据word模板去渲染数据(不包含图片)

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

引包版本(maven版),如果版本不一致,很有可能导致有些底层方法调用失败:

<!-- 代码生成器结束 -->
        <!-- poi-tl -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.12.0</version>
        </dependency>
        <!-- poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>
        <!-- poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.2</version>
        </dependency>
        <!-- poi-ooxml-schemas -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <!-- poi-scratchpad -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>5.2.2</version>
            <scope>compile</scope>
        </dependency>

以下只是放在本地测试的简易代码:word模板,里面的字段占位符设置格式:{{xm}}

使用场景:渲染多个对象,并且每个对象渲染一次word模板,第二个对象渲染模板的是另起一页,最后放在一个word里面;我觉得这样操作使用的好处就是,导出的模板格式不需要自己通过代码手动去调,直接新建一个word,或者用下发的word格式

放入占位符,就可以直接按照原来的模板生成,字体,字体大小,边框这些都不需要通过后端代码手动去调整,这个是最方便的,如果有轻微的模板格式调整,只要占位符不变,直接替换模板就行,不需要重新改代码,再重新打包部署。

 @PostMapping("/testWord2")
    @ApiOperation(value = "测试接口", notes = "测试接口")
    public void testWord2() {
        String templatePath = "C:\\Users\\WordTest.docx";
        String outputPath = "C:\\Users\\ExportedDoc"+ DateUtils.dateTimeNow() +".docx";

        try (XWPFDocument document = new XWPFDocument(new FileInputStream(templatePath));
             FileOutputStream out = new FileOutputStream(outputPath)) {
            // 删除模板中的第一页表格或内容
            if (!document.getTables().isEmpty()) {
                document.removeBodyElement(document.getPosOfTable(document.getTables().get(0)));
            }

            List<Map<String, Object>> records = new ArrayList<>();
            Map<String,Object> map = new HashMap<>();
            map.put("xm","张哈哈哈");
            map.put("xb","-------");
            map.put("lxdh","=========");
            records.add(map);
            Map<String,Object> map2 = new HashMap<>();
            map2.put("xm","张嘻嘻嘻");
            map2.put("xb","-------");
            map2.put("lxdh","=========");
            records.add(map2);

            for (Map<String, Object> record : records) {
                XWPFTemplate template = XWPFTemplate.compile(templatePath);
                template.render(record);
                for (XWPFTable table : template.getXWPFDocument().getTables()) {
                    document.createTable();
                    document.setTable(document.getTables().size() - 1, table);
                }
                if (record != records.get(records.size() - 1)) {
                    document.createParagraph().setPageBreak(true);
                }
                template.close();
            }

            document.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这个只是一个最简易的版本,在本地运行导出,实际情况,一般是Linux服务器部署,所以获取的模板的方式和导出的方式肯定是要有更改的,再一个,要考虑导出的模板在我们服务器是不是要保存一份呢?

Hello!希望可以共同进步!一起讨论吧~

 

posted @ 2024-12-11 10:23  阿迪di  阅读(291)  评论(0)    收藏  举报
Title