Java生成word/pdf解決方案

首先介紹幾種java導出word方案css

一、Jacob是Java-COM Bridge的縮寫,它在Java與微軟的COM組件之間構建一座橋樑。使用Jacob自帶的DLL動態連接庫,並經過JNI的方式實現了在Java平臺上對COM程序的調用。DLL動態連接庫的生成須要windows平臺的支持。html

二、 Apache POI包括一系列的API,它們能夠操做基於MicroSoft OLE 2 Compound Document Format的各類格式文件,能夠經過這些API在Java中讀寫Excel、Word等文件。他的excel處理很強大,對於word還侷限於讀取,目前只能實現一些簡單文件的操做,不能設置樣式。java

三、 Java2word是一個在java程序中調用 MS Office Word 文檔的組件(類庫)。該組件提供了一組簡單的接口,以便java程序調用他的服務操做Word 文檔。
這些服務包括: 打開文檔、新建文檔、查找文字、替換文字,插入文字、插入圖片、插入表格,在書籤處插入文字、插入圖片、插入表格等。填充數據到表格中讀取表格數據 ,1.1版加強的功能: 指定文本樣式,指定表格樣式。如此,則可動態排版word文檔。linux

四、 iText操做Excel還行。對於複雜的大量的word也是噩夢。用法很簡單, 可是功能不多, 不能設置打印方向等問題。apache

五、 JSP輸出樣式基本不達標,並且要打印出來就更是慘不忍睹。windows

六、 用XML作就很簡單了。Word從2003開始支持XML格式,大體的思路是先用office2003或者2007編輯好word的樣式,而後另存爲xml,將xml翻譯爲FreeMarker模板,最後用java來解析FreeMarker模板並輸出Doc。經測試這樣方式生成的word文檔徹底符合office標準,樣式、內容控制很是便利,打印也不會變形,生成的文檔和office中編輯文檔徹底同樣。api


看過方案後就知道了 第 6 種方案效果好點,下面咱們就舉個例子介紹下這個方案函數

首先要製做模板 模板裏的須要傳入的數據用${data} 填充,在代碼裏給其賦值便可測試

模板製做好後 ,點擊另存爲  xml  文件  如:data.xmlthis

若是xml須要動態填充  能夠用

<!-- 循環開始 -->
< #list personnelView as e>

</#list>

<!-- 循環結束 -->

<!-- 循環開始 -->
<#list personnelView as e>  // personnelView爲循環集合
<w:tr wsp:rsidR="001E023B" wsp:rsidTr="004E45BD">
<w:tblPrEx>
<w:tblCellMar>
<w:top w:w="0" w:type="dxa"/>
<w:bottom w:w="0" w:type="dxa"/>
</w:tblCellMar>
</w:tblPrEx>
<w:trPr>
<w:trHeight w:val="405"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="1920" w:type="dxa"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:pPr>
<w:jc w:val="center"/>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e_index +1}</w:t> //e_index 爲索引從0 開始
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1083" w:type="dxa"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_1}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1142" w:type="dxa"/>
<w:gridSpan w:val="3"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:pPr>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_2}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1578" w:type="dxa"/>
<w:gridSpan w:val="3"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_3}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1168" w:type="dxa"/>
<w:gridSpan w:val="2"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_4}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="2181" w:type="dxa"/>
<w:gridSpan w:val="3"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_5}</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</#list>
<!-- 循環結束 -->

下面是代碼

package com.asiainfo.boms.machineroom.action;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;


import org.apache.struts2.ServletActionContext;


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 Template getTemplate(){
configuration.setClassForTemplateLoading(this.getClass(),
"/com/asiainfo/boms/machineroom/template");
Template t = null;
try {
// test.ftl爲要裝載的模板
t = configuration.getTemplate("sg.xml");
t.setEncoding("utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return t;
}

public Writer getWriter(String jobId){
// 輸出文檔路徑及名稱
String savePath = ServletActionContext.getServletContext().getRealPath("/");
File file = new File(savePath+"upload");
if(!file.exists()){
file.mkdirs();
}
File outFile = new File(savePath+"upload/machine"+jobId+".doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
return out;
}


public void createDoc(Template t,Map dataMap,Writer out) {
try {
t.process(dataMap, out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();


} catch (IOException e) {


e.printStackTrace();
}


}


// private void getData(Map dataMap) {
//
// dataMap.put("title_name", "用戶信息");
//
// dataMap.put("user_name", "李四");
//
// dataMap.put("org_name", "微軟公司");
//
// dataMap.put("dept_name", "事業部");
//
// }


}


public String personnelImportWord() throws Exception{
String path = ServletActionContext.getServletContext().getRealPath("/");
String jobId = getStringParameter("job_id");
Map<String, Object> paraMap = new HashMap<String, Object>();
Map dataMap = new HashMap();
paraMap.put("jobId", jobId);
DocumentHandler dh = new DocumentHandler();
Template t = dh.getTemplate();
Writer out = dh.getWriter(jobId);
Map queryWFjobObj = .............
dataMap.put("COLUMN_1", queryWFjobObj.get("COLUMN_1"));
dataMap.put("COLUMN_2", queryWFjobObj.get("COLUMN_2"));
dataMap.put("COLUMN_3", queryWFjobObj.get("COLUMN_3"));
dataMap.put("CLOB_1", queryWFjobObj.get("CLOB_1"));
dataMap.put("DATE_1", queryWFjobObj.get("DATE_1"));
dataMap.put("DATE_2", queryWFjobObj.get("DATE_2"));
dataMap.put("COLUMN_4", queryWFjobObj.get("COLUMN_4"));
Map mobileObj = .................
dataMap.put("CALLER", mobileObj.get("CALLER"));
dataMap.put("STARTDATE", mobileObj.get("STARTDATE"));
dataMap.put("MSISDN", mobileObj.get("MSISDN"));
Map machineroomObj = .......................
dataMap.put("CALLER1", machineroomObj.get("CALLER"));
dataMap.put("STARTDATE1", machineroomObj.get("STARTDATE"));
dataMap.put("MSISDN1", machineroomObj.get("MSISDN"));
List<Map> personnelView = .........................
dataMap.put("personnelView", personnelView);
dh.createDoc(t, dataMap, out);
fileName = "machine"+jobId+".doc";

return "personnelImportWord";
}

public InputStream getInputStream() throws Exception
     {
String path = ServletActionContext.getServletContext().getRealPath("/");
File file = new File(path+"upload");
if(!file.exists()){
file.mkdirs();
}
        return new FileInputStream(path+"upload/"+fileName);
     }


下面再說幾種導出pdf 的方案

java生成pdf方案總結

1. Jasper Report生成pdf:設計思路是先生成模板,而後獲得數據,最後將二者整合獲得結果。可是Jasper Report的問題在於,其生成模板的方式過於複雜,即便有IDE的幫助,咱們仍是須要對其中的衆多規則有所瞭解才行,不然就會給調試帶來極大的麻煩。

2. openoffice生成pdf:openoffice是開源軟件且能在windows和linux平臺下運行。

3. itext + flying saucer生成pdf:itext和flying saucer都是免費開源的,且與平臺無關,結合css和velocity技術,能夠很好的實現。

通常使用第三種方案比較多,它實現的步驟是很是簡單的。

JAVA生成word優缺點對比

所用技術

優勢

缺點

Jacob

功能強大

代碼量大,設置樣式繁瑣;須要windows平臺支持,沒法跨平臺

Apache POI

讀寫excel功能強大、操做簡單

通常只用它讀取word,可以建立簡單的word,不能設置樣式,功能太少

Java2word

功能強大,操做簡單

能知足通常要求,不支持07格式,國人開發的,參考資料較多,須要windows平臺支持

iText

功能全,能知足通常要求

不能直接生成或操做doc文檔,只能生成rtf格式的文檔,rtf也能夠用word打開

JSP

操做簡單,代碼量少

能把當前頁面導出簡單的word,不能設置樣式,美觀性差,沒法操做word

XML(最佳)

代碼量少,樣式、內容容易控制,打印不變形,徹底符合office標準

須要提早設計好word模板,把須要替換的地方用特殊標記標出來

JAVA生成pdf優缺點對比

所用技術

優勢

缺點

openoffice

自己就是office軟件,很容易設計一些文檔模板,支持java調用實現word轉換成pdf

須要先安裝,設計好pdf模板樣式,而後用程序來填充那些預留好的變量

itext

能知足要求,自己提供了一些api

沒法識別不少html的tag和attribute,沒法識別css,須要用其api函數來設置樣式

Jasper Report

能知足要求,市面上使用的比較多,相關文檔多

複雜,很難徹底掌握,須要先設計模板,強依賴於IDE進行可視化編輯

flying sauser(最佳)

能解析html和css輸出成image、pdf等格式,操做簡單,api強大

須要編寫freemarker或velocity模板,打造html,勾畫pdf的樣式

相關文章
相關標籤/搜索