戶要求用程序生成標准的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格式】編輯文檔的樣式,圖中紅線的部分就是我要輸出的部分:
將編輯好的文檔另存為XML
再用Firstobject free XML editor【Firstobject free XML editor的使用見這里】將xml中我們需要填數據的地方打上FreeMarker標記【FreeMarker的語法見這里】
最后生成的文檔樣式
主要程序代碼:
- 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);
- }
- }