使用Java生成word文档(附源码)

当咱们使用Java生成word文档时,一般首先会想到iTextPOI,这是由于咱们习惯了使用这两种方法操做Excel,天然而然的也想使用这种生成word文档。可是当咱们须要动态生成word时,一般不只要可以显示word中的内容,还要可以很好的保持word中的复杂样式。这时若是再使用ITextPOI去操做,就比如程序员去搬砖同样痛苦。java

 

  这时候,咱们应该考虑使用FreeMarker的模板技术快速实现这个复杂的功能,让程序员在喝咖啡的过程当中就把问题解决。实现思路是这样的:先建立一个word文档,按照需求在word中填好一个模板,而后把对应的数据换成变量${},而后将文档保存为xml文档格式,使用文档编辑器打开这个xml格式的文档,去掉多余的xml符号,使用Freemarker读取这个文档而后替换掉变量,输出word文档便可。程序员

  具体过程以下:app

  1.建立带有格式的word文档,将该须要动态展现的数据使用变量符替换。编辑器

 

140726908.png

2. 将刚刚建立的word文档另存为xml格式。this

140752129.png

 

3编辑这个XMl文档去掉多余的xml标记,如图中蓝色部分spa

 

140826202.png

 

 

  4.从Freemarker官网【下载】最新的开发包,将freemarker.jar拷贝到本身的开发项目中。.net

  5.新建DocUtil类,实现根据Doc模板生成word文件的方法code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.favccxx.secret.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
public class DocUtil {
        privateConfiguration configure = null ;
        publicDocUtil(){
               configure= new Configuration();
               configure.setDefaultEncoding( "utf-8" );
        }
        /**
         * 根据Doc模板生成word文件
         * @param dataMap Map 须要填入模板的数据
         * @param fileName 文件名称
         * @param savePath 保存路径
         */
        publicvoid createDoc(Map<String, Object> dataMap, String downloadType, StringsavePath){
               try {
                      //加载须要装填的模板
                      Templatetemplate  = null ;
                      //加载模板文件
                      configure.setClassForTemplateLoading( this .getClass(), "/com/favccxx/secret/templates" );
                      //设置对象包装器
                      configure.setObjectWrapper(newDefaultObjectWrapper());
                      //设置异常处理器
                      configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
                      //定义Template对象,注意模板类型名字与downloadType要一致
                      template= configure.getTemplate(downloadType + ".xml" );
                      //输出文档
                      FileoutFile = new File(savePath);
                      Writerout = null ;
                      out= new BufferedWriter( new OutputStreamWriter( new FileOutputStream(outFile), "utf-8" ));                                   
                      template.process(dataMap,out);
                      outFile.delete();
               } catch (Exception e) {
                      e.printStackTrace();
               }
        }
}

  6.用户根据本身的须要,调用使用getDataMap获取须要传递的变量,而后调用createDoc方法生成所须要的文档。xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
  * 根据下载类型获取须要传递的Map参数
  * @param oid 对象Id
  * @param downloadType 下载类型
  */
private Map<String, Object> getDataMap(String oid,String downloadType){
     Map<String, Object> dataMap = new HashMap<String, Object>();
     if ( "Parameter1" .equals(downloadType)){
         ...
         ...
         dataMap = DataMapUtil.setObjToMap(Object1);
     } else {
         ...
         ...
         dataMap = DataMapUtil.setObjToMap(Object2);
     }
     return dataMap;
}

  

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class DataMapUtil {
       
     private static Map<String, Object> dataMap = new HashMap<String, Object>();
       
     /**
      * 将对象转换成Map
      * @param obj 对象类
      * @return
      */
     public static Map<String,Object> setObjToMap(Object obj){
         Class c;
         try {
             c = Class.forName(obj.getClass().getName());
             Method[] methods = c.getMethods();
             for ( int i= 0 ,l=methods.length;i<l;i++){
                 String method = methods[i].getName();
                 System.out.println( "The method is:" + method);
                 if (method.startsWith( "get" )){
                     Object value = methods[i].invoke(obj);
                     if (value != null ){
                         if (value.getClass().getClassLoader() != null ){  //处理自定义的对象类型
                             setObjToMap(value);
                         }
                         String key = method.substring( 3 );
                         key = key.substring( 0 , 1 ).toLowerCase() + key.substring( 1 );
                         if ( "java.util.Date" .equals(value.getClass().getName())){
                             value = DateUtil.dateToString((Date)value);
                         }
                         dataMap.put(key, value);
                     }
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return dataMap;
     }
}

 

  7.赶忙把这个方法,应用到本身的项目中吧。对象

本文出自 “这我的的IT世界” 博客,请务必保留此出处http://favccxx.blog.51cto.com/2890523/1331115

相关文章
相关标签/搜索