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

相关推荐
weixin_40809967几秒前
【实战教程】懒人精灵如何实现 OCR 文字识别?接口调用完整指南(附可运行示例)
java·前端·人工智能·后端·ocr·api·懒人精灵
珍朱(珠)奶茶2 分钟前
Spring Boot3整合Jxls工具包实现模版excel导出文件
spring boot·后端·excel
Daemon9 分钟前
AI Agent系列记录(第二篇)
前端·人工智能·后端
独特的螺狮粉13 分钟前
开源鸿蒙跨平台Flutter开发:喝水时间提醒应用
开发语言·flutter·华为·信息可视化·开源·harmonyos·鸿蒙
冰心少年14 分钟前
ROS2节点:机器人的工作细胞
后端
冰心少年14 分钟前
ROS2话题:节点间传递数据的桥梁
后端
花千树-01017 分钟前
Java Agent 集成 MCP 工具协议:让 AI 真正驱动企业系统
java·ai·langchain·ai agent·mcp·harness·j-langchain
橘子编程22 分钟前
GoF 23 种设计模式完整知识总结与使用教程
java·c语言·开发语言·python·设计模式
玖釉-24 分钟前
告别 Shared Memory 瓶颈:Vulkan Subgroup 架构解析与硬核实战指南
开发语言·c++·windows·图形渲染
不懂的浪漫25 分钟前
mqtt-plus 架构解析(一):分层架构与设计哲学
spring boot·分布式·物联网·mqtt·架构