超详细 Java使用freemarker模板技术导出word

来源:互联网 发布:mysql 序号 编辑:程序博客网 时间:2024/06/05 05:13

       搜索了很多freemarker模板技术的文章,发现对于新手来说,都不太详细的博客,方法是可以直接使用,但就是有时候不知道如何入手,我总结了一下前辈的东西,把详细的步骤贴出来:

       步骤一:首先准备一个word,里面有你想要输出来的格式,比如复杂的表格或者很多其他的东西,我的word原始文件如下,现在想要从数据库里面获取姓名name填充到这个word当中:


       步骤二: 当准备好word格式文件,点击另存为,保存为word的xml格式,如下:

    

               使用一些ue或者其他文本工具打开,我用的是ue,会在关键节点上有颜色区分,这样能找到你需要替换的节点,一开始的格式很乱,你可以直接百度"xml 格式化",在线格式化一下你的xml文件:

  


       步骤三:当xml格式统一好后,找到你需要替换的"xxx",使用freemarker的特殊格式,使用"${}"来代替你即将从数据库获取的name,这样就能自动给你填充数据,如下:


          替换了之后,点击保存,然后修改后缀名为ftl,放到工程目录中.如下:

           步骤四:java代码中,填充name的数据,也就是${name},然后替换上自己的ftl模板文件,如下,点击运行代码,就可以在d盘生成word文件:

            最终效果如下:


              就两个java文件如下Main.java:

package com.havenliu.document;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;public class Main {/** * @param args * @throws UnsupportedEncodingException */public static void main(String[] args) throws UnsupportedEncodingException {// DocumentHandler dh=new DocumentHandler();// dh.createDoc();// MyTest mt=new MyTest();// mt.createDoc();Map<String, Object> dataMap = new HashMap<String, Object>();dataMap.put("name", "你好邱林和");MDoc mdoc = new MDoc();mdoc.createDoc(dataMap, "D:/outFile.doc");System.out.println("模板生成成功");}}

       MDoc.java:

 

package com.havenliu.document;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 MDoc {private Configuration configuration = null;public MDoc() {configuration = new Configuration();configuration.setDefaultEncoding("utf-8");}public void createDoc(Map<String, Object> dataMap, String fileName) throws UnsupportedEncodingException {// dataMap 要填入模本的数据文件// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,// 这里我们的模板是放在com.havenliu.document.template包下面configuration.setClassForTemplateLoading(this.getClass(), "/com/havenliu/document/template");Template t = null;try {// test.ftl为要装载的模板t = configuration.getTemplate("ceshifreemaker.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();}}}

             自此大功告成,感谢这些前辈:http://blog.csdn.net/kkdelta/article/details/7220252,其他的我就不贴图了.  

             测试工程下载链接:Java导出word的freemarker测试例子

         


原创粉丝点击