java生成復雜word文檔的完美解決方案

openkk 13年前發布 | 8K 次閱讀 搜索框 云計算 iPhone 4S

客戶要求用程序生成標準的word文檔,要能打印,而且不能變形,以前用過很多解決方案,都在客戶嚴格要求下犧牲的無比慘烈。

POI讀word文檔還行,寫文檔實在不敢恭維,復雜的樣式很難控制不提,想象一下一個20多頁,嵌套很多表格和圖像的word文檔靠POI來寫代碼輸出,對程序員來說比去山西挖煤還慘,況且文檔格式還經常變化。

iText操作Excel還行。對于復雜的大量的word也是噩夢。

直接通過JSP輸出樣式基本不達標,而且要打印出來就更是慘不忍睹。

Word從2003開始支持XML格式,用XML還做就很簡單了。

大致的思路是先用office2003或者2007編輯好word的樣式,然后另存為xml,將xml翻譯為FreeMarker模板,最后用java來解析FreeMarker模板并輸出Doc。經測試這樣方式生成的word文檔完全符合office標準,樣式、內容控制非常便利,打印也不會變形,生成的文檔和office中編輯文檔完全一樣。

看看實際效果:

首先用office【版本要2003以上,以下的不支持xml格式】編輯文檔的樣式,圖中紅線的部分就是我要輸出的部分: doc1

將編輯好的文檔另存為XML
doc2

再用Firstobject free XML editor【Firstobject free XML editor的使用見這里將xml中我們需要填數據的地方打上FreeMarker標記【FreeMarker的語法見這里
doc3

最后生成的文檔樣式
doc4
主要程序代碼:

package com.havenliu.document;

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 DocumentHandler { private Configuration configuration = null;

public DocumentHandler() {
    configuration = new Configuration();
    configuration.setDefaultEncoding("utf-8");
}

public void createDoc() {
    //要填入模本的數據文件
    Map dataMap=new HashMap();
    getData(dataMap);
    //設置模本裝置方法和路徑,FreeMarker支持多種模板裝載方法。可以重servlet,classpath,數據庫裝載,
    //這里我們的模板是放在com.havenliu.document.template包下面
    configuration.setClassForTemplateLoading(this.getClass(), "/com/havenliu/document/template");
    Template t=null;
    try {
        //test.ftl為要裝載的模板
        t = configuration.getTemplate("test.ftl");
    } catch (IOException e) {
        e.printStackTrace();
    }
    //輸出文檔路徑及名稱
    File outFile = new File("D:/temp/outFile.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();
    }
}

/**
 * 注意dataMap里存放的數據Key值要與模板中的參數相對應
 * @param dataMap
 */
 private void getData(Map dataMap)
  {
      dataMap.put("author", "張三");
      dataMap.put("remark", "這是測試備注信息");
      List

_table1=new ArrayList ();

      Table1 t1=new Table1();
      t1.setDate("2010-10-1");
      t1.setText("制定10月開發計劃內容。");
      _table1.add(t1);

      Table1 t2=new Table1();
      t2.setDate("2010-10-2");
      t2.setText("開會討論開發計劃");
      _table1.add(t2);

      dataMap.put("table1", _table1);

      List

_table2=new ArrayList (); for(int i=0;i<5;i++) { Table2 _t2=new Table2(); _t2.setDetail("測試開發計劃"+i); _t2.setPerson("張三——"+i); _t2.setBegindate("2010-10-1"); _t2.setFinishdate("2010-10-31"); _t2.setRemark("備注信息"); _table2.add(_t2); } dataMap.put("table2", _table2); } } </pre>轉自:http://www.havenliu.com/java/514.html

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!