[java]代码库
public static void exportToDocumentTemplate(String path, String templateName, File outFile, Map<String, Object> map) {
Configuration cfg = new Configuration();
Template template = null;
Writer out = null;
try {
cfg.setDirectoryForTemplateLoading(new File(path));
cfg.setDefaultEncoding("UTF-8");
template = cfg.getTemplate(templateName);
template.setEncoding("UTF-8");
out = new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8");
template.process(map, out);
} catch (Exception e) {
throw new ExportDocumentException(e);
} finally {
out.flush();
out.close();
}
}
public invoke() {
String path = ServletActionContext.getServletContext().getRealPath("/");
String docName = "要生成的文档名.doc";
String templateName = "刚才保存好了的模版文件名.ftl";
File documentFile = new File(path + docName);
Map<String, Object> data = new HashMap<String, Object>();
data.put("content", "这里是要填充到模版里的内容。。。");
exportToDocumentTemplate(path, templateName, documentFile, data);
}
//源代码片段来自云代码http://yuncode.net