2008年4月28日 星期一

iReportJasperreportsample

要在jsp上產生pdf 檔 ,
第一步是安裝iReport , 然後編輯報表檔.

第二步使用iReport編譯生成報表名.jasper檔

然後在jsp上讀取.jasper然後生成 *.pdf

jsp sample

<%@ page contentType="text/html;charset=BIG5"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%
/* 連線部分 */
Connection conn=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:pg","pg","pgusr");

/* 取得 *.jasper 檔案的路徑, 並轉換成 String 物件 */
File inputFile = new File(application.getRealPath("AlanTest.jasper"));
String filePath = inputFile.getPath();

/* 設定輸出 PDF 檔的路徑位置 */
String outPath = application.getRealPath("BBB.pdf");
//String outPath = application.getRealPath("AlanTest.jasper") "C:\\BBB.pdf";
/* 帶參數進去給報表 */
Map map = new HashMap();
map.put("name","王小明");

/* 把報表轉換成 PDF, 給定 ( 輸入路徑, 輸出路徑, 參數, 連線 ) */
JasperRunManager.runReportToPdfFile(filePath, outPath, map, con);

/* 連線關閉 */
con.close();

%>
<% out.println("讀取生產報表");%>
如果產生的PDF裏面中文字是空白, 要做以下的設定去處理
去下載iTextAsian.jar
將iTextAsian.jar copy 至 iReport 及tomcat中的Lib中

要顯示中文的文字欄位屬性修改:
Font name:選用新細明體
PDF font name: 選用MHei-Medium
最下面的PDF Embeded要打勾
PDF encoding換用有(Chinese Traditional)的
這樣設置後就能產生正確無誤的中文PDF檔了。



下面談到很多安裝步驟,但其實是不需要使用jasperreports-2.0.5. 及裝ant的,
只要裝iReport 就可以了。

1. 下載 iReport-2.0.5
一個window執行檔, 在視寫介面下可以無痛安裝,但要使用java jdk 1.6以上版本,

iReport官方網站:http://ireport.sourceforge.net
iReport函式庫下載:http://0rz.net/8e0zZ

2. 下載 JasperReport
放到C槽底下
JasperReports官方網站:http://jasperreports.sourceforge.net
JasperReports函式庫下載:http://0rz.net/f10xQ

iTextAsian.jar函式庫下載:http://0rz.net/fe0to

3. 下載 Ant 工具

放到C槽底下, 需要指定CLASSPATH

4. 安裝
目錄解壓縮後就算安裝完成了, 第一步要start Server.
先開啟一個 console ,然後到 C:\jasperreports\demo\hsqldb 下

輸入以下指令:
# ant runServer

看到以下訊息:
Buildfile: build.xml

runServer:
[java] server.properties not found, using command line or default properties
[java] Opening database: test
[java] HSQLDB server 1.7.1 is running
[java] Use SHUTDOWN to close normally. Use [Ctrl]+[C] to abort abruptly
[java] Mon Apr 07 16:22:21 CST 2008 Listening for connections ...

很好!跑起來了。再另外開一個 console 介面:
再到隨便一個範例中,擺放範例的目錄C:\jasperreports\demo\samples,
到 barbecue 看看
# cd C:\jasperreports\demo\samples\barbecue

給他 ant -p 一下,看看目前 build.xml 有定義了哪些 task 可以玩:
# ant –p

Buildfile: build.xml
Shows how barcodes could be included in reports using specilized barcode APIs.
Main targets:

clean Deletes all the generated files.
compile Compiles the XML report design and produces the .jasper file.
csv Generates a CSV version of the report by converting the .jrprint file.

fill Fills the compiled report design with data and produces the .jrprint file.

html Generates an HTML version of the report by converting the .jrprint file.

javac Compiles the java source files of the test application.

jxl Generates an XLS version of the report by converting the .jrprint file using the JExcelApi library.

odt Generates an ODT version of the report by converting the .jrprint file.

pdf Generates a PDF version of the report by converting the .jrprint file.

print Sends the report in the .jrprint file to the printer.

rtf Generates an RTF version of the report by converting the .jrprint file.

run Generates a PDF file by filling the .jasper report design. No intermediate .jrprint file is produced.

view Launches the report viewer to preview the report stored in the .jrprint file.

viewDesign Launches the design viewer to preview the compiled report design.

viewDesignXML Launches the design viewer to preview the XML report design.

viewXML Launches the report viewer to preview the generated report stored in an XML file.

xls Generates an XLS version of the report by converting the .jrprint file using the POI library.

xml Generates an XML version of the report by converting the .jrprint file.

xmlEmbed Generates an XML version of the report by converting the .jrprint file.

Default target: javac

嘿嘿,還真多項目啊,每個項目都有說明,看看就大概知道他在幹嘛了。預設 task 是 javac。
好吧,繼續照說明書走吧。

# ant javac
因為預設項目是 javac
所以要直接下 ant 省略 javac 也行。

# ant compile

Buildfile: build.xml

compile:



來吧,把該塞進去的資料塞一塞:
# ant fill

看一看成品
# ant view

看一看還沒塞資料的時候的羞澀模樣
# ant viewDesign

把成品轉成 pdf 的型式看看
# ant run

把剛剛產生的東西都清光光
# ant clean


這篇文章是參考一個很棒的原創網站 http://blog.xuite.net/wildwindjen/engineer而寫的,若
要看全文請到這個網站參觀http://blog.xuite.net/wildwindjen/engineer。

沒有留言: