1、依赖
org.freemarker
freemarker
2.3.28
2、
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
Map<String, Object> dataMap = new HashMap<String, Object>();
//编号
dataMap.put(“id”, “123456”);
//日期
try {
dataMap.put(“date”, new SimpleDateFormat(“yyyy年MM月dd日”).format(new SimpleDateFormat(“yyyy-MM-dd”).parse(“2018-09-19”)));
} catch (ParseException e) {
e.printStackTrace();
}
//附件张数
dataMap.put(“number”, 1);
//受款人
dataMap.put(“payee”, “张三”);
//付款用途
dataMap.put(“use_of_payment”, “test”);
//大写金额
dataMap.put(“capitalization_amount”, “壹佰元”);
//小写金额
dataMap.put(“lowercase_amount”, “100”);
//Configuration 用于读取ftl文件
Configuration configuration = new Configuration(Configuration.getVersion());
configuration.setDefaultEncoding(“utf-8”);
try {
configuration.setDirectoryForTemplateLoading(new File(“D:\test”));
Template template = configuration.getTemplate(“报销信息导出.ftl”);
FileWriter out = new FileWriter(new File(“D:\test\test1.doc”));
try {
template.process(dataMap,out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}