在需要使用word文档进行模板实现动态打印的时候,可以使用poi-tl这个开源库来实现

1 为什么使用 poi-tl 来进行模板文件的生成

alt textimg

poi-tl是一个基于Apache POI的Word模板引擎,也是一个免费开源的Java类库,你可以非常方便的加入到你的项目中,并且拥有着让人喜悦的特性。

2 使用方法及注意事项

JDK1.8+ Apache POI 5.2.2+ 依赖:poi-ooxml

每个版本的poi-tl对 poi(poi-ooxml)的版本依赖都不尽相同,一定要注意。这里给一组比较完整的maven引用

        <!-- poi-tl 按docx模板生成word -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.12.2</version>
        </dependency>

        <!-- poi和 poi-ooxml的版本依赖 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.3</version>
        </dependency>

在java中引用

    private String doGenerateRegisterDocxForm(DataDTO dataDTO) {
        String generateRegisterDocxFormUri = "";
        try {
            UserRegister userRegister=dataDTO.getUserRegister();

            Map<String, Object> params = new HashMap<>();
            params.put("studentId", userRegister.getStudentId());
            params.put("studentSex", userRegister.getStudentSex()==1?"男":"女");
            params.put("studentName", userRegister.getStudentName());
            params.put("householdAddress", userRegister.getHouseholdAddress());
            params.put("studentIdNumber", userRegister.getStudentIdNumber());
            Resource resource = new ClassPathResource("/static/register_tpl/bmTpl.docx");

            File file = resource.getFile();
            // 数据填充
            XWPFTemplate template = XWPFTemplate.compile(file).render(params);

            String localGenerateFilePath = ResourceUtils.getURL(uploadConfig.getFrontendUploadPath()).getPath();

            String docOutDir = String.join("/", localGenerateFilePath.replaceAll("/$", ""), uploadConfig.getGenerateRegisterDocxFormDir());
            String fileName = String.format("ER_%s.docx", userRegister.getStudentId());
            String docOutPath = String.format("%s/%s", docOutDir, fileName);
            //生成用于下载打印的相对路径地址
            String relativeFileName = String.join("/","", uploadConfig.getGenerateRegisterDocxFormDir(), fileName);

            template.writeAndClose(new FileOutputStream(docOutPath));

            generateRegisterDocxFormUri = relativeFileName;
        } catch (Exception e) {
            sys_log.error(e.toString());
        }


        return generateRegisterDocxFormUri;
    }

    
    模板原型
    ![alt text](imag![img](https://img2023.cnblogs.com/blog/611772/202405/611772-20240510104753723-41290289.png)e-7.png)

    生成的打印表
    ![alt text](imag![img](https://img2023.cnblogs.com/blog/611772/202405/611772-20240510104901469-1993790974.png)e-8.png)

3 配置springboot打包时能包含docx模板文件

img

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
        <configuration>
            <nonFilteredFileExtensions>docx</nonFilteredFileExtensions>
        </configuration>
        <executions>
            ......
        </execution>
    </executions>
</plugin>

4 参考地址

http://deepoove.com/poi-tl/#_maven