java 根据模板文件生成word文档

  原文链接:https://blog.csdn.net/zhangzhangjava/article/details/52937159

 

最近需要做一个导出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代码:

@Test
    public void test(){
        Map<String,Object> dataMap = new HashMap<String, Object>();
        try {
            //编号
            dataMap.put("id", "123456");
            //日期
            dataMap.put("date", new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse("2018-09-19")));
            //附件张数
            dataMap.put("number", 1);
            //受款人
            dataMap.put("payee", "张三");
            //付款用途
            dataMap.put("use_of_payment", "test");
            //大写金额
            dataMap.put("capitalization_amount", MoneyUtils.change(100.20));
            //小写金额
            dataMap.put("lowercase_amount", "100");
            //Configuration 用于读取ftl文件
            Configuration configuration = new Configuration(new Version("2.3.0"));
            configuration.setDefaultEncoding("utf-8");

            /**
             * 以下是两种指定ftl文件所在目录路径的方式,注意这两种方式都是
             * 指定ftl文件所在目录的路径,而不是ftl文件的路径
             */
            //指定路径的第一种方式(根据某个类的相对路径指定)
//                configuration.setClassForTemplateLoading(this.getClass(), "");

            //指定路径的第二种方式,我的路径是C:/a.ftl
            configuration.setDirectoryForTemplateLoading(new File("c:/"));

            //输出文档路径及名称
            File outFile = new File("D:/报销信息导出.doc");

            //以utf-8的编码读取ftl文件
            Template template = configuration.getTemplate("报销信息导出.ftl", "utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
            template.process(dataMap, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

注意:我使用的 freemarker 版本为2.3.28,Configuration对象不推荐直接new Configuration(),仔细看Configuration.class文件会发现,推荐的是 Configuration(Version incompatibleImprovements) 这个构造方法,具体这个构造方法里面传的就是Version版本类,而且版本号不能低于2.3.0

 

 


      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的字符串: 

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

 



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

     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类 

     public class User {  
        private String a;  
        private String b;  
        private String c;  
    //生成set和get方法,此处省略  
    }  



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

     @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, 异常信息如下:

     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导出(注意编码问题,防止中文乱码) 

     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();  

 

  • 46
    点赞
  • 275
    收藏
    觉得还不错? 一键收藏
  • 41
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 41
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值