初次使用poi-tl 渲染docx

文章目录

概要

记录首次使用poi-tl渲染docx模板

步骤

环境为java21

  • 目录结构如下
    在这里插入图片描述

  • 引入maven依赖

<!--  玩一下docx解析   poi-tl  依赖开始  -->
		<dependency>
			<groupId>com.deepoove</groupId>
			<artifactId>poi-tl</artifactId>
			<version>1.12.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>5.2.3</version> <!-- 确保版本与 poi-tl 兼容 -->
		</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-ooxml-schemas</artifactId>
			<version>4.1.2</version> <!-- 版本需与 poi-ooxml 兼容 -->
		</dependency>
		<dependency>
			<groupId>org.apache.xmlbeans</groupId>
			<artifactId>xmlbeans</artifactId>
			<version>5.1.1</version> <!-- 版本需与 poi-ooxml-schemas 兼容 -->
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.11.0</version> <!-- 版本可根据实际情况调整 -->
		</dependency>
		<dependency>
			<groupId>org.apache.xmlgraphics</groupId>
			<artifactId>batik-transcoder</artifactId>
			<version>1.17</version>
		</dependency>
		<dependency>
			<groupId>org.apache.xmlgraphics</groupId>
			<artifactId>batik-codec</artifactId>
			<version>1.17</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
		<!--<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.32</version>
		</dependency>-->
		<!-- Log4j2 API 依赖 -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.20.0</version> <!-- 使用适当的版本 -->
    </dependency>
		<!-- Log4j2 核心库 依赖 -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.20.0</version> <!-- 使用适当的版本 -->
    </dependency>
		<!-- 可选:Log4j2 配置文件依赖 -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.20.0</version> <!-- 使用适当的版本 -->
    </dependency>
		<!-- 其他必要的依赖 -->
		<!--  玩一下docx解析   poi-tl  依赖结束  -->
  • 准备一个模板
    在这里插入图片描述
  • 编写测试代码
package com.yuy.poi_tl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
 * http://deepoove.com/poi-tl/#_why_poi_tl
 */
import com.deepoove.poi.XWPFTemplate;

public class Poitl_Test {
	public static void main(String[] args) throws FileNotFoundException, IOException, URISyntaxException {
		 // 获取当前类的资源URL
        URL resourceUrl = Poitl_Test.class.getResource("");
        System.out.println(resourceUrl);
        if (resourceUrl != null) {
            // 转换URL为File路径
            File file = Paths.get(resourceUrl.toURI()).toFile();
            System.out.println("template.docx的绝对路径: " + file.getAbsolutePath());
            //创建数据map
            Map<String, Object> dataMap=new HashMap<String, Object>();
            dataMap.put("title", "Hi, poi-tl Word模板引擎");
            //放入数据集,进行渲染
            XWPFTemplate template = XWPFTemplate.compile(file.getAbsolutePath()+"/testPoitl.docx").render(dataMap);
            //写出
            template.writeAndClose(new FileOutputStream("F:\\output.docx"));
        } else {
            System.out.println("找不到资源文件template.docx");
        }
        
        
	}
}

  • 添加日志 log4j2.xml(必须)
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>
  • 查看测试结果
    在这里插入图片描述

小结

多看官方文档,好使的很

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值