Birt 小結
一、報表範本設計環境
1.使用如下URL下載報表的設計環境:
http://download.eclipse.org/birt/downloads/
下載birt-report-framework-2.1.2.zip檔案,解壓縮該檔案到eclipse安裝目錄下。
2.使用如下URL下載 itext-1.3.jar:
http://prdownloads.sourceforge.net/itext/itext-1.3.jar
3.複製itext-1.3.jar到eclipse/plugins/com.lowagie.itext_*/lib目錄下
二、報表的運行環境
1. 在http://download.eclipse.org/birt/downloads/ 中下載運行環境:birt-runtime-2.1.2.zip
解壓縮birt-runtime-2.1.2.zip,複製birt-runtime-2_1_2/ReportEngine/lib下的所有檔案到WEB應用中WEB-INF/lib/目錄下,將下載好的itext-1.3.jar也複製到該目錄下。
2.使用Report API運行報表要設定EngineHome,代碼如下:
EngineConfig config = new EngineConfig();
config.setEngineHome( "BirtHomePath" );
其中BirtHomePath就是/birt-runtime-2_1_2/ReportEngine目錄
3.在server配置中選擇Application標籤,將WEB應用的classloader mode的值修改為:PARENT_LAST,入圖:
4.參考代碼片斷:
EngineConfig config = new EngineConfig();
config.setEngineHome( "E:/ReportEngine" );
ReportEngine engine = new ReportEngine( config );
if(engine==null){
System.out.println("null");
}
IReportRunnable design=null;
try {
design =
engine.openReportDesign(
"E:/workspace/TestReport/testreport.rptdesign");
} catch (EngineException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFileName("c:/customers.pdf");
options.setOutputFormat("pdf");
task.setRenderOption(options);
try {
task.run();
} catch (EngineException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
engine.destroy();
三、動態配置報表資料來源
1.選擇Data Explore標籤下Data Sources選項,選擇其中需要配置的資料來源
2.選擇beforeOpen事件,輸入如下參考代碼:
importPackage( Packages.java.io );
importPackage( Packages.java.util );
fin = new java.io.FileInputStream(new String("e:/config.txt"));
props = new java.util.Properties( );
props.load(fin);
extensionProperties.odaURL = new String(props.getProperty("url"));
extensionProperties.odaDriverClass = new String(props.getProperty("driver"));
extensionProperties.odaUser = new String(props.getProperty("userid"));
extensionProperties.odaPassword = new String(props.getProperty("password"));
fin.close();
在e:/config.txt寫入資料來源配置資訊,例如:
url=jdbc:mysql://localhost:3306/test
driver=com.mysql.jdbc.Driver
userid=root
password=111111
3.以上代碼中 資料來源設定檔的路徑可以作為參數傳入。修改如下:
fin = new java.io.FileInputStream(new String(params["propdir"]));
在報表檔案中添加報表參數 propdir
四、向報表中傳遞參數
1.設定資料集的查詢參數(用於SQL語句的參數)
雙擊需要編輯的資料集,編寫SQL語句:如圖:
SQL語句中用。設定了查詢參數,雙擊資料集裡的Parameters選項添加參數,如圖:
添加參數面板中的Linked To Report Parameter是指資料集的查詢參數是否要和報表參數對應起來。一般選擇和定義好的報表參數對應。
2. 如果報表中定義了3個參數(pcity,ppname,propdir),如圖:
使用BIRT提供的API寫如下代碼:
IReportRunnable design=null;
try {
design = engine.openReportDesign( "E:/workspace/TestReport/testreport.rptdesign");
} catch (EngineException e) {