FreeMarker生成word文件入门


 

废话不多少了,如果是单纯的打印报表,而且客户要求不高的话建议用网页输出,直接打印,因为操作比较简单嘛。

今天主要讨论下用FreeMarker如何输出word。

因为FreeMarker是用 模版+数据=word的方法,所以

1.建立模版

首先word把表格提前做好,红色字体为需要填写的内容,然后另存为xml格式文件。

然后用xml编辑器打开这个文件,把刚才红色字体部分的内容用FreeMarker指令元素替换一下,我用的是这个(后来发现用eclipse也可以,让我费了半天劲,这个编辑器真心没有eclipse好用啊),现在的性别扽sex修改为${sex},类似el表达式,还比较好理解。

修改完成之后用word打开看一下:如下

如果打不开说明你的文件格式有问题

然后把文件的扩展名xml改为ftl,例如,ceshi.xml修改完成之后为ceshi.ftl

到此,模版ceshi.ftlok

2.准备数据

我就直接用map代替了。使用时直接getDate()

  public Map getDate(){

       Map rootMap = new HashMap();

       rootMap.put("name","小明");

       rootMap.put("age","10");

       rootMap.put("sex","男");

       rootMap.put("title","小明的简历");

       return rootMap;

    }


 

  3.写程序输出word

首先你需要一个freemarker.jar,添加到你的项目里

 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.HashMap;

import java.util.Map;

 

import freemarker.template.Configuration;

import freemarker.template.DefaultObjectWrapper;

import freemarker.template.Template;

import freemarker.template.TemplateException;

 

publicclass Test {

 

    /**

     *输出文件

     *@throwsIOException

     *@throwsTemplateException

     *        为了看的清楚直接抛出异常了,实际开发中应该处理的

     */

    publicvoid create()throws IOException, TemplateException {

       //这个是存储freeMarker应用级设置的核心部分,也是处理创建和缓存预解析模版的工作

       Configuration cfg = newConfiguration();

       //这里我们的模板是放在template包下面

       cfg.setClassForTemplateLoading(this.getClass(),"/template");

       //指定模版如何检索数据模型,只是一个高级的主题了,但是可以先这样来用:--具体什么意思

       cfg.setObjectWrapper(new DefaultObjectWrapper());

       //设定编码,如果不设置输出的word则不正常

       cfg.setDefaultEncoding("UTF-8");

 

       // ceshi.ftl为要装载的模板

       Template template = cfg.getTemplate("ceshi.ftl");

       

       //输出文件

       Writer out = null;

       out = new OutputStreamWriter(new FileOutputStream("d:/ceshi.doc"),

              "utf-8");

 

       template.process(getDate(), out);

       out.flush();

       out.close();

 

       System.out.println("输出完成");

    }

 

    /**

     *该方法相当于获取数据模型;注意getDate里存放的数据Key值要与模板中的参数相对应

     *@returnrootMap

     */

    public Map getDate() {

       Map rootMap = new HashMap();

       rootMap.put("name","小明");

       rootMap.put("age","10");

       rootMap.put("sex","男");

       rootMap.put("title","小明的简历");

       return rootMap;

    }

 

    publicstaticvoid main(String[] args) {

 

       try {

           new Test().create();

       } catch (IOException e) {

           //TODO Auto-generated catch block

           e.printStackTrace();

       } catch (TemplateException e) {

           //TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

 

}

 


 

测试结果

 

入门的话就这么多,详细的可以参考FreeMarker手册,这个有中文的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值