java使用poi-tl自定义word模板导出

文章目录

概要

在软件开发领域,自定义Word模板的使用是导出格式化数据的一种常见做法。poi-tl(Apache POI Template Language)作为一款基于广受认可的Apache POI库的Word模板引擎,它以纯Java组件的形式提供服务,确保了跨平台的兼容性。poi-tl以其简洁高效的代码著称,不仅易于集成,还通过其插件机制实现了功能的高度可扩展性,允许开发者根据项目需求灵活定制。这一特性使得poi-tl成为处理Word文档生成任务时的一个强有力工具。

整体架构流程

官网:poi-tl

java 复制代码
 <!--    POI 依赖 使用xlsx xml的格式(即XSSFWorkbook)   -->
  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.17</version>
  </dependency>
        <!--     poi模板导入,主力包      -->
  <dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.12.1</version>
  </dependency>

创建word模板

核心代码

java 复制代码
       try {
            String pictureUrl = "D:\\file\\test\\image\\bigPicture10.jpeg";
            if (pictureUrl != null && !pictureUrl.isEmpty()) {
                PictureRenderData picture = Pictures.ofLocal(pictureUrl).size(40, 30).create();
                finalMap.put("signPicture", picture);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 从classpath加载Word模板文件到临时文件
        try (InputStream inputStream = TestWord.class.getClassLoader().getResourceAsStream("template.docx")) {
            if (inputStream == null) {
                throw new RuntimeException("无法找到模板文件:template.docx");
            }

            // 创建一个临时文件用于XWPFTemplate处理
            Path tempFilePath = Files.createTempFile("word-template-", ".docx");
            Files.copy(inputStream, tempFilePath, StandardCopyOption.REPLACE_EXISTING);

            // 使用临时文件作为模板
            File wordTemplate = tempFilePath.toFile();
            LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
            Configure build = Configure.builder().bind(policy, "workList").build();

            XWPFTemplate render = XWPFTemplate.compile(wordTemplate, build).render(finalMap);

            // 定义输出路径
            String outputPath = "D:\\file\\htht\\project\\260\\开发\\output.docx"; // 修改为期望的输出文件路径
            File outputFile = new File(outputPath);
            try {
                if (!outputFile.getParentFile().exists()) {
                    outputFile.getParentFile().mkdirs(); // 确保父目录存在
                }
                render.writeToFile(outputFile.getAbsolutePath());
                System.out.println("Word文档成功导出到: " + outputPath);
            } catch (IOException e) {
                throw new RuntimeException("无法写入文件: " + e.getMessage(), e);
            }

            // 删除临时文件(可选)
            Files.deleteIfExists(tempFilePath);

        } catch (IOException e) {
            throw new RuntimeException("读取模板文件失败: " + e.getMessage(), e);
        }

导出结果

资源提取:https://download.csdn.net/download/yy12345_6_/90275767

相关推荐
敢敢J的憨憨L6 分钟前
GPTL(General Purpose Timing Library)使用教程
java·服务器·前端·c++·轻量级计时工具库
26 分钟前
JUC专题 - 并发编程带来的安全性挑战之同步锁
后端
凯哥197027 分钟前
迁移PostgreSQL数据库教程
后端
Ray6636 分钟前
单例模式
后端
用户83562907805136 分钟前
掌控PDF页面:使用Python轻松实现添加与删除
后端·python
无责任此方_修行中42 分钟前
谁动了我的数据?一个 Bug 背后的“一行代码”真凶
后端·node.js·debug
sg_knight1 小时前
Spring Cloud与RabbitMQ深度集成:从入门到生产级实战
java·spring boot·spring·spring cloud·消息队列·rabbitmq·stream
hsjkdhs1 小时前
C++之类的继承与派生
开发语言·c++
用户47949283569151 小时前
面试官:讲讲2FA 双因素认证原理
前端·后端·安全
疯狂的程序猴1 小时前
移动端H5网页远程调试:WEINRE、Charles与Genymotion完整指南
后端