如何能讓Java生成復(fù)雜Word文檔
客戶要求用程序生成標(biāo)準(zhǔn)的word文檔,要能打印,而且不能變形,以前用過(guò)很多解決方案,都在客戶嚴(yán)格要求下?tīng)奚臒o(wú)比慘烈。
POI讀word文檔還行,寫文檔實(shí)在不敢恭維,復(fù)雜的樣式很難控制不提,想象一下一個(gè)20多頁(yè),嵌套很多表格和圖像的word文檔靠POI來(lái)寫代碼輸出,對(duì)程序員來(lái)說(shuō)比去山西挖煤還慘,況且文檔格式還經(jīng)常變化。
iText操作Excel還行。對(duì)于復(fù)雜的大量的word也是噩夢(mèng)。
直接通過(guò)JSP輸出樣式基本不達(dá)標(biāo),而且要打印出來(lái)就更是慘不忍睹。
Word從2003開(kāi)始支持XML格式,用XML還做就很簡(jiǎn)單了。
大致的思路是先用office2003或者2007編輯好word的樣式,然后另存為xml,將xml翻譯為FreeMarker模板,最后用java來(lái)解析FreeMarker模板并輸出Doc。經(jīng)測(cè)試這樣方式生成的word文檔完全符合office標(biāo)準(zhǔn),樣式、內(nèi)容控制非常便利,打印也不會(huì)變形,生成的文檔和office中編輯文檔完全一樣。
看看實(shí)際效果
首先用office【版本要2003以上,以下的不支持xml格式】編輯文檔的樣式,圖中紅線的部分就是我要輸出的部分:
將編輯好的文檔另存為XML
#p#
再用Firstobject free XML editor將xml中我們需要填數(shù)據(jù)的地方打上FreeMarker標(biāo)記
最后生成的文檔樣式
#p#
主要程序代碼:
- 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() {
 - //要填入模本的數(shù)據(jù)文件
 - Map dataMap=new HashMap();
 - getData(dataMap);
 - //設(shè)置模本裝置方法和路徑,FreeMarker支持多種模板裝載方法??梢灾豷ervlet,classpath,數(shù)據(jù)庫(kù)裝載,
 - //這里我們的模板是放在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里存放的數(shù)據(jù)Key值要與模板中的參數(shù)相對(duì)應(yīng)
 - * @param dataMap
 - */
 - private void getData(Map dataMap)
 - {
 - dataMap.put("author", "張三");
 - dataMap.put("remark", "這是測(cè)試備注信息");
 - List
 - _table1=new ArrayList();
 - Table1 t1=new Table1();
 - t1.setDate("2010-10-1");
 - t1.setText("制定10月開(kāi)發(fā)計(jì)劃內(nèi)容。");
 - _table1.add(t1);
 - Table1 t2=new Table1();
 - t2.setDate("2010-10-2");
 - t2.setText("開(kāi)會(huì)討論開(kāi)發(fā)計(jì)劃");
 - _table1.add(t2);
 - dataMap.put("table1", _table1);
 - List
 - _table2=new ArrayList();
 - for(int i=0;i<5;i++)
 - {
 - Table2 _t2=new Table2();
 - _t2.setDetail("測(cè)試開(kāi)發(fā)計(jì)劃"+i);
 - _t2.setPerson("張三——"+i);
 - _t2.setBegindate("2010-10-1");
 - _t2.setFinishdate("2010-10-31");
 - _t2.setRemark("備注信息");
 - _table2.add(_t2);
 - }
 - dataMap.put("table2", _table2);
 - }
 - }
 
【編輯推薦】
- MVC之父對(duì)“模型-視圖-控制器”的最初定義
 - ASP.NET MVC中很酷的jQuery驗(yàn)證插件
 - MVC開(kāi)發(fā)人員必須擁有的五個(gè)工具
 - 淺談ASP.NET MVC 3中如何使用Model
 - Java MVC框架性能比較
 



















 
 
 







 
 
 
 