Java codeMap dataMap = new HashMap();
getData(dataMap);
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
// 模板是放在com.havenliu.document.template包下面
configuration.setClassForTemplateLoading(this.getClass(),
"/com/havenliu/document/template");
Template t = null;
try {
// test.ftl为要装载的模板
t = configuration.getTemplate("test.xml");
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File("D:/files/outfile.doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile),"utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}