java导出word之freemarker导出(详细教程)

来源:互联网 发布:纵队 知乎 编辑:程序博客网 时间:2024/05/16 14:47

最近需要做一个导出word的功能, 在网上搜索了一下,发现了各种方法,但是在这个过程中遇到不少问题,各种报错,各种调试,先整理各种总结,希望能对大家有所帮助,少走弯路。欢迎大家留言交流。同时感谢网络中的大神,帮助我学习进步。

在网上搜了下, 有用POI,JXL,iText等jar生成一个word文件然后将数据写到该文件中,API非常繁琐而且拼出来的样式也不美观,于是选择了另一种方式----feemarker基于word模板的导出方式, 这种方式非常简单而且导出的样式美观, 其原理就是先做一个word模板, 该模板中变量数据用${xxx}这种方式填写, 然后再导出时只需读取模板然后用相应的数据替换其中的${xxx}即可. 

 一,简单模板导出(不含图片, 不含表格循环) 
         1, 新建一个word文档, 输入如下类容: 


2, 将该word文件另存为xml格式(注意是另存为,不是直接改扩展名) 
         3, 将xml文件的扩展名直接改为ftl 
         4, 用java代码完成导出(需要导入freemarker.jar) 

Java代码

  1. @Test  
  2.    public void exportSimpleWord() throws Exception{  
  3.        // 要填充的数据, 注意map的key要和word中${xxx}的xxx一致  
  4.       Map<String,String> dataMap = new HashMap<String,String>();  
  5.       dataMap.put("username""张三");  
  6.       dataMap.put("sex""男");  
  7.             
  8.     //Configuration用于读取ftl文件  
  9.       Configuration configuration = new Configuration();  
  10.       configuration.setDefaultEncoding("utf-8");  
  11.         
  12.       /*以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是 
  13.        * 指定ftl文件所在目录的路径,而不是ftl文件的路径 
  14.        */  
  15.       //指定路径的第一种方式(根据某个类的相对路径指定)  
  16.       //configuration.setClassForTemplateLoading(this.getClass(),"");  
  17.         
  18.       //指定路径的第二种方式,我的路径是C:/a.ftl  
  19.       configuration.setDirectoryForTemplateLoading(new File("C:/"));  
  20.         
  21.         
  22.       // 输出文档路径及名称  
  23.      File outFile = new File("D:/test.doc");  
  24.        
  25.      //以utf-8的编码读取ftl文件  
  26.      Template t =  configuration.getTemplate("a.ftl","utf-8");  
  27.      Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);  
  28.         t.process(dataMap, out);  
  29.         out.close();  
  30.    }  
或者如下这段代码(本人亲测可用):

package com.test.ceshi;
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.Writer;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import freemarker.template.Configuration;  
import freemarker.template.Template;  
import freemarker.template.TemplateException;  
  
public class WordTest {  
      
    private Configuration configuration = null;  
      
    public WordTest(){  
        configuration = new Configuration();  
        configuration.setDefaultEncoding("UTF-8");  
    }  
      
    public static void main(String[] args) {  
        WordTest test = new WordTest();  
        test.createWord();  
    }  
      
    public void createWord(){  
        Map<String,Object> dataMap=new HashMap<String,Object>();  
        getData(dataMap);  
        configuration.setClassForTemplateLoading(this.getClass(), "/com");  //FTL文件所存在的位置  
        Template t=null;  
        try {
//            t = configuration.getTemplate("wordModel.ftl"); //文件名
            t = configuration.getTemplate("myword.ftl"); //文件名
        } catch (IOException e) {
            e.printStackTrace();  
        }
        File outFile = new File("D:/outFilessa"+Math.random()*10000+".doc");  
        Writer out = null;  
        try {  
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));  
        } catch (FileNotFoundException e1) {  
            e1.printStackTrace();  
        }  
           
        try {  
            t.process(dataMap, out);  
        } catch (TemplateException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    private void getData(Map<String, Object> dataMap) {  
        dataMap.put("title", "标题说的太对了");  
        dataMap.put("year", "2012");  
        dataMap.put("month", "2");  
        dataMap.put("day", "13");  
        dataMap.put("auditor", "唐鑫");  
        dataMap.put("phone", "13020265912");  
        dataMap.put("weave", "占文涛");  
//      dataMap.put("number", 1);  
//      dataMap.put("content", "内容"+2);  
          
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();  
        for (int i = 0; i < 10; i++) {  
            Map<String,Object> map = new HashMap<String,Object>();  
            map.put("number", i);  
            map.put("content", "内容"+i);  
            list.add(map);  
        }
        dataMap.put("list", list);  
    }  
}  

 5, 这时在D盘下就生成了一个test.word, 打开可以看到${xxx}已被替换 


 二, word文件中导入图片 
          1, 新建一个word文档, 在要插入图片的地方随便插入一张图片 


          2, 将word另存为xml 
          3, 将xml扩展名改为ftl 
          4, 打开ftl文件, 搜索w:binData 或者 png可以快速定位图片的位置,图片 已经编码成0-Z的字符串了, 如下: 


          5, 将上述0-Z的字符串全部删掉,写上${imgStr}(变量名随便写)后保存 

          6, 导入图片的代码与上述代码是一样的, 也是创建一个Map, 将数据存到map中,只不过我们要把图片用代码进行编码,将其也编成0-Z的字符串: 

Java代码:

  1.  Map<String,String> dataMap = new HashMap<String,String>();  
  2.  dataMap.put("imgStr", getImageStr());  
  3.   
  4. //....其余省略  
 Map<String,String> dataMap = new HashMap<String,String>();
 dataMap.put("imgStr", getImageStr());
//....其余省



这是对图片进行编码的代码: 

  1. public String getImageStr() {  
  2.              String imgFile = "d:/aa.png";  
  3.              InputStream in = null;  
  4.              byte[] data = null;  
  5.              try {  
  6.                  in = new FileInputStream(imgFile);  
  7.                  data = new byte[in.available()];  
  8.                  in.read(data);  
  9.                  in.close();  
  10.              } catch (Exception e) {  
  11.                  e.printStackTrace();  
  12.              }  
  13.              BASE64Encoder encoder = new BASE64Encoder();  
  14.              return encoder.encode(data);  
  15.          }  
public String getImageStr() {
        String imgFile = "d:/aa.png";
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

注意: 该代码需要用到 sun.misc.BASE64Encoder 类,这个类就是JDK中的类,但在eclipse中默认是不访问的,需要设置一下,设置方式: 
项目上右键-->Build Path-->Configure Build Path... 


双击Access rules,点击add, 选择Accessible,下方输入**, OK , 这样就可以访问sun.misc.BASE64Encoder 类了 



   三, 导出循环的表格 

      1, 新建一个word文档, 插入如下表格: 


       2, 另存为xml, 将扩展名改为ftl 

       3, 搜索  w:tr 可以找到行的起点与结束点(注意第一对w:tr 是表头,应找第二对 w:tr), 如图: 


       4, 用<#list userList as user> </#list>标签将第二对 w:tr 标签包围起来(userList是集合的key, user是集合中的每个元素, 类似<c:forEach items='userList' var='user'>), 如图: 


 5, 解析该ftl文件 


这是User类

Java代码 :

  1. public class User {  
  2.     private String a;  
  3.     private String b;  
  4.     private String c;  
  5. //生成set和get方法,此处省略  
  6. }  
public class User {
private String a;
private String b;
private String c;
//生成set和get方法,此处省略
}


这是解析ftl文件的代码,跟上面一样,只是Map的value是一个集合而已

Java代码:

  1. @Test  
  2.    public void exportListWord() throws Exception{  
  3.        //构造数据  
  4.        Map<String,List> dataMap = new HashMap<String,List>();  
  5.        List<User> list = new ArrayList<User>();  
  6.          for(int i=0;i<10;i++){  
  7.              User user = new User();  
  8.              user.setA("a"+(i+1));  
  9.              user.setB("b"+(i+1));  
  10.              user.setC("c"+(i+1));  
  11.              list.add(user);  
  12.          }  
  13.          dataMap.put("userList", list);  
  14.            
  15.          Configuration configuration = new Configuration();  
  16.          configuration.setDefaultEncoding("utf-8");  
  17.          configuration.setDirectoryForTemplateLoading(new File("C:/"));  
  18.          File outFile = new File("D:/test.doc");  
  19.          Template t =  configuration.getTemplate("c.ftl","utf-8");  
  20.          Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);  
  21.          t.process(dataMap, out);  
  22.          out.close();  
  23.    }  
@Test
public void exportListWord() throws Exception{
  //构造数据
  Map<String,List> dataMap = new HashMap<String,List>();
  List<User> list = new ArrayList<User>();
    for(int i=0;i<10;i++){
    User user = new User();
    user.setA("a"+(i+1));
    user.setB("b"+(i+1));
    user.setC("c"+(i+1));
    list.add(user);
    }
    dataMap.put("userList", list);
    
    Configuration configuration = new Configuration();
    configuration.setDefaultEncoding("utf-8");
    configuration.setDirectoryForTemplateLoading(new File("C:/"));
    File outFile = new File("D:/test.doc");
    Template t =  configuration.getTemplate("c.ftl","utf-8");
    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);
        t.process(dataMap, out);
        out.close();
  }

如果你需要输出集合的索引, 用${user_index}即可. 


四, 常见问题解决方案 

4.1, 异常信息如下:

Xml代码:

  1. freemarker.core.ParseException: Encountered "<" at line 3, column 28888 in test.ftl.  
  2. Was expecting one of:  
  3.     <STRING_LITERAL> ...  
  4.     <RAW_STRING> ...  
  5.     "false" ...  
  6.     "true" ...  
  7.     <INTEGER> ...  
  8.     <DECIMAL> ...  
  9.     "." ...  
  10.     "+" ...  
  11.     "-" ...  
  12.     "!" ...  
  13.     "[" ...  
  14.     "(" ...  
freemarker.core.ParseException: Encountered "<" at line 3, column 28888 in test.ftl.
Was expecting one of:
    <STRING_LITERAL> ...
    <RAW_STRING> ...
    "false" ...
    "true" ...
    <INTEGER> ...
    <DECIMAL> ...
    "." ...
    "+" ...
    "-" ...
    "!" ...
    "[" ...
    "(" ...


  这是由于在写${xxx}表达式的时候, xxx与其前方的文字样式不一致, 在另存为xml后你可以搜索一下 "${" , 会发现如下图这种情况: 



由于${xxx}中的xxx格式与其前方的文字不一致, 那么在生成xml时,就会有一些修饰xxx样式的标签,例如修饰xxx的字体,颜色等的标签, 所以在word中看似写的是${xxx}实际上转为xml后变成了${<w:color ...>xxx</w:color>},这样这个el表达式中的标签就解析不了报错了, 可以去掉${}内部的标签只留下xxx或者删掉 "${" 和 "}"然后给xxx加上el表达式都可以解决此问题. 

   五, javaWeb中利用response导出(注意编码问题,防止中文乱码) 

Java代码:

  1. Map<String,String> dataMap = new HashMap<String,String>();  
  2.           dataMap.put("username""张三");  
  3.           dataMap.put("sex""男");  
  4.             
  5.           Configuration configuration = new Configuration();  
  6.           configuration.setDefaultEncoding("utf-8");  
  7.           configuration.setDirectoryForTemplateLoading(new File(request.getRealPath("/")+"/templete"));//指定ftl所在目录,根据自己的改  
  8.           response.setContentType("application/msword");     
  9.           response.setHeader("Content-Disposition""attachment;filename=\"" + new String("文件名.doc".getBytes("GBK"), "iso8859-1") + "\"");  
  10.           response.setCharacterEncoding("utf-8");//此句非常关键,不然word文档全是乱码  
  11.           PrintWriter out = response.getWriter();  
  12.          Template t =  configuration.getTemplate("test.ftl","utf-8");//以utf-8的编码读取ftl文件  
  13.          t.process(dataMap, out);  
  14.          out.close();  
Map<String,String> dataMap = new HashMap<String,String>();
     dataMap.put("username", "张三");
     dataMap.put("sex", "男");
     
     Configuration configuration = new Configuration();
     configuration.setDefaultEncoding("utf-8");
     configuration.setDirectoryForTemplateLoading(new File(request.getRealPath("/")+"/templete"));//指定ftl所在目录,根据自己的改
     response.setContentType("application/msword");   
     response.setHeader("Content-Disposition", "attachment;filename=\"" + new String("文件名.doc".getBytes("GBK"), "iso8859-1") + "\"");
     response.setCharacterEncoding("utf-8");//此句非常关键,不然word文档全是乱码
     PrintWriter out = response.getWriter();
    Template t =  configuration.getTemplate("test.ftl","utf-8");//以utf-8的编码读取ftl文件
         t.process(dataMap, out);
         out.close();



参考文章连接:http://18810098265.iteye.com/blog/2056122

参考文章链接:http://blog.csdn.net/zhanwentao2/article/details/7255432

参考文章连接:http://blog.csdn.net/jackfrued/article/details/39449021

0 0