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

编程入门 行业动态 更新时间:2024-10-18 14:25:06

java导出word之freemarker导出(详细<a href=https://www.elefans.com/category/jswz/34/1771193.html style=教程)"/>

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

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

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

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


2, 将该word文件另存为xml格式(注意是另存为,不是直接改扩展名) 
         3, 将xml文件的扩展名直接改为ftl 

         4, 用java代码完成导出(需要导入freemarker.jar) 

@Test  public void exportSimpleWord() throws Exception{  // 要填充的数据, 注意map的key要和word中${xxx}的xxx一致  Map<String,String> dataMap = new HashMap<String,String>();  dataMap.put("username", "张三");  dataMap.put("sex", "男");  //Configuration用于读取ftl文件  Configuration configuration = new Configuration();  configuration.setDefaultEncoding("utf-8");  /*以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是 * 指定ftl文件所在目录的路径,而不是ftl文件的路径 */  //指定路径的第一种方式(根据某个类的相对路径指定)  //configuration.setClassForTemplateLoading(this.getClass(),"");  //指定路径的第二种方式,我的路径是C:/a.ftl  configuration.setDirectoryForTemplateLoading(new File("C:/"));  // 输出文档路径及名称  File outFile = new File("D:/test.doc");  //以utf-8的编码读取ftl文件  Template t =  configuration.getTemplate("a.ftl","utf-8");  Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);  t.process(dataMap, out);  out.close();  }  

或者如下这段代码(本人亲测可用):

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

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


原文来自:




更多推荐

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

本文发布于:2024-02-26 19:30:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1703638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:教程   详细   java   word   freemarker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!