第一步,引入poi-tl.jar
这是一个开源的world模板引擎jar包
我使用的Apache POI是4.1.2 所以对应jar包版本就是1.10.1
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.10.1</version>
</dependency>
第二步,创建一个world模板 并且在里面写上{{name}}

第三步,引入工具类(使用到了其他的工具类 不影响总体使用 如果你没有这些工具类的话 就替换下代码)
public class WorldUtil {
private static final Logger log = LoggerFactory.getLogger(WorldUtil.class);
private XWPFTemplate compile = null; //模板对象
private Object templateData = null; //模板数据 第二步:创建数据,用于把我们模板中的{{}}包裹的变量替换掉,map中保存的key一定要和模板中的变量保持一致
private String templateName = ""; //模板名称 xxx.docx
public WorldUtil(String templateName,Object templateData){
this.templateName = templateName;
this.templateData = templateData;
init();
}
/**
* 初始化
*/
private void init(){
//第一步:读取模板
ConfigureBuilder builder = Configure.builder();
//使用SpringEL表达式 & 参数不存在时不抛出异常
builder.useSpringEL(false);
//生成模板对象
compile = XWPFTemplate.compile("/你的模板文件夹存放地址/" + templateName, builder.build());
//写入World
writeWorld();
}
/**
* 写入World
*/
private void writeWorld()
{
//第三步:把设置的参数写入,并生成新的word文档
compile.render(templateData);
}
/**
* 导出World
*
* @return {@link String 导出后的文件名称}
*/
public String exportWorld(){
try {
//清除模板名称中 "模板" 二字
String templateName = StringUtils.replace(this.templateName, "模板", "");
String filename = UUID.randomUUID().toString() + "_" + templateName;
compile.writeToFile("/导出的文件存放地址/" + filename);
compile.close();
return filename;
}catch (Exception e){
log.error("导出World失败{}", e.getMessage());
throw new UtilException("导出World失败,请联系网站管理员!");
}
}
}
第四步 调用工具类
public static void main(String[] args){
HashMap map = new HashMap();
map.put("name","小碗神的岚切");
new WorldUtil("你刚才创建的模板名称",map).exportWorld();
}
没报错就去你生成文件的文件夹里面找

最后
也可以在模板里面生成表格和图片等等,具体操作直接看官方文档