1.freemaker - 图2

2.实现程序

  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStreamWriter;
  7. import java.io.UnsupportedEncodingException;
  8. import java.io.Writer;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import freemarker.template.Configuration;
  14. import freemarker.template.Template;
  15. import freemarker.template.TemplateException;
  16. /**
  17. * 使用freemark生成word
  18. *
  19. */
  20. public class Freemark {
  21. public static void main(String[] args){
  22. Freemark freemark = new Freemark("template/");
  23. freemark.setTemplateName("wordTemplate.ftl");
  24. freemark.setFileName("doc_"+new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date())+".doc");
  25. freemark.setFilePath("bin\\doc\\");
  26. //生成word
  27. freemark.createWord();
  28. }
  29. private void createWord(){
  30. Template t = null;
  31. try {
  32. //获取模板信息
  33. t = configuration.getTemplate(templateName);
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. File outFile = new File(filePath+fileName);
  38. Writer out = null;
  39. try {
  40. out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
  41. } catch (UnsupportedEncodingException e) {
  42. e.printStackTrace();
  43. } catch (FileNotFoundException e) {
  44. e.printStackTrace();
  45. }
  46. Map map = new HashMap<String, Object>();
  47. map.put("name", "蒙奇·D·路飞");
  48. map.put("country", "日本");
  49. map.put("city", "东京");
  50. map.put("time",new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date()));
  51. try {
  52. //输出数据到模板中,生成文件。
  53. t.process(map, out);
  54. out.close();
  55. } catch (TemplateException e) {
  56. e.printStackTrace();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. /**
  62. * freemark初始化
  63. * @param templatePath 模板文件位置
  64. */
  65. public Freemark(String templatePath) {
  66. configuration = new Configuration();
  67. configuration.setDefaultEncoding("utf-8");
  68. configuration.setClassForTemplateLoading(this.getClass(),templatePath);
  69. }
  70. /**
  71. * freemark模板配置
  72. */
  73. private Configuration configuration;
  74. /**
  75. * freemark模板的名字
  76. */
  77. private String templateName;
  78. /**
  79. * 生成文件名
  80. */
  81. private String fileName;
  82. /**
  83. * 生成文件路径
  84. */
  85. private String filePath;
  86. public String getFileName() {
  87. return fileName;
  88. }
  89. public void setFileName(String fileName) {
  90. this.fileName = fileName;
  91. }
  92. public String getFilePath() {
  93. return filePath;
  94. }
  95. public void setFilePath(String filePath) {
  96. this.filePath = filePath;
  97. }
  98. public String getTemplateName() {
  99. return templateName;
  100. }
  101. public void setTemplateName(String templateName) {
  102. this.templateName = templateName;
  103. }
  104. }

3.程序运行后,会在bin的doc目录下生成doc文件,效果图
4.

if指令

if指令进行条件输出控制,类似程序语言中的if语句。
格式:
<#if condition>content</#if> <#if condition>content<#else>other content</#if>

list指令

list指令用来遍历集合中的内容。
格式:
<_#list vars as var> repeat content </#list>_
5.

assign指令

assign指令用来定义一个变量并给其赋值,或者替换原有变量的值
格式:
<#assign name=value [name1=value1…]>
<#assign name>capture this</#assign>