word模板内容替换

1.pom引入依赖:

java 复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.0.5</version>
</dependency>
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.10.5</version>
</dependency>

2.写工具类

java 复制代码
package  *****;

import com.deepoove.poi.XWPFTemplate;

import java.io.FileOutputStream;
import java.io.IOException;

public class GenerateWordUtils {
    /**
     *
     * @param path 文件模板路径
     * @param fileName 生成的文件名
     * @param param 需要替换的对象
     */
    public static String generateWord(String path,String fileName,Object param) {
        try {
            XWPFTemplate template = XWPFTemplate.compile(path).render(param);
            template.writeAndClose(new FileOutputStream(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }
}

3.调用

例子:

java 复制代码
//相对路径在项目根目录
String path = GenerateWordUtils.generateWord(new String(模板路径path.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8), 文件名fileName, 需替换的结果集resultMap);
//项目路径
String projectPath=System.getProperty("user.dir");
//文件真实路径
String realPath = projectPath+File.separator+path
相关推荐
期待のcode8 小时前
Java的包装类
java·开发语言
李少兄8 小时前
从一篇IDEA笔记开始,我走出了自己的技术创作路
java·笔记·intellij-idea
松涛和鸣8 小时前
41、Linux 网络编程并发模型总结(select / epoll / fork / pthread)
linux·服务器·网络·网络协议·tcp/ip·算法
Linux Huang8 小时前
spring注册组件/服务无效,问题排查
大数据·服务器·数据库·spring
鹿角片ljp8 小时前
力扣26.有序数组去重:HashSet vs 双指针法
java·算法
SweetCode8 小时前
汉诺塔问题
android·java·数据库
Muscleheng8 小时前
Linux安装LibreOffice
linux·运维·服务器
bugtraq20218 小时前
PentestGPT的部署和使用本地大模型的调试记录
linux·服务器·python
p&f°8 小时前
Java面试题(全)自用
java·开发语言
爬山算法8 小时前
Hibernate(9)什么是Hibernate的Transaction?
java·后端·hibernate