新聞中心
客戶要求用程序生成標(biāo)準(zhǔn)的word文檔,要能打印,而且不能變形,以前用過很多解決方案,都在客戶嚴(yán)格要求下犧牲的無比慘烈。

成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括白堿灘網(wǎng)站建設(shè)、白堿灘網(wǎng)站制作、白堿灘網(wǎng)頁制作以及白堿灘網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,白堿灘網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到白堿灘省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
POI讀word文檔還行,寫文檔實(shí)在不敢恭維,復(fù)雜的樣式很難控制不提,想象一下一個(gè)20多頁,嵌套很多表格和圖像的word文檔靠POI來寫代碼輸出,對程序員來說比去山西挖煤還慘,況且文檔格式還經(jīng)常變化。
iText操作Excel還行。對于復(fù)雜的大量的word也是噩夢。
直接通過JSP輸出樣式基本不達(dá)標(biāo),而且要打印出來就更是慘不忍睹。
Word從2003開始支持XML格式,用XML還做就很簡單了。
大致的思路是先用office2003或者2007編輯好word的樣式,然后另存為xml,將xml翻譯為FreeMarker模板,最后用java來解析FreeMarker模板并輸出Doc。經(jīng)測試這樣方式生成的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ù)庫裝載,
- //這里我們的模板是放在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ù)相對應(yīng)
- * @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月開發(fā)計(jì)劃內(nèi)容。");
- _table1.add(t1);
- Table1 t2=new Table1();
- t2.setDate("2010-10-2");
- t2.setText("開會(huì)討論開發(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("測試開發(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);
- }
- }
文章名稱:如何能讓Java生成復(fù)雜Word文檔
文章地址:http://www.5511xx.com/article/ccedocs.html


咨詢
建站咨詢
