详情请参考https://www.cnblogs.com/zhangxiaozhen/p/10495034.html
亲测好用
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
package com.bean; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.HashMap; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class Test { public static void main(String[] args) throws IOException, TemplateException { //1.创建配置类 Configuration configuration = new Configuration(Configuration.getVersion()); //2.设置模板所在的目录 configuration.setDirectoryForTemplateLoading(new File("E:/CanDelete/demo/src/main/resources")); //C:Eclipse_Workspacejttx_recordsrcmain esources est.ftl //3.设置字符集 configuration.setDefaultEncoding("utf-8"); //4.加载模板 Template template = configuration.getTemplate("tt.ftl"); //5.创建数据模型 Map map=new HashMap(); map.put("name", "甄士隐 "); map.put("age","12"); map.put("sex","男"); //6.创建 Writer 对象 Writer out =new FileWriter(new File("G:\tt.doc")); //7. template.process(map, out); //8.关闭 Writer 对象 out.close(); } }