效果图:
实现步骤:
1:将已编排好格式的word文档另存为xml.
2:用nodpad++等工具的xml插件格式化xml文档。
3:编写freemark标签:
4:响应文件流
TemplateParam genData = new TemplateParam(temlateName);
genData.setDataMap(dataMap);//数据map
genData.setGenFileName("xxxxx.doc");//文件名
FreemarkerUtils.generateFile(genData, SystemUtils.getResponse());
FreemarkerUtils类:
public class FreemarkerUtils {
public static void main(String[] args) {
// 生成模板,resource/templates包
TemplateParam templateParam = new TemplateParam("testgen.txt");
// 生成的文件名
templateParam.setGenFileName("测试freemarker文件生成.txt");
// 生成的本地路径,response写出方式可不填
templateParam.setGenFilePath("D:\\");
// 生成的参数,也可以templateParam.setDataMap(dataMap);
templateParam.append("param1", "这是参数1").append("param2", "这是参数2");
// 生成文件
generateFile(templateParam);
}
private static String encodeing = "utf-8";
public static void generateFile(TemplateParam genData) {
// 获取输出
try (Writer out = getFileWriter(genData.getGenFilePath(), genData.getGenFileName())) {
// 获取模板
Template template = getFremarkerTemplate(genData.getTemplateName());
// 写出文件
template.process(genData.getDataMap(), out);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void generateFile(TemplateParam genData, HttpServletResponse response) {
// 获取输出
try (Writer out = getResponseWriter(response, genData.getGenFileName())) {
// 获取模板
Template template = getFremarkerTemplate(genData.getTemplateName());
// 写出文件
template.process(genData.getDataMap(), out);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Template getFremarkerTemplate(String templateName) throws IOException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setTemplateLoader(new ClassTemplateLoader(ClassUtils.getDefaultClassLoader(), "templates"));
Template template = cfg.getTemplate(templateName, encodeing);
template.setOutputEncoding(encodeing);
return template;
}
private static OutputStreamWriter getResponseWriter(HttpServletResponse response, String respnseFileName) {
try {
OutputStream outputStream = response.getOutputStream();
OutputStreamWriter out = new OutputStreamWriter(outputStream, encodeing);
response.addHeader("Content-type", "application");
response.setContentType("application/ms-html");
response.setCharacterEncoding(encodeing);
response.setHeader("Content-Disposition", "attachment;filename=" + new String(respnseFileName.getBytes("GB2312"), "ISO_8859_1"));
response.flushBuffer();
return out;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static Writer getFileWriter(String genFilePath, String genFileName) throws IOException {
// 创建根目录
File file = new File(genFilePath);
if (!file.exists()) {
file.mkdirs();
}
// 输出文档
File outFile = new File(genFilePath + File.separator + genFileName);
// 清掉老文件
if (outFile.exists()) {
outFile.delete();
}
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), encodeing));
}
public static class TemplateParam {
private String genFilePath;
private String genFileName;
private String templateName;
private Context dataMap = new Context();
public TemplateParam(String templateName) {
this.templateName = templateName;
}
public String getGenFilePath() {
if (DrinStringUtils.isNull(genFilePath)) {
throw new LocalException("生成文件路径不能为空");
}
return genFilePath;
}
public void setGenFilePath(String genFilePath) {
this.genFilePath = genFilePath;
}
public String getGenFileName() {
if (DrinStringUtils.isNull(genFileName)) {
throw new LocalException("生成文件名不能为空");
}
return genFileName;
}
public void setGenFileName(String genFileName) {
this.genFileName = genFileName;
}
public String getTemplateName() {
if (DrinStringUtils.isNull(templateName)) {
throw new LocalException("模板名称不能为空");
}
return templateName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
public Context getDataMap() {
if (DrinStringUtils.isNull(dataMap) || dataMap.isEmpty()) {
throw new LocalException("模板数据不能为空");
}
return dataMap;
}
public void setDataMap(Context dataMap) {
this.dataMap = dataMap;
}
public TemplateParam append(String key, Object value) {
this.dataMap.put(key, value);
return this;
}
}
}