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

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

Java 专栏收录该内容
27 篇文章 0 订阅

  原文链接: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代码:


     
     
  1. @Test
  2. public void test(){
  3. Map<String,Object> dataMap = new HashMap<String, Object>();
  4. try {
  5. //编号
  6. dataMap.put( "id", "123456");
  7. //日期
  8. dataMap.put( "date", new SimpleDateFormat( "yyyy年MM月dd日").format( new SimpleDateFormat( "yyyy-MM-dd").parse( "2018-09-19")));
  9. //附件张数
  10. dataMap.put( "number", 1);
  11. //受款人
  12. dataMap.put( "payee", "张三");
  13. //付款用途
  14. dataMap.put( "use_of_payment", "test");
  15. //大写金额
  16. dataMap.put( "capitalization_amount", MoneyUtils.change( 100.20));
  17. //小写金额
  18. dataMap.put( "lowercase_amount", "100");
  19. //Configuration 用于读取ftl文件
  20. Configuration configuration = new Configuration( new Version( "2.3.0"));
  21. configuration.setDefaultEncoding( "utf-8");
  22. /**
  23. * 以下是两种指定ftl文件所在目录路径的方式,注意这两种方式都是
  24. * 指定ftl文件所在目录的路径,而不是ftl文件的路径
  25. */
  26. //指定路径的第一种方式(根据某个类的相对路径指定)
  27. // configuration.setClassForTemplateLoading(this.getClass(), "");
  28. //指定路径的第二种方式,我的路径是C:/a.ftl
  29. configuration.setDirectoryForTemplateLoading( new File( "c:/"));
  30. //输出文档路径及名称
  31. File outFile = new File( "D:/报销信息导出.doc");
  32. //以utf-8的编码读取ftl文件
  33. Template template = configuration.getTemplate( "报销信息导出.ftl", "utf-8");
  34. Writer out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(outFile), "utf-8"), 10240);
  35. template.process(dataMap, out);
  36. out.close();
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. }

注意:我使用的 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的字符串: 


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

 



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


     
     
  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. }

 


注意: 该代码需要用到 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类 


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



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


     
     
  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. Configuration configuration = new Configuration();
  15. configuration.setDefaultEncoding( "utf-8");
  16. configuration.setDirectoryForTemplateLoading( new File( "C:/"));
  17. File outFile = new File( "D:/test.doc");
  18. Template t = configuration.getTemplate( "c.ftl", "utf-8");
  19. Writer out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(outFile), "utf-8"), 10240);
  20. t.process(dataMap, out);
  21. out.close();
  22. }


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

四, 常见问题解决方案 
4.1, 异常信息如下:


     
     
  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. "(" ...



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

 

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

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


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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值