当前位置: 移动技术网 > IT编程>开发语言>Java > Java用freemarker导出word实用示例

Java用freemarker导出word实用示例

2019年07月22日  | 移动技术网IT编程  | 我要评论

浮云神仙道,陈首林,红色机尾在线观看

最近一个项目要导出word文档,折腾老半天,发现还是用freemarker的模板来搞比较方便省事,现总结一下关键步骤,供大家参考,这里是一个简单的试卷生成例子。

一、模板的制作

先用word做一个模板,如下图:
(注意,上面是有表格的,我设置了边框不可见)然后另存为xml文件,之后用工具打开这个xml文件,有人用firstobject xml editor感觉还不如notepad++,我这里用notepad++,主要是有高亮显示,和元素自动配对,效果如下:
上面黑色的地方基本是我们之后要替换的地方,比如xytitle替换为${xytitle},对已表格要十分注意,比如选择题下面的表格,我们可以通过<w:tr>查找来定位,一对<w:tr></w:tr>代表一行,也就是一条记录(一道题),我们这里要用一对<#list></#list>来将其包括,以便后续填充数据,具体可参照freemarker页面语法,例如这里选择题,我们是两行为一条记录,所以要<#list></#list>要包括两行,形如:<#list table1 as plan1><w:tr>题号 题目</w:tr><w:tr>选项</w:tr></#list>,然后在这其中找着对应的xzn,xztest,ans1,ans2,ans3,ans4替换为${plan1.xzn},${plan1.xztest},${plan1.ans1},${plan1.ans2},${plan1.ans3},${plan1.ans4},注意这里的table1及plan1命名,table1后续填充数据要用到,其他的替换同理操作,得到效果如下:
保存后,修改后缀名为ftl,至此模板制作完毕。

二、编程实现

这里用到了freemarker-2.3.13.jar包,代码如下:

package common; 
 
import java.io.bufferedwriter; 
import java.io.file; 
import java.io.filenotfoundexception; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.outputstreamwriter; 
import java.io.unsupportedencodingexception; 
import java.io.writer; 
import java.util.map; 
 
import freemarker.template.configuration; 
import freemarker.template.template; 
import freemarker.template.templateexception; 
 
public class documenthandler { 
 
 private configuration configuration = null; 
 
 public documenthandler() { 
  configuration = new configuration(); 
  configuration.setdefaultencoding("utf-8"); 
 } 
 
 public void createdoc(map<string,object> datamap,string filename) throws unsupportedencodingexception { 
  //datamap 要填入模本的数据文件 
  //设置模本装置方法和路径,freemarker支持多种模板装载方法。可以重servlet,classpath,数据库装载, 
  //这里我们的模板是放在template包下面 
  configuration.setclassfortemplateloading(this.getclass(), "/template"); 
  template t=null; 
  try { 
   //test.ftl为要装载的模板 
   t = configuration.gettemplate("fctestpaper.ftl"); 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } 
  //输出文档路径及名称 
  file outfile = new file(filename); 
  writer out = null; 
  fileoutputstream fos=null; 
  try { 
   fos = new fileoutputstream(outfile); 
   outputstreamwriter owriter = new outputstreamwriter(fos,"utf-8"); 
   //这个地方对流的编码不可或缺,使用main()单独调用时,应该可以,但是如果是web请求导出时导出后word文档就会打不开,并且包xml文件错误。主要是编码格式不正确,无法解析。 
   //out = new bufferedwriter(new outputstreamwriter(new fileoutputstream(outfile))); 
    out = new bufferedwriter(owriter); 
  } catch (filenotfoundexception e1) { 
   e1.printstacktrace(); 
  } 
   
  try { 
   t.process(datamap, out); 
   out.close(); 
   fos.close(); 
  } catch (templateexception e) { 
   e.printstacktrace(); 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } 
   
  //system.out.println("---------------------------"); 
 } 
} 

然后是准备数据调用就行,代码如下:

package com.havenliu.document; 
 
import java.io.unsupportedencodingexception; 
import java.util.arraylist; 
import java.util.hashmap; 
import java.util.list; 
import java.util.map; 
 
public class main { 
 
 /** 
  * @param args 
  * @throws unsupportedencodingexception 
  */ 
 public static void main(string[] args) throws unsupportedencodingexception {; 
 
  map<string, object> datamap = new hashmap<string, object>(); 
  datamap.put("xytitle", "试卷"); 
  int index = 1; 
  // 选择题 
  list<map<string, object>> list1 = new arraylist<map<string, object>>();//题目 
  list<map<string, object>> list11 = new arraylist<map<string, object>>();//答案 
  index = 1; 
  for (int i = 0; i < 5; i++) { 
 
   map<string, object> map = new hashmap<string, object>(); 
   map.put("xzn", index + "."); 
   map.put("xztest", 
     "( )操作系统允许在一台主机上同时连接多台终端,多个用户可以通过各自的终端同时交互地使用计算机。"); 
   map.put("ans1", "a" + index); 
   map.put("ans2", "b" + index); 
   map.put("ans3", "c" + index); 
   map.put("ans4", "d" + index); 
   list1.add(map); 
 
   map<string, object> map1 = new hashmap<string, object>(); 
   map1.put("fuck", index + "."); 
   map1.put("abc", "a" + index); 
   list11.add(map1); 
 
   index++; 
  } 
  datamap.put("table1", list1); 
  datamap.put("table11", list11); 
 
  // 填空题 
  list<map<string, object>> list2 = new arraylist<map<string, object>>(); 
  list<map<string, object>> list12 = new arraylist<map<string, object>>(); 
  index = 1; 
  for (int i = 0; i < 5; i++) { 
 
   map<string, object> map = new hashmap<string, object>(); 
   map.put("tkn", index + "."); 
   map.put("tktest", 
     "操作系统是计算机系统中的一个___系统软件_______,它管理和控制计算机系统中的___资源_________."); 
   list2.add(map); 
 
   map<string, object> map1 = new hashmap<string, object>(); 
   map1.put("fill", index + "."); 
   map1.put("def", "中级调度" + index); 
   list12.add(map1); 
 
   index++; 
  } 
  datamap.put("table2", list2); 
  datamap.put("table12", list12); 
 
  // 判断题 
  list<map<string, object>> list3 = new arraylist<map<string, object>>(); 
  list<map<string, object>> list13 = new arraylist<map<string, object>>(); 
  index = 1; 
  for (int i = 0; i < 5; i++) { 
 
   map<string, object> map = new hashmap<string, object>(); 
   map.put("pdn", index + "."); 
   map.put("pdtest", 
     "复合型防火墙防火墙是内部网与外部网的隔离点,起着监视和隔绝应用层通信流的作用,同时也常结合过滤器的功能。"); 
   list3.add(map); 
 
   map<string, object> map1 = new hashmap<string, object>(); 
   map1.put("judge", index + "."); 
   map1.put("hij", "对" + index); 
   list13.add(map1); 
 
   index++; 
  } 
  datamap.put("table3", list3); 
  datamap.put("table13", list13); 
 
  // 简答题 
  list<map<string, object>> list4 = new arraylist<map<string, object>>(); 
  list<map<string, object>> list14 = new arraylist<map<string, object>>(); 
  index = 1; 
  for (int i = 0; i < 5; i++) { 
 
   map<string, object> map = new hashmap<string, object>(); 
   map.put("jdn", index + "."); 
   map.put("jdtest", "说明作业调度,中级调度和进程调度的区别,并分析下述问题应由哪一级调度程序负责。"); 
   list4.add(map); 
 
   map<string, object> map1 = new hashmap<string, object>(); 
   map1.put("answer", index + "."); 
   map1.put("xyz", "说明作业调度,中级调度和进程调度的区别,并分析下述问题应由哪一级调度程序负责。"); 
   list14.add(map1); 
 
   index++; 
  } 
  datamap.put("table4", list4); 
  datamap.put("table14", list14); 
 
  mdoc mdoc = new mdoc(); 
  mdoc.createdoc(datamap, "e:/outfile.doc"); 
 } 
 
} 

注意上面map中的key必须和模板中的对应,否则会报错。效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网