第十三章 BIRT報表引擎API及報表API

13.1報表引擎詳解,API結構

BIRT包含幾個引擎。一個引擎是一個提供一個特定領域的功能的一套Java API。例如,數據引擎包含管理數據的API,圖表引擎包含生成一個圖表的API。一般而言,BIRT包含四個引擎,報表設計引擎(design Engine),報表引擎(report engine),圖表引擎(chat engine),數據引擎(data engine)。

a) 關於報表設計引擎

報表設計引擎包含驗證一個報表設計文件的有效性和生成一個報表設計文件的API。報表設計引擎被BIRT Report Designer和任何定製的生成一個BIRT報表設計的Java應用程序所使用。生成引擎在創建一個報表文檔時也使用報表設計引擎。報表設計引擎包含使用ROM技術規範來驗證設計文件的元素和結構的有效性的API。

b) 關於報表引擎

報表引擎包含兩部分:生成引擎和展示引擎。BIRT Report Viewer和定製的Java應用程序使用報表引擎來處理一個報表設計並按照設計中指定的格式生成報表。生成引擎包含讀取和解釋一個報表設計的API。生成引擎使用數據引擎來讀取和轉換來自由報表設計標識的數據源。生成引擎的輸出是一個報表文檔,這是一個報表產品中的一箇中間文檔。展示引擎處理由生成引擎創建的報表文檔並按設計中指定的格式產生報表。和生成引擎一樣,展示引擎也使用數據引擎。但是,在展示階段,數據引擎從報表文件而不是數據源中獲取數據。展示引擎它需要的任何一個報表發射器按照設計中指定的格式生成一個報表。BIRT有兩個標準的發射器,HTML和PDF。BIRT也支持不是HTML和PDF格式的定製的發射器。圖表報表項和定製的報表項擴展展示引擎來提供這些報表項的展示功能。

c) 關於圖表引擎

圖標引擎包含生成圖表及將圖表與來自數據源的數據關聯起來的API。圖表引擎的使用不僅限於BIRT應用程序。任何Java程序都可以使用圖表引擎來創建和現實一個圖表。BIRT Report Viewer 解釋報表設計中的圖標設計信息並使用圖表引擎來生成圖表。

d) 關於數據引擎

數據引擎包含獲取和轉換數據的API。當被生成引擎使用時,數據引擎直接從數據源中獲取數據。當被展示引擎使用時,數據引擎從報表文檔中獲取數據。

1) 關於數據引擎組件

數據引擎包含兩個主要組件:數據訪問組件和數據轉換組件。數據訪問組件與ODA框架通信來獲取數據。數據引擎的數據轉換組件執行像排序、分組、累計和過濾來自數據訪問組件的數據的操作。

2) 關於ODA框架

ODA框架管理ODA和本地驅動、裝載驅動、打開連接並管理數據請求。ODA框架使用Eclipse Data Tools Platform項目來管理連接。ODA框架包含擴展點,籍此可以添加一個定製的ODA驅動。數據引擎擴展用於數據源的連接方法和驅動。如果有一個BIRT不支持的數據源就需要一個定製的ODA驅動。創建一個定製的ODA驅動時可能不僅需要擴展數據引擎而且還需要擴展BIRT Report Designer。如果數據源需要一個GUI組件來制定數據集,那麼還需要一個BIRT Report Designer擴展。

 

應用開發者只需要瞭解公用的API,這些API包含在以下的幾個包內:

a) org.eclipse.birt.report.model.api:這個包內包含了93個class,兩個接口和一個exception,這個包是爲那些想要定製report designer的開發者使用的,報表設計引擎包含這個包

b) org.eclipse.birt.report.engine.api:這個包包含了一個類,17個接口和四個exception,是給那些想要定製一個report generator的開發者使用的,報表引擎包含這個包

c) org.eclipse.birt.charthierarchy:這個包是給想定製一個chartgenerator的開發者使用的,圖表引擎包含這個包


BIRT報表引擎(BIRT report engine)在幾個環境提供了報表生成和翻譯服務,包含在以下組件中:

a) Stand-alone engine:這個engine可以讓開發者使用一個命令行程序來從一個report design文件(.rptdesign)中得到Html或PDF格式的report

b) BIRT report viewer:BIRT Report Designer使用它來預覽report。這個程序是一個web程序,它運行在一個Eclipse內嵌的Tomcat中,這個Tomcat中有一個內嵌的report engine

c) Custom report designer with an embedded engine:一個定製的桌面報表應用集成BIRT report engine來預覽報表

d) Web application that embeds the engine:一個類似於BIRT report viewer的Web程序,可以產生基於web的報表。

 

如下圖所示:


另外,用戶還能定製報表引擎,除了通常是集成到一個Web應用或者一個獨立的Java應用程序外,定製的Java報表生成器質性和BIRT報表生成器相同的功能。和BIRT 報表瀏覽器Web應用一樣,定製的Java報表生成器使用報表引擎的API來讀取報表設計文件並生成一個報表。一個定製的業務邏輯來管理安全事宜、控制目錄和確定輸出的格式。

 

Report engine的核心engine運行並實施報表,而將其他的環境相關的任務留給運行該引擎的應用程序,比如:URLconstruction, image storage, and design file caching

BIRT report engine的API可以做以下幾件事情:

a) 發現一組爲報表定義的參數(包括動態參數和級聯參數)

b) 獲得參數的默認值

c) 產生任務(task)實施操作的應用

d)包含1到2個操作階段((RunTask 然後 Render Task 或者RunAndRenderTask)

d) 爲報表產生圖片或chart

e) 產生報表文檔

f) 運行報表產生HTML或PDF格式的輸出



EngineConfig –設置報表引擎可選的參數.

ReportEngine – 用於運行一到多個報表任務

IReportRunnable –用於打開報表設計文檔,並把句柄傳給報表任務

IReportDocument – 用於打開報表文檔,並把句柄傳給報表任務,同時包含很多方法用於獲TOC,數據等等

HTMLRenderContext – 定義展示HTML的環境,例如設置HTML圖片地址,事件處理

PDFRenderContext – 定義展示PDF的環境,例如設置字體的地址,事件處理

HTMLRenderOption – 用於設置html的可選選項,比如導出時的路徑



報表設計引擎(Design Engine)需要完成的事情:

1、用於產生或者修改報表設計文檔,報表模板文檔,報表庫文檔

2、有時候報表引擎需要修改報表設計文檔,這個時候就需要調用報表設計引擎

3、如果報表引擎在運行時,事件機制和腳本機制中本身就包含修改報表設計的代碼,這個時候也要調用報表設計引擎

4、用於獲取和設置報表參數

5、可以用於獲取報表設計的種種屬性和參數


用戶還能定製報表設計器,一個定製的報表設計器是一個由Java開發人員創建的用來基於特定的需求生成一個格式良好的報表設計文件Java應用程序。定製的報表設計器不需要包含用戶界面。定製的報表設計起的一個典型的例子是一個動態確定一個報表的目錄、結構或者數據元的Java應用程序。定製的報表設計器使用和BIRTReport Designer相同的設計引擎API。

圖表引擎用於產生圖表,展示圖表,修改圖表,是一個比較獨立的引擎。通常包含兩個API包:

org.eclipse.birt.chart.model.*,org.eclipse.birt.chart.*


一般報表設計器在產生一份包含圖表的設計文檔的時候,都要調用圖表的設計和展示引擎,但圖表引擎不僅僅包含在報表設計器的類庫中,報表在展示階段如果包含圖表,或者需要修改圖表的時候,也會調用圖表的引擎。


事實上,birt提供的開源產品Report Designer,Example Viewer都包含這三個引擎(報表設計引擎,報表引擎,圖表引擎),所以用戶在自定義設計器,自定義web展示器,自定義獨立運行的展示器的時候,最好也要包含這三類引擎。

構建一個報表的必須工作:

創建和配置報表引擎

單個報表引擎可以通過創建多個設計器來生成多個報表。通過Platform.createFactoryObject()方法創建一個ReportEngine對象。設置BIRT目錄,指向需要的plug-in和庫。

打開報表文檔(*.rptdesign 或 *.rptdocument)
通過ReportEngine對象的openReportDesign( )打開一個報表設計模版,參數是文件路徑名或InputStream,返回一個IReportRunnable對象。
通過ReportEngine對象的openReportDocument( )打開一個報表文檔,參數是文件路徑名,返回一個IReportDocument對象。

連接數據源
通過創建數據連接或使用應用程序提供的連接爲數據集提供數據源。

報表生成的準備工作
用一個IRenderOption對象設置輸出格式,輸出文件名,輸出流或輸出位置,和一些特定格式設置。HTMLRenderOption支持HTML格式的輸出,輸出PDF格式使用RenderOption

生成報表
使用IRunAndRenderTask對象生成IReportRunnable類型的報表,使用IRenderTask對象創建IReportDocument類型的報表。
也可以訪問部署在應用程序服務器上的report viewer servlet來生成報表。

銷燬引擎
當我們不再需要使用報表引擎時將它銷燬。

可選的任務:

獲取參數

如果報表中設置了參數,引擎要使用傳入的參數生成報表,如果沒有傳入參數則使用默認值。

創建報表文檔文件(*.rptdocument)
報表文檔文件已二進制的形式存儲的,如果應用程序使用報表設計文件,要通過IRunTask對象生成一個IReportDocument對象的報表文檔。

從報表文檔導出數據。
使用IDataExtractionTask對象在報表文檔中導出一項或多項的值。


開發環境要求
把需要的類庫都引入classpath或開發工具中。配置BIRT目錄,目錄中包含了從報表設計文件生成報表所需要的plug-in和類庫。 BIRT的報表引擎包在他的ReportEngine子目錄裏提供了完整的BIRT home。如果你想動手修改BIRT源碼的話,一定要保證源碼的版本與BIRT home中的plug-in和類庫的版本是一致的。

類庫和plug-in
BIRT home的ReportEngine\lib子目錄和ReportEngine\plugins中包含了報表應用程序需要的用到的類庫和plug-in,你可以根據自己項目的情況刪除掉不用的包。

jdbc驅動
BIRT home的plugins/org.eclipse.birt.report.data.oda.jdbc_...文件夾用於存放連接數據庫所需要的驅動程序,你可以把需要的驅動程序包放到該目錄下(引入classpath中好像是不起作用的…)

通過api修改報表設計模版
報表應用程序在生成報表前可以修改報表設計模版。如:添加刪除數據項,更深入的定製報表等。DesignEngine API提供了修改設計模版(包括修改腳本)DesignEngine的主要接口在org.eclipse.birt.model.api包中。

下面給出一個簡單的例子:

該報表的設計如下:


數據集上有事件處理綁定:


需要在報表的資源中導入事件處理包:


預覽如下:


報表引擎的源碼如下:

  1. package REAPI;  
  2. import java.util.logging.Level;  
  3. import org.eclipse.birt.core.framework.Platform;  
  4. import org.eclipse.birt.report.engine.api.EXCELRenderOption;  
  5. import org.eclipse.birt.report.engine.api.EngineConfig;  
  6. import org.eclipse.birt.report.engine.api.EngineConstants;  
  7. import org.eclipse.birt.report.engine.api.EngineException;  
  8. import org.eclipse.birt.report.engine.api.HTMLRenderOption;  
  9. import org.eclipse.birt.report.engine.api.IReportEngine;  
  10. import org.eclipse.birt.report.engine.api.IReportEngineFactory;  
  11. import org.eclipse.birt.report.engine.api.IReportRunnable;  
  12. import org.eclipse.birt.report.engine.api.IRunAndRenderTask;  
  13. import org.eclipse.birt.report.engine.api.PDFRenderOption;  
  14.   
  15.   
  16. public class RunAndRenderTask {  
  17.   
  18.     public void runReport() throws EngineException  
  19.     {  
  20.   
  21.         IRunAndRenderTask task=null;  
  22.         IReportEngine engine=null;  
  23.         EngineConfig config = null;  
  24.   
  25.         try{  
  26.             //System.setProperty("java.io.tmpdir", "c:/temp/test/testsampledb");      
  27.             config = new EngineConfig( );             
  28.             config.setLogConfig("d:\\dwn.txt", Level.INFO);  
  29.             config.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, this.getClass().getClassLoader());  
  30.             config.getAppContext().put(EngineConstants.WEBAPP_CLASSPATH_KEY, "d:\\eventjar.jar");             
  31.             config.setBIRTHome("E:\\birt漢化包\\birt-runtime-3_7_2\\ReportEngine");  
  32.             //config.setLogConfig(null, Level.FINEST);  
  33.             Platform.startup( config );  
  34.               
  35.             IReportEngineFactory factory = (IReportEngineFactory) Platform  
  36.             .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );  
  37.             engine = factory.createReportEngine( config );  
  38.           
  39.   
  40.         IReportRunnable design = null;  
  41.         //Open the report design  
  42.           
  43.         design = engine.openReportDesign("d:\\dataseteventhandler.rptdesign");   
  44.         task = engine.createRunAndRenderTask(design);         
  45.         //task.setParameterValue("Top Count", (new Integer(5)));  
  46.         task.validateParameters();  
  47.               
  48.           
  49.         HTMLRenderOption options = new HTMLRenderOption();        
  50.         options.setImageDirectory("d:\\");  
  51.         options.setOutputFileName("d:\\eventhandlerjar.html");  
  52.         options.setOutputFormat("html");  
  53.           
  54.         //PDFRenderOption options = new PDFRenderOption();  
  55.         //options.setOutputFileName("d:\\topn.pdf");  
  56.         //options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");  
  57.         //options.setOutputFormat("pdf");  
  58.           
  59.         //EXCELRenderOption options = new EXCELRenderOption();    
  60.         //options.setOutputFormat("xls");  
  61.         //options.setOutputFileName("d:\\customers.xls");  
  62.         //options.setWrappingText(true);  
  63.           
  64.         task.setRenderOption(options);  
  65.         task.run();  
  66.         task.close();  
  67.         engine.destroy();  
  68.         }catch( Exception ex){  
  69.             ex.printStackTrace();  
  70.         }                 
  71.         finally  
  72.         {  
  73.             if ( !task.getErrors( ).isEmpty( ) )  
  74.   
  75.             {  
  76.   
  77.                 for ( Object e : task.getErrors( ) )  
  78.   
  79.                 {  
  80.   
  81.                     ( (Exception) e ).printStackTrace( );  
  82.   
  83.                 }  
  84.   
  85.             }             
  86.             Platform.shutdown( );  
  87.             System.out.println("Finished");  
  88.         }  
  89.           
  90.     }     
  91.   
  92.   
  93.     /** 
  94.      * @param args 
  95.      */  
  96.     public static void main(String[] args) {  
  97.         try  
  98.         {  
  99.   
  100.             RunAndRenderTask ex = new RunAndRenderTask( );  
  101.             ex.runReport();  
  102.   
  103.         }  
  104.         catch ( Exception e )  
  105.         {  
  106.             e.printStackTrace();  
  107.         }  
  108.     }  
  109. }  

運行,會看到D盤產生eventhandlerjar.html文檔,查看如下:


下面詳細介紹如何構建一個報表應用程序

org.eclipse.birt.report.engine.api包含了用來生成報表的類和接口,其中主要的類和接口有 ReportEngine, EngineConfig, IReportRunnable, IRenderOption以及它的子類,IEngineTask以及它的子類。
Eclipse提供的Birt集成開發工具的幫助文檔裏有詳細的api參考,不過有點簡略。

一、創建報表引擎

報表引擎是ReportEngine類的一個實例,是任何報表應用程序的關鍵部分。首先用EngineConfig對象來爲ReportEngine準備參數,然後用BIRT報表引擎工廠來創建引擎。創建一個配置對象:
EngineConfig config = new EngineConfig();

設置BIRT home
BIRT home就是BIRT plug-ins和類庫的路徑,它是報表引擎的關鍵參數。
你可以用下面的方式進行BIRT home設置:
對於獨立的應用程序可以吐過下面的方式設置:
config.setBIRTHome ( "C:/birt-runtime-<version>/ReportEngine" );
也可以再你自己的環境變量中設置:
set BIRT_HOME="C:\birt-runtime-<version>\ReportEngine"
SET CLASSPATH=%BIRT_HOME%\<required library 1>;
■ Web應用程序裏要首先取得絕對路徑讓後設置到config
config.setBIRTHome( servletContext.getRealPath( "/WEB-INF" ) );
WAR 文件的Web應用程序中可以使用PlatformServletContext.
config.setBIRTHome( "" );
■ Eclipse,通過VM運行參數設置
-DBIRT_HOME="C:\birt-runtime-<version>\ReportEngine"

設置引擎的其他參數
通過配置對象進行配置的參數還有日誌,OSGI(Open Services Gateway Initiative),平臺上下文,資源文件,臨時資源文件,腳本等,這些都是可選的參數,詳細信息可以參考EngineConfigAPI

不同運行環境的差異
應用程序是獨立運行的還是以web形式運行的決定了平臺上下文和HTML發射器(emitter,不知道是不是生成器更合適一點)的配置的不同。 平臺上下文提供了報表引擎連接plug-in的機制,默認使用獨立運行的方式。HTML發射器提供了處理圖片和處理超鏈接和標籤事件的功能。

設置上下文
BIRT是一個基於Eclipse的應用程序,因此它使用OSGI平臺來啓動插件來產生報表和設計引擎。BIRT需要的plug-in存儲在 BIRT主目錄下,通過platform context進行配置,它實現了接口org.eclipse.birt.core.framework.IPlatformContext。 context作爲EngineConfig的一個參數通過setEngineContext( )進行設置。
BIRT中提供了兩個IPlatformContext的實現:
PlatformFileContext通過文件系統目錄配置context,用於獨立運行的應用程序和web應用程序中部署的基於系統文件目錄的BIRT應用程序。
IPlatformContext context = new PlatformFileContext( );
config.setEngineContext( context );
對於web應用上部署的BIRT應用程序,使用PlatformServletContext。它使用基於資源連接的J2EE ServletContext來定位BIRT plug-in。它默認使用“/WEB-INF/platform/”
IPlatformContext context =
new PlatformServletContext(request.getSession().getServletContext());
config.setPlatformContext(context);
如果默認的BIRT提供的實現不能滿足你的要求,可以定義自己的實現。

設置HTML emitter
當使用HTML格式生成一個報表,引擎通過一個HTMLRenderOption對象來定義如何處理HTML emitter使用的資源(圖像濾鏡,圖像元素,圖表,書籤等)。桌面應用程序和web應用程序用來處理圖像的方法是不同的。使用HTML emitter首先要創建HTMLRenderOption,使用方法:
HTMLRenderOption ho = new HTMLRenderOption( );
ho.setImageHandler( new HTMLCompleteImageHandler( ));
config.setEmitterConfiguration( RenderOptionBase.OUTPUT_FORMAT_HTML, ho );
創建引擎時可以不必設置HTML emitter,你可以需要生成HTML報表時再去進行配置。
IHTMLImageHandler是用來處理圖像的接口,BIRT提供了兩個IHTMLImageHandler的實現:
HTMLCompleteImageHander可以將圖像保存到文件目錄。它首先使用HTMLRenderOption設置的圖像目錄,如果沒 有則使用EngineConfig.setTempDir( )設置的臨時文件目錄,如果還沒有則使用java.io.tmpdir定義的系統臨時文件目錄。生成的HTML中引用的圖像也是引擎創建的。
HTMLServerImageHandler應用在web應用程序中,它將圖像文件保存在HTMLRenderOption設置的圖像文目錄 中。HTML的圖像的src指向生成的圖像文件的路徑也是在HTMLRenderOption中配置的。這樣報表引擎就可以在本地文件中創建圖像文件,並 且通過URL共享。
ho.setImageDirectory("output/image");
ho.setBaseImageURL("http://myhost/prependme?image=");
如果IHTMLImageHandler得實現不能滿足你的要求,你可以開發自己的實現。或者繼承並重寫已有的image handlerHTMLCompleteImageHander已經提供了足夠的圖像文件接口因此一般情況下不需要我們進行擴展。

啓動平臺
設置完成平臺上下文環境,你可以通過org.eclipse.birt.core.framework.Platform類啓動一個平臺。 Platform是一個Eclipse OSGI平臺的包裝類,提供一個同步的靜態方法startup( )用來啓動平臺,該操作的開銷比較大,因此你的應用程序最好只調用一次。平臺使用結束後調用Platform.shutdown( )關閉。如果你是在Web應用程序裏使用報表引擎,請在servletinit方法中調用startup方法或者在第一次處理用到平臺的請求時。最好啓 動平臺的代碼封裝爲一個單例。如果你使用Birt提供的Web Viewer或使用基於Eclipse的富客戶端程序(RCP),你就不需要啓動平臺,因爲應用程序已經自己啓動了OSGi


Platform Class的作用

用於啓動所需要的插件

使用 OSGi.

DE API, CE API and the RE API都需要啓動平臺

使用 PlatformConfig 類去配置startup.

DesignConfig and EngineConfig 是擴展自 PlatformConfig.

PlatformContext 類決定了插件(Plugins)的地址

創建Factory用於產生DesignEngine或者ReportEngine


創建報表引擎
BIRT提供一個工廠服務用於創建ReportEngine對象。Platform.createFactoryObject( )創建一個實現了org.eclipse.birt.report.engine.api.IReportEngineFactory接口的工廠對象。該 方法需要一個PlatformConfig對象。因爲EngineConfig是繼承自PlatformConfig,你可以使用 EngineConfig來創建一個工廠。最後通過IReportEngineFactory.createReportEngine( )方法和剛纔使用的EngineConfig對象創建報表引擎。

實例
在獨立運行的應用程序中創建一個報表引擎,如下:

  1. // Create an EngineConfig object.  
  2. EngineConfig config = new EngineConfig( );  
  3. // Set up the path to your BIRT home directory.  
  4. config.setBIRTHome("E:\\birt漢化包\\birt-runtime-3_7_2\\ReportEngine");  
  5. // Explicitly set up the stand-alone application  
  6. IPlatformContext context = new PlatformFileContext( );  
  7. config.setEngineContext( context );  
  8. // Start the platform for a non-RCP application.  
  9. Platform.startup( config );  
  10. IReportEngineFactory factory = ( IReportEngineFactory ) Platform.createFactoryObject  
  11. ( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );  
  12. // Set up writing images or charts embedded in HTML output.  
  13. HTMLRenderOption ho = new HTMLRenderOption( );  
  14. ho.setImageHandler( new HTMLCompleteImageHandler( ));  
  15. config.setEmitterConfiguration( RenderOptionBase.OUTPUT_FORMAT_HTML, ho );  
  16. // Create the engine.  
  17. IReportEngine engine = factory.createReportEngine( config );  

說明:上面一個在windows系統上的獨立運行的應用程序,它使用BIRT run-time提供的BIRT home,報表輸出格式是HTML

  1. 在<span style="font-family:Calibri;">Web</span><span style="font-family:宋體;">應用程序上創建一個報表引擎,如下:</span>  
  1. // Example class to create the report engine  
  2. public class BirtEngine {  
  3. private static IReportEngine birtEngine = null;  
  4. public static synchronized IReportEngine getBirtEngine( ServletContext sc ) {  
  5. if (birtEngine == null) {  
  6. EngineConfig config = new EngineConfig( );  
  7. config.setBIRTHome( "" );  
  8. IPlatformContext context = new PlatformServletContext( sc );  
  9. config.setPlatformContext( context );  
  10. try{  
  11. Platform.startup( config );  
  12. IReportEngineFactory factory = ( IReportEngineFactory )  
  13. Platform.createFactoryObject(   
  14. IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );  
  15. birtEngine = factory.createReportEngine( config );  
  16. }catch ( Exception e ) { e.printStackTrace( ); }  
  17. }  
  18. return birtEngine;  
  19. }  
  20. }  

說明:上面的代碼是一個用單例模式實現的報表引擎的創建的類,EngineConfig配置了兩個參數 BIRTHomePlatformContext

在上面例子的基礎上進行更多的配置

  1. // In a different class, get the report engine  
  2. reportEngine = BirtEngine.getBirtEngine( request.getSession( ).getServletContext( ));  
  3. // Set up the engine  
  4. EngineConfig config = reportEngine.getConfig( );  
  5. HTMLRenderOption ho = new HTMLRenderOption( );  
  6. ho.setImageHandler( new HTMLServerImageHandler( ));  
  7. ho.setImageDirectory("output/image");  
  8. ho.setBaseImageURL("http://myhost/prependme?image=");  
  9. config.setEmitterConfiguration( RenderOptionBase.OUTPUT_FORMAT_HTML, ho );  

使用日誌環境調試應用程序
BIRT報表引擎使用java.util.loggingLoggerLevel類來處理引擎平臺的日誌信息。你在Eclipse中運行一個 應用程序運行信息默認的是顯示在控制檯上的。在非Eclipse環境中運行信息的輸出就要有運行環境決定了。默認的日誌級別是Level.INFO,你可 以通過改變日誌級別來減少系統內部的日誌信息。
使用EngineConfig.setLogConfig( )可以讓日誌輸出到磁盤文件中。該方法需要兩個參數,第一個參數是日誌輸出的目錄,BIRT 引擎會在指定目錄中創建一個名稱格式爲ReportEngine
_YYYY_MM_DD_hh_mm_ss.log的日誌文件。第二個參數是日誌信息的最低級別,級別越高輸出的日誌信息越少。ReportEngine.changeLogLevel( )方法可以讓你修改日誌級別。

如何使用BIRT日誌 如下:

  1. // Set up the location and level of the logging output.  
  2. config.setLogConfig( "C:/Temp", Level.ERROR );  
  3. engine.changeLogLevel( Level.INFO );  

二、打開報表資源文件

BIRT引擎可以通過報表設計文件和報表文檔兩種方式生成報表,引擎還可以從報表設計文件生成報表文檔。
你可以通過openReportDesign( )方法打開一個報表設計文件文件,他實例化一個IReportRunnable對象,支持文件路徑或一個輸入流。
你可以通過openReportDocument( )方法打開一個報表設計文件文件,他實例化一個IReportDocument對象,也支持文件路徑或一個輸入流。
使用這些方法要處理EngineException

理解IReportRunnable對象
IReportRunnable對象提供到報表設計的基本屬性的直接接口。報表設計屬性的屬性名是靜態字符串變量,如:IReportRunnable.AUTHOR。可以通過getProperty( )方法去訪問這些屬性。下面的代碼展示瞭如何連接到報表設計文件:

  1. String designName = "./SimpleReport.rptdesign";  
  2. IReportRunnable runnable = null;  
  3. try {  
  4. runnable = engine.openReportDesign( designName );  
  5. }catch ( EngineException e ) {  
  6. System.err.println( "Design " + designName + " not found!" );  
  7. engine.destroy( );  
  8. System.exit( -1 );  
  9. }  
  10. // Get the value of a simple property.  
  11. String author = ( String ) runnable.getProperty( IReportRunnable.AUTHOR );  

理解IReportDocument對象
IReportDocument對象提供了訪問報表數據和報表結構的接口,它提供了方法來訪問表格內容,書籤,頁面信息等。
使用findTOC( )訪問表格內容,該方法使用TOCNode參數返回一個TOCNode對象。使用null參數就可以訪問根表格。
調用getTOCTree( )返回一個ITOCTree對象獲取更詳細的Table信息。調用TOCNode.getChildren( )方法獲取table的子節點,返回值是一個TOCNode對象列表。
通過TOCNode對象你可以得到表格中的內容和書籤。你可以使用書籤對象作爲參數調用getPageNumber( )方法獲取書籤連接的頁面的頁數。通過這些信息你可以在以格式化的報表中顯示特定的頁面。
下面的代碼展示瞭如何打開一個報表文檔,並在頁面上查找元素:

  1. String dName = "./SimpleReport.rptdocument";  
  2. IReportDocument doc = null;  
  3. try {  
  4. doc = engine.openReportDocument( dName );  
  5. catch ( EngineException e ) {  
  6. System.err.println( "Document " + dName + " not found!" );  
  7. engine.destroy( );  
  8. System.exit( -1 );  
  9. }  
  10. // Get the root of the table of contents.  
  11. TOCNode td = doc.findTOC( null );  
  12. java.util.List children = td.getChildren( );  
  13. long pNumber;  
  14. // Loop through the top level table of contents entries.  
  15. if ( children != null && children.size( ) > 0 ) {  
  16. for ( int i = 0; i < children.size( ); i++ ) {  
  17. // Find the required table of contents entry.  
  18. TOCNode child = ( TOCNode ) children.get( i );  
  19. if ( child.getDisplayString( ).equals( "103" ) ) {  
  20. // Get the number of the page that contains the data.  
  21. pNumber = doc.getPageNumber( child.getBookmark( ) );  
  22. System.out.println( "Page to print is " + pNumber );  
  23. }  
  24. }  
  25. }  

三、用程序處理報表參數

參數是報表生成之前想報表設計文件輸入的報表元素。報表的應用程序可以訪問報表的參數的屬性,最常用的屬性是namevalue,你可以使用參數名或通用編碼來獲取參數。

爲報表設計創建一個參數定義任務
一個IGetParameterDefinitionTask對象提供鏈接到報表設計所有參數的接口。通過調用 ReportEngine.createGetParameterDefinitionTask( )創建一個參數定義對象,使用結束後調用他的close方法關閉。

測試報表設計是否有參數
IGetParameterDefinitionTask.getParameterDefns( )可以測試報表設計是否有參數,它返回一個參數集合,調用集合的isEmpty( )方法可以測試集合中是否有元素。

獲取報表設計的參數
通過IGetParameterDefinitionTask.getParameterDefn( )方法獲取一個已知名字的參數,它返回一個IParameterDefnBase類型對象,也可以調用getParameterDefns( )方法返回一個參數集合。
getParameterDefns( )需要一個boolean參數,如果爲false返回一個未分組的參數集合,如果爲true返回報表設計中定義的參數組。
調用IParameterDefnBase.getParameterType( )可以檢驗參數是否是一個組。如果是一個組則返回IParameterDefnBase.PARAMETER_GROUP,如果是一個級聯參數組則返回 IParameterDefnBase.CASCADING_PARAMETER_GROUP。爲獲取一組報表參數,使用方法 IParameterGroupDefn.getContents( ),它返回一個數據類型是IScalarParameterDefnArrayList對象。

獲取報表參數的默認值
這個任務是可選的。調用IGetParameterDefinitionTask.getDefaultValue( )獲取一個已知參數的默認值,它返回的是一個Object。可以通過調用IScalarParameterDefn.getDataType( )來獲取Object的有效類型,它返回一個IScalarParameterDefn定義的int類型的靜態變量。調用 IGetParameterDefinitionTask.getDefaultValues( )來獲取報表設計的所有參數的默認值,它返回一個HashMap對象存儲了從參數名到默認值的映射。

使用常量列表提供的效值
許多報表參數只接受常量列表的值,這些值可能是靜態常量,也可能是數據庫裏查出來的數據list。使用 IGetParameterDefinitionTask.getSelectionList( )可以返回參數所能接受的參數(IParameterSelectionChoice)的集合,如果集合爲null,則可以接受任何值。 IParameterSelectionChoicegetLabel( )方法返回現實的文本,getValue( )返回值。

獲取每個報表參數的屬性
這個任務是可選的。使用IScalarParameterDefn的方法可以獲取表參數的屬性。應用程序使用屬性生成用戶自定義接口。例如,獲取參數的數據類型使用getDataType( )方法。

爲參數設置值
調用IGetParameterDefinitionTask.setParameterValue( )爲參數設置值。如果你是通過應用程爲日期數值等參數返回一個字符串,要把他們轉化成和本地無關的格式。調用方法 ReportParameterConverter.parse( )方法可以完成這項工作。getParameterValues( )方法返回一個HashMap包含了已經設置的所有參數。

實例
下面的代碼展示瞭如何設置一個已知參數名的參數值:

  1. // Create a parameter definition task.  
  2. IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask( runnable );  
  3. // Instantiate a scalar parameter.  
  4. IScalarParameterDefn param = (IScalarParameterDefn)  
  5. task.getParameterDefn( "customerID" );  
  6. // Get the default value of the parameter. In this case,  
  7. // the data type of the parameter, customerID, is Double.  
  8. int customerID = ((Double) task.getDefaultValue( param )).intValue( );  
  9. // Get a value for the parameter. This example assumes that  
  10. // this step creates a correctly typed object, inputValue.  
  11. // Set the value of the parameter.  
  12. task.setParameterValue( "customerID", inputValue );  
  13. // Get the values set by the application for all parameters.  
  14. HashMap parameterValues = task.getParameterValues( );  
  15. // Close the parameter definition task.  
  16. task.close( );  

下面的代碼展示瞭如何使用報表參數集合,例子中使用ReportParameterConverter將字符串轉化成用戶接口接受的參數格式。

  1. // Create a parameter definition task.  
  2. IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask( runnable );  
  3. // Create a collection of the parameters in the report design.  
  4. Collection params = task.getParameterDefns( false );  
  5. // Get the default values of the parameters.  
  6. HashMap parameterValues = task.getDefaultValues( );  
  7. // Get values for the parameters. Later code in this example  
  8. // assumes that this step creates a HashMap object,  
  9. // inputValues. The keys in the HashMap are the parameter  
  10. // names and the values are those that the user provided.  
  11. // Iterate through the report parameters, setting the values  
  12. // in standard locale-independent format.  
  13. Iterator iterOuter = params.iterator( );  
  14. ReportParameterConverter cfgConverter =  
  15. new ReportParameterConverter( "", Locale.getDefault() );  
  16. while ( iterOuter.hasNext( ) ) {  
  17. IParameterDefnBase param = (IParameterDefnBase) iterOuter.next( );  
  18. String value = (String) inputValues.get( param.getName( ));  
  19. if ( value != null ) {  
  20. parameterValues.put( param.getName( ),  
  21. cfgConverter.parse( value, param.getDataType( ) ) );  
  22. }  
  23. }  
  24. // Close the parameter definition task.  
  25. task.close( );  

使用級聯參數
級聯參數參數是一組可供用戶選擇的參數值的集合。第一個參數的選擇會影響到第二個參數中的值。參數使用一個或多個查詢來從數據集把數據展示給用戶。參數定義任務根據前面的選擇以行爲單位過濾參數組中的數據。
使用報表引擎實現級聯參數,要進行如下工作:
使用IGetParameterDefinitionTask.evaluateQuery( )方法爲級聯參數準備數據,它需要參數group的名字作爲參數。
調用IGetParameterDefinitionTask.getSelectionListForCascadingGroup( )獲取參數組的第一個參數的值,它需要兩個參數,參數名和對象數組,對第一個參數來說這個數組是空的。該方法返回一個 IParameterSelectionChoice集合。
■ 再次調用getSelectionListForCascadingGroup( )方法獲取後面的參數,這次Object[ ]中放的是前面獲取的參數值。

下面的代碼展示瞭如何通過查詢實現級聯參數:

  1. // Create a grouped collection of the design’s parameters.  
  2. Collection params = task.getParameterDefns( true );  
  3. // Iterate through the parameters to find the cascading group.  
  4. Iterator iter = params.iterator( );  
  5. while ( iter.hasNext( ) ) {  
  6. IParameterDefnBase param = (IParameterDefnBase) iter.next();  
  7. if ( param.getParameterType() ==  
  8. IParameterDefnBase.CASCADING_PARAMETER_GROUP ) {  
  9. ICascadingParameterGroup group = (ICascadingParameterGroup) param;  
  10. Iterator i2 = group.getContents( ).iterator( );  
  11. // Run the query for the cascading parameters.  
  12. task.evaluateQuery( group.getName() );  
  13. Object[ ] userValues = new Object[group.getContents( ).size( )];  
  14. // Get the report parameters in the cascading group.  
  15. int i = 0;  
  16. while ( i2.hasNext( ) ) {  
  17. IScalarParameterDefn member = (IScalarParameterDefn) i2.next( );  
  18. // Get the values for the parameter.  
  19. Object[ ] setValues = new Object[i];  
  20. if ( i > 0 ) System.arraycopy( userValues, 0, setValues, 0, i );  
  21. Collection c = task.getSelectionListForCascadingGroup(  
  22. group.getName(),setValues );  
  23. // Iterate through the values for the parameter.  
  24. Iterator i3 = c.iterator();  
  25. while ( i3.hasNext( ) ) {  
  26. IParameterSelectionChoice s =  
  27. ( IParameterSelectionChoice ) i3.next( );  
  28. String choiceValue = s.getValue( );  
  29. String choiceLabel = s.getLabel( );  
  30. }  
  31. // Get the value for the parameter from the list of  
  32. // choices. This example does not provide the code for  
  33. // this task.  
  34. userValues[i] = inputChoiceValue;  
  35. i++;  
  36. }  
  37. }  
  38. }  

四、報表生成的準備工作

BIRT支持HTML, Adobe PDF, Adobe PostScript (PS), Microsoft Excel (XLS), Microsoft PowerPoint (PPT)和 Microsoft Word (DOC) 格式的報表生成器。你也可以通過擴展支持自定義的格式。

有三個task類用來支持從源文件生成報表,就是:

■ IRunAndRenderTask. 通過運行報表設計文件直接生成目標格式的報表。調用方法ReportEngine.createRunAndRenderTask( )可以創建這個對象。

■ IRunTask. 通過報表設計文件生成報表文檔,ReportEngine.createRunTask( )方法可以創建這個對象。

■ IRenderTask. 通過對報表文檔中的內容進行格式化生成一個完整的報表或一個頁面集。ReportEngine. createRenderTask( )方法返回該對象的一個實例。
每一個任務都可以使用多個報表文件,任務結束後都要調用close方法關閉。

爲運行報表設計文件準備參數
IRunAndRenderTask 和 IRunTask對象允許設置報表參數。調用setParameterValues( )方法,通過傳入一個參數的HashMap設置所有的參數,設置單個參數可以使用setParameterValue( )方法。

添加到報表引擎的類路徑
一些報表設計需要擴展的java類,BIRT報表引擎通過使用環境變量信息定位類。通過動態設置應用程序上下文信息或引擎配置對象也可以配置java類的位置。使用EngineConstants中的常量來配置這些參數。

使用EngineTask對象和EngineConfig對象可以設置應用程序上下文,如下:

configOrTask.getAppContext( ).put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,MyClass.class.getClassLoader( ));


使用EngineConfig對象設置CLASSPATH,如下:
Java代碼 

config.setProperty( EngineConstants.WEBAPP_CLASSPATH_KEY, "c:/myjars/jar1.jar;c:/myclasses" );

使用Java System類設置CLASSPATH,如下:

System.setProperty(EngineConstants.WEBAPP_CLASSPATH_KEY, "c:/myjars/jar1.jar;c:/myclasses" );

BIRT按下面的順序來查找擴展類:
■ 報表引擎plug-inCLASSPATH
■ 上下文環境中定義的EngineConstants.APPCONTEXT_CLASSLOADER_KEY
■Java System 類或EngineConfig 中定義的EngineConstants.WEBAPP_CLASSPATH_KEY.
■Java System 類或EngineConfig 中定義的EngineConstants. PROJECT _CLASSPATH_KEY.
■Java System 類或EngineConfig 中定義的
EngineConstants. WORKSPACE _CLASSPATH_KEY.
■ 報表設計中引入的JAR文件

爲報表設計提供外部對象
BIRT支持把以前實例化的對象拿過來重用。調用報表引擎前你要在應用程序中持有這個已經在內存中存在的對象。如果在腳本環境中使用,你傳遞對象給引擎後,腳本可以通過對象名在必要的是後獲取對象。
可以通過EngineConfigtask得上下文環境爲引擎提供對象。如下:

  1. MyObject mo = new MyObject( );  
  2. config = new EngineConfig( );  
  3. // Get the application context from the config or the task  
  4. HashMap hm = config.getAppContext( );  
  5. //HashMap hm = task.getAppContext( );  
  6. hm.put( "MyCreatedObject", mo );  
  7. config.setAppContext( hm );  
  8. // To refer to this object in a BIRT script  
  9. // or expression, use MyCreatedObject.myMethod()  

設置rendering選項
按指定格式輸出報表前要先設置報表輸出選項。必須設置一個文件名或輸出流用來輸出報表,其他的設置都是可選的如是否創建一個可嵌入的HTML。 BIRT支持可嵌入和非可嵌入兩種HTML,可嵌入HTML可以被嵌入其他的web頁面,不包含頭信息<body> <html>表籤。

應用程序通過rendering options對象設置輸出選項,指定格式的rendering options類實現了IRenderOption接口,並且繼承了RenderOption類。HTMLRenderOption用來設置HTML輸出 選項,PDFRenderOption用來設置PDF格式輸出選項。其他的格式都使用RenderOption

常用的設置有超連接的URL,事件處理器,圖像處理器,輸出格式,支持的圖片輸出格式,還可以設置輸出到數據流還是文件。

設置HTML rendering選項
生成一個使用了圖片的HTML報表,必須進行更多的配置。HTMLRenderOption提供了許多方式去自定義HTML的生產。下面是一些常用的選項:
■ 圖像路徑,生成HTML需要的圖像(圖片,動態生成的圖表等)的路徑通過調用方法HTMLRenderOption.setImageDirectory( )進行設置。
■ 基本圖像URL,在Web應用程序開發時,瀏覽器要引用到圖像所要使用的URL路徑前綴。通過HTMLRenderOption.setBaseImageURL( )進行設置。
■ 圖像處理器,使用HTMLRenderOption.setImageHandler( ) 進行設置
■ 可嵌入的HTML,調用HTMLRenderOption.setEmbeddable( )
■ 從左到右的生成方式,調用HTMLRenderOption.setHtmlRtLFlag( )設置。
■ 標題標籤,調用HTMLRenderOption.setHtmlTitle( )設置,該標籤將顯示在瀏覽器的標籤頁上。
■ 主頁面內容,通過主頁面可以設置頁面大小、頁眉、頁腳等。如果不想讓主頁面的設置影響HTML的生成,可以調用HTMLRenderOption.setMasterPageContent( )設置。
■ 浮動的頁腳,主頁面的頁腳默認直接顯示在內容的下方。傳一個booleanfalse給方法 HTMLRenderOption.setPageFooterFloatFlag( ),強制頁腳在頁面底部的固定位置顯示。這個設置將在生成的HTML中添加一個固定高度的DIV
■主頁的頁邊距,HTMLRenderOption.setOutputMasterPageMargins( )設置主頁的頁邊距是否影響到HTML頁面。
■ HTML分頁,生成HTML時也支持分頁。設置HTMLRenderOption.setHtmlPagination( )可以讓HTML頁面支持分頁。
下面的實例展示了在IRunAndRenderTask對象上使用rendering選項:

  1. // Create a run and render task object.  
  2. IRunAndRenderTask task = engine.createRunAndRenderTask( runnable );  
  3. // Set values for all parameters in a HashMap, parameterValues  
  4. task.setParameterValues( parameterValues );  
  5. // Validate parameter values.  
  6. boolean parametersAreGood = task.validateParameters( );  
  7. // Set the name of an output file.  
  8. HTMLRenderOption options = new HTMLRenderOption( );  
  9. String output = name.replaceFirst( ".rptdesign"".html" );  
  10. options.setOutputFileName( output );  
  11. options.setImageDirectory( "image" );  
  12. options.setHtmlRtLFlag( true );  
  13. options.setEmbeddable( false );  
  14. // Apply the rendering options to the task.  
  15. task.setRenderOption( options );  
  16. // Run and close the task  
  17. task.run();  
  18. task.close();  

設置PDF rendering選項
生成PDF格式的報表需要在報表中嵌入字體,或者使用非標準路徑的字體,應用程序支持PDF rendering以下設置:
■ 字體路徑,如果使用自定義路徑下的字體,使用setFontDirectory( )進行設置。
■ 嵌入的字體,調用PDFRenderOption.setEmbededFont( )可以使用嵌入字體。

五、生成報表

IRunAndRenderTaskIRunTask對象的run方法可以生成報表,同時要處理run方法拋出的EngineException異常。
下面的代碼展示瞭如何生成一個報表:

  1. try {  
  2. task.run( );  
  3. System.out.println( "Created Report " + output + "." );  
  4. }catch ( EngineException e1 ) {  
  5. System.err.println( "Report " + name + " run failed." );  
  6. System.err.println( e1.toString( ) );  
  7. }  
  8. engine.destroy( );  

六、取消報表運行任務

BIRT報表引擎支持檢查引擎任務的狀態,並取消這個任務。通常使用不同的進程來執行這些操作。
使用EngineTask.getStatus( )方法可以檢查報表的運行狀態,返回值有下面這些:
■ IEngineTask.STATUS_NOT_STARTED. 任務沒有啓動
■ IEngineTask.STATUS_RUNNING. 任務正在運行中
■ IEngineTask.STATUS_SUCCEEDED. 任務正常結束
■ IEngineTask.STATUS_FAILED. 任務沒有正常執行
■ IEngineTask.STATUS_CANCELLED. 任務被取消
調用IEngineTask.cancel( )方法可以取消任務。如果一個正在運行的任務被取消了,它將停止生成報表,如果任務正在查詢數據庫,則數據庫要繼續執行完查詢任務。
下面就是一個取消任務的例子:

  1. private class CancelReport extends Thread  
  2. {  
  3. private IEngineTask eTask;  
  4. public CancelReport( String myName, IEngineTask task ) {  
  5. super( myName );  
  6. eTask = task;  
  7. }  
  8. public void cancel( ) {  
  9. try {  
  10. Thread.currentThread( ).sleep( 100 );  
  11. eTask.cancel( );  
  12. System.out.println( "#### Report cancelled #####" );  
  13. }catch( Exception e )  
  14. {  
  15. e.printStackTrace();  
  16. }  
  17. }  
  18. }  
  19. CancelReport cancelThread = new CancelReport( "cancelReport", task);  
  20. cancelThread.cancel( );  
下面我給出幾個比較完整的例子來講解報表引擎的工作。

下面是一個運行並導出部分列數據的代碼:

  1. package REAPI;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.logging.Level;  
  6.   
  7. import org.eclipse.birt.core.framework.Platform;  
  8. import org.eclipse.birt.data.engine.api.IConditionalExpression;  
  9. import org.eclipse.birt.data.engine.api.IFilterDefinition;  
  10. import org.eclipse.birt.data.engine.api.querydefn.ConditionalExpression;  
  11. import org.eclipse.birt.data.engine.api.querydefn.FilterDefinition;  
  12. import org.eclipse.birt.data.engine.core.DataException;  
  13. import org.eclipse.birt.report.engine.api.EngineConfig;  
  14. import org.eclipse.birt.report.engine.api.EngineException;  
  15. import org.eclipse.birt.report.engine.api.IDataExtractionTask;  
  16. import org.eclipse.birt.report.engine.api.IDataIterator;  
  17. import org.eclipse.birt.report.engine.api.IExtractionResults;  
  18. import org.eclipse.birt.report.engine.api.IReportDocument;  
  19. import org.eclipse.birt.report.engine.api.IReportEngine;  
  20. import org.eclipse.birt.report.engine.api.IReportEngineFactory;  
  21. import org.eclipse.birt.report.engine.api.IResultMetaData;  
  22. import org.eclipse.birt.report.engine.api.IResultSetItem;  
  23.   
  24. public class DataExtract {  
  25.   
  26.     static void readReport() throws EngineException  
  27.     {  
  28.         IReportEngine engine=null;  
  29.         EngineConfig config = null;  
  30.   
  31.         try{  
  32.       
  33.             config = new EngineConfig( );             
  34.             config.setBIRTHome("E:\\birt漢化包\\birt-runtime-3_7_2\\ReportEngine");  
  35.             config.setLogConfig(null, Level.OFF);  
  36.             Platform.startup( config );  
  37.             IReportEngineFactory factory = (IReportEngineFactory) Platform  
  38.             .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );  
  39.             engine = factory.createReportEngine( config );  
  40.         }catch( Exception ex){  
  41.             ex.printStackTrace();  
  42.         }  
  43.           
  44.         IReportDocument iReportDocument = engine.openReportDocument("d:\\customers.rptdocument");  
  45.           
  46.         //Setup data extraction task  
  47.         IDataExtractionTask task = engine.createDataExtractionTask(iReportDocument);          
  48.       
  49.         /** 
  50.          * returns the metadata corresponding to the data stored in the report 
  51.          * document.  Could specify a component. 
  52.          */  
  53.           
  54.         ArrayList resultSetList = (ArrayList)task.getResultSetList( );  
  55.   
  56.         IResultSetItem resultItem = null;         
  57.         Iterator it = resultSetList.iterator();  
  58.         while (it.hasNext()) {  
  59.             resultItem = (IResultSetItem)it.next();  
  60.             System.out.println("ResultSetName ====> " + resultItem.getResultSetName( ));  
  61.         }         
  62.           
  63.         resultItem = (IResultSetItem)resultSetList.get( 0 );  
  64.         String dispName = resultItem.getResultSetName( );  
  65.         //Name Tables for ease  
  66.         task.selectResultSet( dispName );  
  67.           
  68.         //Return Only customers between 201-300  
  69.         IFilterDefinition[] FilterExpression = new IFilterDefinition[2];  
  70.         FilterExpression[0] = new FilterDefinition( new ConditionalExpression( "row[\"CUSTOMERNUMBER\"]",  
  71.         IConditionalExpression.OP_GE, "201"null ) );  
  72.         FilterExpression[1] = new FilterDefinition( new ConditionalExpression( "row[\"CUSTOMERNUMBER\"]",  
  73.         IConditionalExpression.OP_LT, "300"null ) );  
  74.         task.setFilters( FilterExpression );      
  75.           
  76.         IExtractionResults iExtractResults = task.extract();  
  77.   
  78.         IDataIterator iData = null;  
  79.         try{  
  80.             if ( iExtractResults != null )  
  81.             {  
  82.   
  83.                 iData = iExtractResults.nextResultIterator( );  
  84.                 //Get metadata on retrieved results  
  85.                 IResultMetaData irmd = iData.getResultMetaData();  
  86.                 if ( iData != null  ){  
  87.                     int colCount = irmd.getColumnCount();  
  88.                     System.out.println("Cloumn Count =" + colCount );  
  89.                     forint j=0; j< colCount; j++){  
  90.                         System.out.println("Cloumn Name =" + irmd.getColumnName(j) );  
  91.                         System.out.println("Cloumn Type =" + irmd.getColumnTypeName(j) );  
  92.                           
  93.                     }  
  94.                   
  95.                     while ( iData.next( ) )  
  96.                     {     
  97.                         //Just disply the first two columns  
  98.                         Object objColumn1;  
  99.                         Object objColumn2;  
  100.                         try{  
  101.                             objColumn1 = iData.getValue(4);  
  102.                         }catch(DataException e){  
  103.                             objColumn1 = new String("");  
  104.                         }  
  105.                         try{  
  106.                             objColumn2 = iData.getValue(12);  
  107.                         }catch(DataException e){  
  108.                             objColumn2 = new String("");  
  109.                         }  
  110.                             System.out.println( objColumn1 + " , " + objColumn2 );  
  111.                     }  
  112.                     iData.close();  
  113.                 }  
  114.             }  
  115.         }  
  116.         catch( Exception e){  
  117.             e.printStackTrace();  
  118.         }  
  119.   
  120.         task.close();  
  121.         engine.destroy();  
  122.         Platform.shutdown();  
  123.         System.out.println("Finished");  
  124.         System.exit(0);  
  125.     }     
  126.     /** 
  127.      * @param args 
  128.      */  
  129.     public static void main(String[] args) {  
  130.         try  
  131.         {  
  132.             readReport( );  
  133.         }  
  134.         catch ( Exception e )  
  135.         {  
  136.             e.printStackTrace();  
  137.         }  
  138.     }  
  139.   
  140. }  

其中customers.rptdesign的設計如下:


預覽如下:


該源碼打印了結果集中的所有表名,從第一個表中獲取了結果集列屬性信息,導出了客戶號在201300之間客戶第4列和第12列信息

運行結果如下:

  1. ResultSetName ====> MyCustomerTable  
  2. ResultSetName ====> innertable  
  3. ResultSetName ====> innertable_1  
  4. ResultSetName ====> innertable_2  
  5. ResultSetName ====> innertable_3  
  6. ResultSetName ====> innertable_4  
  7. ResultSetName ====> innertable_5  
  8. ResultSetName ====> innertable_6  
  9. ResultSetName ====> innertable_7  
  10. ResultSetName ====> innertable_8  
  11. ResultSetName ====> innertable_9  
  12. ResultSetName ====> innertable_10  
  13. ResultSetName ====> innertable_11  
  14. ResultSetName ====> innertable_12  
  15. ResultSetName ====> innertable_13  
  16. ResultSetName ====> innertable_14  
  17. ResultSetName ====> innertable_15  
  18. ResultSetName ====> innertable_16  
  19. ResultSetName ====> innertable_17  
  20. ResultSetName ====> innertable_18  
  21. ResultSetName ====> innertable_19  
  22. ResultSetName ====> innertable_20  
  23. ResultSetName ====> innertable_21  
  24. ResultSetName ====> innertable_22  
  25. ResultSetName ====> innertable_23  
  26. ResultSetName ====> innertable_24  
  27. ResultSetName ====> innertable_25  
  28. ResultSetName ====> innertable_26  
  29. ResultSetName ====> innertable_27  
  30. ResultSetName ====> innertable_28  
  31. ResultSetName ====> innertable_29  
  32. ResultSetName ====> innertable_30  
  33. ResultSetName ====> innertable_31  
  34. ResultSetName ====> innertable_32  
  35. ResultSetName ====> innertable_33  
  36. ResultSetName ====> innertable_34  
  37. ResultSetName ====> innertable_35  
  38. ResultSetName ====> innertable_36  
  39. ResultSetName ====> innertable_37  
  40. ResultSetName ====> innertable_38  
  41. ResultSetName ====> innertable_39  
  42. ResultSetName ====> innertable_40  
  43. ResultSetName ====> innertable_41  
  44. ResultSetName ====> innertable_42  
  45. ResultSetName ====> innertable_43  
  46. ResultSetName ====> innertable_44  
  47. ResultSetName ====> innertable_45  
  48. ResultSetName ====> innertable_46  
  49. ResultSetName ====> innertable_47  
  50. ResultSetName ====> innertable_48  
  51. ResultSetName ====> innertable_49  
  52. ResultSetName ====> innertable_50  
  53. ResultSetName ====> innertable_51  
  54. ResultSetName ====> innertable_52  
  55. ResultSetName ====> innertable_53  
  56. ResultSetName ====> innertable_54  
  57. ResultSetName ====> innertable_55  
  58. ResultSetName ====> innertable_56  
  59. ResultSetName ====> innertable_57  
  60. ResultSetName ====> innertable_58  
  61. ResultSetName ====> innertable_59  
  62. ResultSetName ====> innertable_60  
  63. ResultSetName ====> innertable_61  
  64. ResultSetName ====> innertable_62  
  65. ResultSetName ====> innertable_63  
  66. ResultSetName ====> innertable_64  
  67. ResultSetName ====> innertable_65  
  68. ResultSetName ====> innertable_66  
  69. ResultSetName ====> innertable_67  
  70. ResultSetName ====> innertable_68  
  71. ResultSetName ====> innertable_69  
  72. ResultSetName ====> innertable_70  
  73. ResultSetName ====> innertable_71  
  74. ResultSetName ====> innertable_72  
  75. ResultSetName ====> innertable_73  
  76. ResultSetName ====> innertable_74  
  77. ResultSetName ====> innertable_75  
  78. ResultSetName ====> innertable_76  
  79. ResultSetName ====> innertable_77  
  80. ResultSetName ====> innertable_78  
  81. ResultSetName ====> innertable_79  
  82. ResultSetName ====> innertable_80  
  83. ResultSetName ====> innertable_81  
  84. ResultSetName ====> innertable_82  
  85. ResultSetName ====> innertable_83  
  86. ResultSetName ====> innertable_84  
  87. ResultSetName ====> innertable_85  
  88. ResultSetName ====> innertable_86  
  89. ResultSetName ====> innertable_87  
  90. ResultSetName ====> innertable_88  
  91. ResultSetName ====> innertable_89  
  92. ResultSetName ====> innertable_90  
  93. ResultSetName ====> innertable_91  
  94. ResultSetName ====> innertable_92  
  95. ResultSetName ====> innertable_93  
  96. ResultSetName ====> innertable_94  
  97. ResultSetName ====> innertable_95  
  98. ResultSetName ====> innertable_96  
  99. ResultSetName ====> innertable_97  
  100. ResultSetName ====> innertable_98  
  101. ResultSetName ====> innertable_99  
  102. ResultSetName ====> innertable_100  
  103. ResultSetName ====> innertable_101  
  104. ResultSetName ====> innertable_102  
  105. ResultSetName ====> innertable_103  
  106. ResultSetName ====> innertable_104  
  107. ResultSetName ====> innertable_105  
  108. ResultSetName ====> innertable_106  
  109. ResultSetName ====> innertable_107  
  110. ResultSetName ====> innertable_108  
  111. ResultSetName ====> innertable_109  
  112. ResultSetName ====> innertable_110  
  113. ResultSetName ====> innertable_111  
  114. ResultSetName ====> innertable_112  
  115. ResultSetName ====> innertable_113  
  116. ResultSetName ====> innertable_114  
  117. ResultSetName ====> innertable_115  
  118. ResultSetName ====> innertable_116  
  119. ResultSetName ====> innertable_117  
  120. ResultSetName ====> innertable_118  
  121. ResultSetName ====> innertable_119  
  122. ResultSetName ====> innertable_120  
  123. ResultSetName ====> innertable_121  
  124. Cloumn Count =13  
  125. Cloumn Name =CUSTOMERNUMBER  
  126. Cloumn Type =Integer  
  127. Cloumn Name =CUSTOMERNAME  
  128. Cloumn Type =String  
  129. Cloumn Name =CONTACTLASTNAME  
  130. Cloumn Type =String  
  131. Cloumn Name =CONTACTFIRSTNAME  
  132. Cloumn Type =String  
  133. Cloumn Name =PHONE  
  134. Cloumn Type =String  
  135. Cloumn Name =ADDRESSLINE1  
  136. Cloumn Type =String  
  137. Cloumn Name =ADDRESSLINE2  
  138. Cloumn Type =String  
  139. Cloumn Name =CITY  
  140. Cloumn Type =String  
  141. Cloumn Name =STATE  
  142. Cloumn Type =String  
  143. Cloumn Name =POSTALCODE  
  144. Cloumn Type =String  
  145. Cloumn Name =COUNTRY  
  146. Cloumn Type =String  
  147. Cloumn Name =SALESREPEMPLOYEENUMBER  
  148. Cloumn Type =Integer  
  149. Cloumn Name =CREDITLIMIT  
  150. Cloumn Type =Float  
  151. 02 9936 8555 , 107800.0  
  152. +61 2 9495 8555 , 93300.0  
  153. (604) 555-3392 , 90300.0  
  154. (514) 555-8054 , 48700.0  
  155. (604) 555-4555 , 89600.0  
  156. 86 21 3555 , 120800.0  
  157. 88.60.1555 , 53800.0  
  158. 61.77.6555 , 61100.0  
  159. +33 1 46 62 7555 , 68100.0  
  160. 30.59.8555 , 77900.0  
  161. 0372-555188 , 0.0  
  162. 069-0555984 , 0.0  
  163. 0221-5554327 , 120400.0  
  164. 089-0877555 , 0.0  
  165. +852 2251 1555 , 58600.0  
  166. 011-4988555 , 113000.0  
  167. 035-640555 , 119600.0  
  168. +47 2212 1555 , 95100.0  
  169. +612 9411 1555 , 0.0  
  170. (93) 203 4555 , 60300.0  
  171. (91) 745 6555 , 0.0  
  172. +41 26 425 50 01 , 0.0  
  173. 0897-034555 , 141300.0  
  174. (171) 555-2282 , 92700.0  
  175. (198) 555-8888 , 93900.0  
  176. 6175557555 , 68700.0  
  177. 6265557265 , 90700.0  
  178. 3105552373 , 11000.0  
  179. 7605558146 , 105000.0  
  180. 6175558555 , 123700.0  
  181. Finished  

下面這個案例運行報表,並導出html文件:

  1. package REAPI;  
  2.   
  3. import java.util.logging.Level;  
  4. import org.eclipse.birt.core.framework.Platform;  
  5. import org.eclipse.birt.report.engine.api.EXCELRenderOption;  
  6. import org.eclipse.birt.report.engine.api.EngineConfig;  
  7. import org.eclipse.birt.report.engine.api.EngineException;  
  8. import org.eclipse.birt.report.engine.api.HTMLRenderOption;  
  9. import org.eclipse.birt.report.engine.api.IPDFRenderOption;  
  10. import org.eclipse.birt.report.engine.api.IRenderOption;  
  11. import org.eclipse.birt.report.engine.api.IRenderTask;  
  12. import org.eclipse.birt.report.engine.api.IReportDocument;  
  13. import org.eclipse.birt.report.engine.api.IReportEngine;  
  14. import org.eclipse.birt.report.engine.api.IReportEngineFactory;  
  15. import org.eclipse.birt.report.engine.api.PDFRenderOption;  
  16. import org.eclipse.birt.report.engine.api.RenderOption;  
  17.   
  18.   
  19. public class RenderTask {  
  20.   
  21.     public void runReport() throws EngineException  
  22.     {  
  23.   
  24.         IReportEngine engine=null;  
  25.         EngineConfig config = null;  
  26.   
  27.         try{  
  28.       
  29.             config = new EngineConfig( );             
  30.             config.setBIRTHome("E:\\birt漢化包\\birt-runtime-3_7_2\\ReportEngine");  
  31.             config.setLogConfig(null, Level.FINE);  
  32.             Platform.startup( config );  
  33.             IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );  
  34.             engine = factory.createReportEngine( config );  
  35.         }catch( Exception ex){  
  36.             ex.printStackTrace();  
  37.         }  
  38.   
  39.         IReportDocument document = null;  
  40.         //Open the report design  
  41.         document = engine.openReportDocument("d:\\ps.rptdocument");   
  42.         IRenderOption options = new RenderOption();       
  43.         options.setOutputFormat("html");  
  44.         options.setOutputFileName("d:\\ps.html");  
  45.         options.setOption( IRenderOption.HTML_PAGINATION,  
  46.                 Boolean.TRUE );       
  47. //            
  48. //      if( options.getOutputFormat().equalsIgnoreCase("html")){  
  49. //          HTMLRenderOption htmlOptions = new HTMLRenderOption( options);  
  50. //          htmlOptions.setImageDirectory("output/image");  
  51. //          //set this if you want your image source url to be altered  
  52. //          //htmlOptions.setBaseImageURL("http://myhos/prependme?image=");  
  53. //          htmlOptions.setHtmlRtLFlag(false);  
  54. //          htmlOptions.setEmbeddable(false);  
  55. //      }else if( options.getOutputFormat().equalsIgnoreCase("pdf") ){  
  56. //            
  57. //          PDFRenderOption pdfOptions = new PDFRenderOption( options );  
  58. //          /*  CLIP_CONTENT:             clip the content  
  59. //           *  FIT_TO_PAGE_SIZE:         scale the content to fit into the page  
  60. //           *  OUTPUT_TO_MULTIPLE_PAGES: divided the content into multiple pages  
  61. //           *  ENLARGE_PAGE_SIZE:        enlarge the page size to contain all the content.  
  62. //           */  
  63. //          pdfOptions.setOption( IPDFRenderOption.PAGE_OVERFLOW, Integer.valueOf( PDFRenderOption.FIT_TO_PAGE_SIZE ) );  
  64. //  
  65. //      }  
  66.           
  67.         IRenderTask task = engine.createRenderTask(document);         
  68.         task.setRenderOption(options);  
  69.         //task.setPageRange("1-3");  
  70.         //task.setPageNumber(0);  
  71.         //task.setReportlet("chart3");  
  72.         task.render();  
  73.         task.close();  
  74.         document.close();  
  75.         engine.destroy();  
  76.         Platform.shutdown();  
  77.         System.out.println("Finished");  
  78.     }     
  79.   
  80.   
  81.     /** 
  82.      * @param args 
  83.      */  
  84.     public static void main(String[] args) {  
  85.         try  
  86.         {  
  87.   
  88.             RenderTask ex = new RenderTask( );  
  89.             ex.runReport();  
  90.   
  91.         }  
  92.         catch ( Exception e )  
  93.         {  
  94.             e.printStackTrace();  
  95.         }  
  96.         System.exit(0);  
  97.     }  
  98.       
  99. }  

報表的設計如下:


運行源碼,可以看到D盤導出的文件:


而下面的這段源碼,打印了報表的所有參數:

  1. package REAPI;  
  2.   
  3. import java.util.Collection;  
  4. import java.util.Collections;  
  5. import java.util.HashMap;  
  6. import java.util.Iterator;  
  7. import java.util.logging.Level;  
  8.   
  9. import org.eclipse.birt.core.framework.Platform;  
  10. import org.eclipse.birt.report.engine.api.EngineConfig;  
  11. import org.eclipse.birt.report.engine.api.EngineException;  
  12. import org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask;  
  13. import org.eclipse.birt.report.engine.api.IParameterDefnBase;  
  14. import org.eclipse.birt.report.engine.api.IParameterGroupDefn;  
  15. import org.eclipse.birt.report.engine.api.IParameterSelectionChoice;  
  16. import org.eclipse.birt.report.engine.api.IReportEngine;  
  17. import org.eclipse.birt.report.engine.api.IReportEngineFactory;  
  18. import org.eclipse.birt.report.engine.api.IReportRunnable;  
  19. import org.eclipse.birt.report.engine.api.IScalarParameterDefn;  
  20. import org.eclipse.birt.report.model.api.CascadingParameterGroupHandle;  
  21. import org.eclipse.birt.report.model.api.ScalarParameterHandle;  
  22.   
  23. public class ReportParameters {  
  24.   
  25.     @SuppressWarnings("unchecked")  
  26.     static void executeReport() throws EngineException  
  27.     {  
  28.         HashMap parmDetails = new HashMap();  
  29.         IReportEngine engine=null;  
  30.         EngineConfig config = null;  
  31.         try{  
  32.   
  33.             config = new EngineConfig( );             
  34.             config.setBIRTHome("E:\\birt漢化包\\birt-runtime-3_7_2\\ReportEngine");  
  35.             config.setLogConfig(null, Level.OFF);  
  36.             Platform.startup( config );  
  37.             IReportEngineFactory factory = (IReportEngineFactory) Platform  
  38.             .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );  
  39.             engine = factory.createReportEngine( config );  
  40.         }catch( Exception ex){  
  41.             ex.printStackTrace();  
  42.         }  
  43.   
  44.   
  45.         //Open a report design   
  46.         IReportRunnable design = engine.openReportDesign("d:\\parameters.rptdesign");   
  47.   
  48.         IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask( design );  
  49.         Collection params = task.getParameterDefns( true );  
  50.   
  51.   
  52.         //task.getSelectionListForCascadingGroup();  
  53.         Iterator iter = params.iterator( );  
  54.         while ( iter.hasNext( ) )  
  55.         {  
  56.             IParameterDefnBase param = (IParameterDefnBase) iter.next( );  
  57.   
  58.             if ( param instanceof IParameterGroupDefn )  
  59.             {  
  60.                 IParameterGroupDefn group = (IParameterGroupDefn) param;  
  61.                 System.out.println( "Parameter Group: " + group.getName( ) );  
  62.   
  63.   
  64.                 // Do something with the parameter group.  
  65.                 // Iterate over group contents.  
  66.   
  67.                 Iterator i2 = group.getContents( ).iterator( );  
  68.                 while ( i2.hasNext( ) )  
  69.                 {  
  70.                     IScalarParameterDefn scalar = (IScalarParameterDefn) i2.next( );  
  71.                     //System.out.println("\t" + scalar.getName());  
  72.                     parmDetails.put( scalar.getName(), loadParameterDetails( task, scalar, design, group));  
  73.                 }  
  74.   
  75.             }  
  76.             else  
  77.             {  
  78.   
  79.                 IScalarParameterDefn scalar = (IScalarParameterDefn) param;  
  80.                 //System.out.println(param.getName());  
  81.                 parmDetails.put( scalar.getName(),loadParameterDetails( task, scalar, design, null));                             
  82.   
  83.   
  84.             }  
  85.         }  
  86.   
  87.         task.close();  
  88.         engine.destroy();  
  89.         Platform.shutdown();  
  90.         System.out.println("Finished");  
  91.     }  
  92.   
  93.     private static HashMap loadParameterDetails(IGetParameterDefinitionTask task, IScalarParameterDefn scalar, IReportRunnable report, IParameterGroupDefn group){  
  94.   
  95.   
  96.   
  97.         HashMap parameter = new HashMap();  
  98.   
  99.         if( group == null){  
  100.             parameter.put("Parameter Group""Default");  
  101.         }else{  
  102.             parameter.put("Parameter Group", group.getName());            
  103.         }  
  104.         parameter.put("Name", scalar.getName());  
  105.         parameter.put("Help Text", scalar.getHelpText());  
  106.         parameter.put("Display Name", scalar.getDisplayName());  
  107.         //this is a format code such as  > for UPPERCASE  
  108.         parameter.put("Display Format", scalar.getDisplayFormat());  
  109.   
  110.         if( scalar.isHidden() ){  
  111.             parameter.put("Hidden""Yes");  
  112.         }else{  
  113.             parameter.put("Hidden""No");  
  114.         }  
  115.         if( scalar.isHidden() ){  
  116.             parameter.put("Hidden""Yes");  
  117.         }else{  
  118.             parameter.put("Hidded""No");  
  119.         }  
  120.         if( scalar.isRequired() ){  
  121.             parameter.put("Is Required""Yes");              
  122.         }else{  
  123.             parameter.put("Is Required""No");  
  124.         }  
  125.         if( scalar.isValueConcealed() ){  
  126.             parameter.put("Conceal Entry""Yes");  //ie passwords etc  
  127.         }else{  
  128.             parameter.put("Conceal Entry""No");  
  129.         }  
  130.   
  131.   
  132.         switch (scalar.getControlType()) {  
  133.         case IScalarParameterDefn.TEXT_BOX:  parameter.put("Type""Text Box"); break;  
  134.         case IScalarParameterDefn.LIST_BOX:  parameter.put("Type""List Box"); break;  
  135.         case IScalarParameterDefn.RADIO_BUTTON:  parameter.put("Type""List Box"); break;  
  136.         case IScalarParameterDefn.CHECK_BOX:  parameter.put("Type""List Box"); break;  
  137.         default: parameter.put("Type""Text Box");break;  
  138.         }  
  139.   
  140.   
  141.         switch (scalar.getDataType()) {  
  142.         case IScalarParameterDefn.TYPE_STRING:  parameter.put("Data Type""String"); break;  
  143.         case IScalarParameterDefn.TYPE_FLOAT:  parameter.put("Data Type""Float"); break;  
  144.         case IScalarParameterDefn.TYPE_DECIMAL:  parameter.put("Data Type""Decimal"); break;  
  145.         case IScalarParameterDefn.TYPE_DATE_TIME:  parameter.put("Data Type""Date Time"); break;  
  146.         case IScalarParameterDefn.TYPE_BOOLEAN:  parameter.put("Data Type""Boolean"); break;  
  147.         default:  parameter.put("Data Type""Any"); break;  
  148.         }  
  149.   
  150.   
  151.         ScalarParameterHandle parameterHandle = ( ScalarParameterHandle ) scalar.getHandle();     
  152.   
  153.         parameter.put("Default Value", scalar.getDefaultValue());  
  154.         parameter.put("Prompt Text", scalar.getPromptText());  
  155.         parameter.put("Data Set Expression", parameterHandle.getValueExpr());  
  156.   
  157.         if(scalar.getControlType() !=  IScalarParameterDefn.TEXT_BOX)  
  158.         {  
  159.             //System.out.println("dynamic parameter");  
  160.   
  161.             if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle ){  
  162.                 Collection sList = Collections.EMPTY_LIST;  
  163.                 if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle )  
  164.                 {  
  165.   
  166.                     String groupName = parameterHandle.getContainer( ).getName( );  
  167.                     //used for Cascading parms see IGetParameterDefinitionTask.java code for comments  
  168.                     //task.evaluateQuery( groupName );  
  169.   
  170.                     //Need to load this for calls to get next level.  
  171.                     //This just gets the first level  
  172.                     Object [] keyValueTmp = new Object[1];  
  173.                     sList = task.getSelectionListForCascadingGroup( groupName, keyValueTmp );  
  174.       
  175.   
  176.                     for ( Iterator sl = sList.iterator( ); sl.hasNext( ); )  
  177.                     {  
  178.                         IParameterSelectionChoice sI = (IParameterSelectionChoice) sl.next( );  
  179.   
  180.   
  181.                         Object value = sI.getValue( );  
  182.                         Object label = sI.getLabel( );  
  183.                         System.out.println( label + "--" + value);  
  184.   
  185.                     }                         
  186.   
  187.                 }                 
  188.             }else{  
  189.                 Collection selectionList = task.getSelectionList( scalar.getName() );  
  190.   
  191.                 if ( selectionList != null )  
  192.                 {  
  193.                     HashMap dynamicList = new HashMap();         
  194.   
  195.                     for ( Iterator sliter = selectionList.iterator( ); sliter.hasNext( ); )  
  196.                     {  
  197.                         IParameterSelectionChoice selectionItem = (IParameterSelectionChoice) sliter.next( );  
  198.   
  199.                         Object value = selectionItem.getValue( );  
  200.                         String label = selectionItem.getLabel( );  
  201.   
  202.                         //System.out.println( label + "--" + value);  
  203.                         //Display label unless null then display value.  Value is the what should get passed to the report.  
  204.                         dynamicList.put(value,label);  
  205.   
  206.                     }  
  207.                     parameter.put("Selection List", dynamicList);  
  208.                 }  
  209.             }  
  210.   
  211.         }  
  212.   
  213.   
  214.         Iterator iter = parameter.keySet().iterator();  
  215.         System.out.println("======================Parameter =" + scalar.getName());  
  216.         while (iter.hasNext()) {  
  217.             String name = (String) iter.next();   
  218.             if( name.equals("Selection List")){  
  219.                 HashMap selList = (HashMap)parameter.get(name);  
  220.                 Iterator selIter = selList.keySet().iterator();  
  221.                 while (selIter.hasNext()) {  
  222.                     Object lbl = selIter.next();  
  223.                     System.out.println( "Selection List Entry ===== Key = " + lbl + " Value = " + selList.get(lbl));  
  224.                 }  
  225.   
  226.             }else{  
  227.                 System.out.println( name + " = " + parameter.get(name));                  
  228.             }  
  229.         }  
  230.         return parameter;  
  231.   
  232.     }     
  233.   
  234.     /** 
  235.      * @param args 
  236.      */  
  237.     public static void main(String[] args) {  
  238.         try  
  239.         {  
  240.             executeReport( );  
  241.         }  
  242.         catch ( Exception e )  
  243.         {  
  244.             e.printStackTrace();  
  245.         }  
  246.     }  
  247.   
  248. }  

運行結果如下:

  1. ======================Parameter =parameter1  
  2. Hidded = No  
  3. Default Value = 1  
  4. Type = Text Box  
  5. Conceal Entry = No  
  6. Hidden = No  
  7. Data Set Expression = null  
  8. Name = parameter1  
  9. Is Required = Yes  
  10. Help Text = null  
  11. Parameter Group = Default  
  12. Display Name = null  
  13. Display Format = #,##0.00%  
  14. Data Type = Decimal  
  15. Prompt Text = "This i prompt text"  
  16. Parameter Group: ParmGroup1  
  17. ======================Parameter =Parameter2  
  18. Hidded = No  
  19. Default Value = parm2  
  20. Type = Text Box  
  21. Conceal Entry = No  
  22. Hidden = No  
  23. Data Set Expression = null  
  24. Name = Parameter2  
  25. Is Required = No  
  26. Help Text = null  
  27. Parameter Group = ParmGroup1  
  28. Display Name = null  
  29. Display Format = Unformatted  
  30. Data Type = String  
  31. Prompt Text = null  
  32. ======================Parameter =Parameter3  
  33. Hidded = No  
  34. Default Value = 10101  
  35. Type = List Box  
  36. Conceal Entry = No  
  37. Hidden = No  
  38. Selection List Entry ===== Key = 10346 Value = 10346  
  39. Selection List Entry ===== Key = 10207 Value = 10207  
  40. Selection List Entry ===== Key = 10347 Value = 10347  
  41. Selection List Entry ===== Key = 10208 Value = 10208  
  42. Selection List Entry ===== Key = 10344 Value = 10344  
  43. Selection List Entry ===== Key = 10205 Value = 10205  
  44. Selection List Entry ===== Key = 10345 Value = 10345  
  45. Selection List Entry ===== Key = 10206 Value = 10206  
  46. Selection List Entry ===== Key = 10342 Value = 10342  
  47. Selection List Entry ===== Key = 10343 Value = 10343  
  48. Selection List Entry ===== Key = 10340 Value = 10340  
  49. Selection List Entry ===== Key = 10209 Value = 10209  
  50. Selection List Entry ===== Key = 10341 Value = 10341  
  51. Selection List Entry ===== Key = 10200 Value = 10200  
  52. Selection List Entry ===== Key = 10203 Value = 10203  
  53. Selection List Entry ===== Key = 10204 Value = 10204  
  54. Selection List Entry ===== Key = 10348 Value = 10348  
  55. Selection List Entry ===== Key = 10201 Value = 10201  
  56. Selection List Entry ===== Key = 10349 Value = 10349  
  57. Selection List Entry ===== Key = 10202 Value = 10202  
  58. Selection List Entry ===== Key = 10333 Value = 10333  
  59. Selection List Entry ===== Key = 10334 Value = 10334  
  60. Selection List Entry ===== Key = 10335 Value = 10335  
  61. Selection List Entry ===== Key = 10336 Value = 10336  
  62. Selection List Entry ===== Key = 10330 Value = 10330  
  63. Selection List Entry ===== Key = 10331 Value = 10331  
  64. Selection List Entry ===== Key = 10332 Value = 10332  
  65. Selection List Entry ===== Key = 10337 Value = 10337  
  66. Selection List Entry ===== Key = 10338 Value = 10338  
  67. Selection List Entry ===== Key = 10339 Value = 10339  
  68. Selection List Entry ===== Key = 10320 Value = 10320  
  69. Selection List Entry ===== Key = 10321 Value = 10321  
  70. Selection List Entry ===== Key = 10324 Value = 10324  
  71. Selection List Entry ===== Key = 10229 Value = 10229  
  72. Selection List Entry ===== Key = 10325 Value = 10325  
  73. Selection List Entry ===== Key = 10322 Value = 10322  
  74. Selection List Entry ===== Key = 10227 Value = 10227  
  75. Selection List Entry ===== Key = 10323 Value = 10323  
  76. Selection List Entry ===== Key = 10228 Value = 10228  
  77. Selection List Entry ===== Key = 10328 Value = 10328  
  78. Selection List Entry ===== Key = 10225 Value = 10225  
  79. Selection List Entry ===== Key = 10329 Value = 10329  
  80. Selection List Entry ===== Key = 10226 Value = 10226  
  81. Selection List Entry ===== Key = 10326 Value = 10326  
  82. Selection List Entry ===== Key = 10223 Value = 10223  
  83. Selection List Entry ===== Key = 10327 Value = 10327  
  84. Selection List Entry ===== Key = 10224 Value = 10224  
  85. Selection List Entry ===== Key = 10221 Value = 10221  
  86. Selection List Entry ===== Key = 10222 Value = 10222  
  87. Selection List Entry ===== Key = 10220 Value = 10220  
  88. Selection List Entry ===== Key = 10310 Value = 10310  
  89. Selection List Entry ===== Key = 10311 Value = 10311  
  90. Selection List Entry ===== Key = 10216 Value = 10216  
  91. Selection List Entry ===== Key = 10312 Value = 10312  
  92. Selection List Entry ===== Key = 10217 Value = 10217  
  93. Selection List Entry ===== Key = 10313 Value = 10313  
  94. Selection List Entry ===== Key = 10218 Value = 10218  
  95. Selection List Entry ===== Key = 10314 Value = 10314  
  96. Selection List Entry ===== Key = 10219 Value = 10219  
  97. Selection List Entry ===== Key = 10315 Value = 10315  
  98. Selection List Entry ===== Key = 10212 Value = 10212  
  99. Selection List Entry ===== Key = 10316 Value = 10316  
  100. Selection List Entry ===== Key = 10213 Value = 10213  
  101. Selection List Entry ===== Key = 10317 Value = 10317  
  102. Selection List Entry ===== Key = 10214 Value = 10214  
  103. Selection List Entry ===== Key = 10318 Value = 10318  
  104. Selection List Entry ===== Key = 10215 Value = 10215  
  105. Selection List Entry ===== Key = 10319 Value = 10319  
  106. Selection List Entry ===== Key = 10210 Value = 10210  
  107. Selection List Entry ===== Key = 10211 Value = 10211  
  108. Selection List Entry ===== Key = 10244 Value = 10244  
  109. Selection List Entry ===== Key = 10243 Value = 10243  
  110. Selection List Entry ===== Key = 10242 Value = 10242  
  111. Selection List Entry ===== Key = 10241 Value = 10241  
  112. Selection List Entry ===== Key = 10248 Value = 10248  
  113. Selection List Entry ===== Key = 10247 Value = 10247  
  114. Selection List Entry ===== Key = 10246 Value = 10246  
  115. Selection List Entry ===== Key = 10245 Value = 10245  
  116. Selection List Entry ===== Key = 10389 Value = 10389  
  117. Selection List Entry ===== Key = 10388 Value = 10388  
  118. Selection List Entry ===== Key = 10249 Value = 10249  
  119. Selection List Entry ===== Key = 10387 Value = 10387  
  120. Selection List Entry ===== Key = 10386 Value = 10386  
  121. Selection List Entry ===== Key = 10385 Value = 10385  
  122. Selection List Entry ===== Key = 10384 Value = 10384  
  123. Selection List Entry ===== Key = 10382 Value = 10382  
  124. Selection List Entry ===== Key = 10383 Value = 10383  
  125. Selection List Entry ===== Key = 10380 Value = 10380  
  126. Selection List Entry ===== Key = 10381 Value = 10381  
  127. Selection List Entry ===== Key = 10240 Value = 10240  
  128. Selection List Entry ===== Key = 10231 Value = 10231  
  129. Selection List Entry ===== Key = 10230 Value = 10230  
  130. Selection List Entry ===== Key = 10233 Value = 10233  
  131. Selection List Entry ===== Key = 10232 Value = 10232  
  132. Selection List Entry ===== Key = 10235 Value = 10235  
  133. Selection List Entry ===== Key = 10234 Value = 10234  
  134. Selection List Entry ===== Key = 10237 Value = 10237  
  135. Selection List Entry ===== Key = 10236 Value = 10236  
  136. Selection List Entry ===== Key = 10378 Value = 10378  
  137. Selection List Entry ===== Key = 10239 Value = 10239  
  138. Selection List Entry ===== Key = 10377 Value = 10377  
  139. Selection List Entry ===== Key = 10238 Value = 10238  
  140. Selection List Entry ===== Key = 10379 Value = 10379  
  141. Selection List Entry ===== Key = 10374 Value = 10374  
  142. Selection List Entry ===== Key = 10373 Value = 10373  
  143. Selection List Entry ===== Key = 10376 Value = 10376  
  144. Selection List Entry ===== Key = 10375 Value = 10375  
  145. Selection List Entry ===== Key = 10370 Value = 10370  
  146. Selection List Entry ===== Key = 10371 Value = 10371  
  147. Selection List Entry ===== Key = 10372 Value = 10372  
  148. Selection List Entry ===== Key = 10269 Value = 10269  
  149. Selection List Entry ===== Key = 10268 Value = 10268  
  150. Selection List Entry ===== Key = 10267 Value = 10267  
  151. Selection List Entry ===== Key = 10266 Value = 10266  
  152. Selection List Entry ===== Key = 10265 Value = 10265  
  153. Selection List Entry ===== Key = 10264 Value = 10264  
  154. Selection List Entry ===== Key = 10263 Value = 10263  
  155. Selection List Entry ===== Key = 10365 Value = 10365  
  156. Selection List Entry ===== Key = 10364 Value = 10364  
  157. Selection List Entry ===== Key = 10363 Value = 10363  
  158. Selection List Entry ===== Key = 10362 Value = 10362  
  159. Selection List Entry ===== Key = 10369 Value = 10369  
  160. Selection List Entry ===== Key = 10368 Value = 10368  
  161. Selection List Entry ===== Key = 10367 Value = 10367  
  162. Selection List Entry ===== Key = 10366 Value = 10366  
  163. Selection List Entry ===== Key = 10360 Value = 10360  
  164. Selection List Entry ===== Key = 10361 Value = 10361  
  165. Selection List Entry ===== Key = 10261 Value = 10261  
  166. Selection List Entry ===== Key = 10262 Value = 10262  
  167. Selection List Entry ===== Key = 10260 Value = 10260  
  168. Selection List Entry ===== Key = 10257 Value = 10257  
  169. Selection List Entry ===== Key = 10359 Value = 10359  
  170. Selection List Entry ===== Key = 10256 Value = 10256  
  171. Selection List Entry ===== Key = 10259 Value = 10259  
  172. Selection List Entry ===== Key = 10258 Value = 10258  
  173. Selection List Entry ===== Key = 10253 Value = 10253  
  174. Selection List Entry ===== Key = 10252 Value = 10252  
  175. Selection List Entry ===== Key = 10255 Value = 10255  
  176. Selection List Entry ===== Key = 10254 Value = 10254  
  177. Selection List Entry ===== Key = 10352 Value = 10352  
  178. Selection List Entry ===== Key = 10351 Value = 10351  
  179. Selection List Entry ===== Key = 10354 Value = 10354  
  180. Selection List Entry ===== Key = 10353 Value = 10353  
  181. Selection List Entry ===== Key = 10356 Value = 10356  
  182. Selection List Entry ===== Key = 10355 Value = 10355  
  183. Selection List Entry ===== Key = 10358 Value = 10358  
  184. Selection List Entry ===== Key = 10357 Value = 10357  
  185. Selection List Entry ===== Key = 10350 Value = 10350  
  186. Selection List Entry ===== Key = 10250 Value = 10250  
  187. Selection List Entry ===== Key = 10251 Value = 10251  
  188. Selection List Entry ===== Key = 10152 Value = 10152  
  189. Selection List Entry ===== Key = 10151 Value = 10151  
  190. Selection List Entry ===== Key = 10150 Value = 10150  
  191. Selection List Entry ===== Key = 10130 Value = 10130  
  192. Selection List Entry ===== Key = 10400 Value = 10400  
  193. Selection List Entry ===== Key = 10402 Value = 10402  
  194. Selection List Entry ===== Key = 10401 Value = 10401  
  195. Selection List Entry ===== Key = 10404 Value = 10404  
  196. Selection List Entry ===== Key = 10291 Value = 10291  
  197. Selection List Entry ===== Key = 10403 Value = 10403  
  198. Selection List Entry ===== Key = 10290 Value = 10290  
  199. Selection List Entry ===== Key = 10406 Value = 10406  
  200. Selection List Entry ===== Key = 10405 Value = 10405  
  201. Selection List Entry ===== Key = 10408 Value = 10408  
  202. Selection List Entry ===== Key = 10295 Value = 10295  
  203. Selection List Entry ===== Key = 10407 Value = 10407  
  204. Selection List Entry ===== Key = 10294 Value = 10294  
  205. Selection List Entry ===== Key = 10293 Value = 10293  
  206. Selection List Entry ===== Key = 10409 Value = 10409  
  207. Selection List Entry ===== Key = 10292 Value = 10292  
  208. Selection List Entry ===== Key = 10298 Value = 10298  
  209. Selection List Entry ===== Key = 10299 Value = 10299  
  210. Selection List Entry ===== Key = 10296 Value = 10296  
  211. Selection List Entry ===== Key = 10297 Value = 10297  
  212. Selection List Entry ===== Key = 10139 Value = 10139  
  213. Selection List Entry ===== Key = 10159 Value = 10159  
  214. Selection List Entry ===== Key = 10135 Value = 10135  
  215. Selection List Entry ===== Key = 10136 Value = 10136  
  216. Selection List Entry ===== Key = 10157 Value = 10157  
  217. Selection List Entry ===== Key = 10137 Value = 10137  
  218. Selection List Entry ===== Key = 10158 Value = 10158  
  219. Selection List Entry ===== Key = 10138 Value = 10138  
  220. Selection List Entry ===== Key = 10155 Value = 10155  
  221. Selection List Entry ===== Key = 10131 Value = 10131  
  222. Selection List Entry ===== Key = 10156 Value = 10156  
  223. Selection List Entry ===== Key = 10132 Value = 10132  
  224. Selection List Entry ===== Key = 10153 Value = 10153  
  225. Selection List Entry ===== Key = 10133 Value = 10133  
  226. Selection List Entry ===== Key = 10154 Value = 10154  
  227. Selection List Entry ===== Key = 10134 Value = 10134  
  228. Selection List Entry ===== Key = 10161 Value = 10161  
  229. Selection List Entry ===== Key = 10141 Value = 10141  
  230. Selection List Entry ===== Key = 10160 Value = 10160  
  231. Selection List Entry ===== Key = 10140 Value = 10140  
  232. Selection List Entry ===== Key = 10163 Value = 10163  
  233. Selection List Entry ===== Key = 10162 Value = 10162  
  234. Selection List Entry ===== Key = 10168 Value = 10168  
  235. Selection List Entry ===== Key = 10148 Value = 10148  
  236. Selection List Entry ===== Key = 10169 Value = 10169  
  237. Selection List Entry ===== Key = 10149 Value = 10149  
  238. Selection List Entry ===== Key = 10146 Value = 10146  
  239. Selection List Entry ===== Key = 10147 Value = 10147  
  240. Selection List Entry ===== Key = 10164 Value = 10164  
  241. Selection List Entry ===== Key = 10144 Value = 10144  
  242. Selection List Entry ===== Key = 10165 Value = 10165  
  243. Selection List Entry ===== Key = 10145 Value = 10145  
  244. Selection List Entry ===== Key = 10166 Value = 10166  
  245. Selection List Entry ===== Key = 10142 Value = 10142  
  246. Selection List Entry ===== Key = 10167 Value = 10167  
  247. Selection List Entry ===== Key = 10143 Value = 10143  
  248. Selection List Entry ===== Key = 10422 Value = 10422  
  249. Selection List Entry ===== Key = 10170 Value = 10170  
  250. Selection List Entry ===== Key = 10421 Value = 10421  
  251. Selection List Entry ===== Key = 10424 Value = 10424  
  252. Selection List Entry ===== Key = 10423 Value = 10423  
  253. Selection List Entry ===== Key = 10174 Value = 10174  
  254. Selection List Entry ===== Key = 10173 Value = 10173  
  255. Selection List Entry ===== Key = 10420 Value = 10420  
  256. Selection List Entry ===== Key = 10172 Value = 10172  
  257. Selection List Entry ===== Key = 10171 Value = 10171  
  258. Selection List Entry ===== Key = 10273 Value = 10273  
  259. Selection List Entry ===== Key = 10272 Value = 10272  
  260. Selection List Entry ===== Key = 10271 Value = 10271  
  261. Selection List Entry ===== Key = 10270 Value = 10270  
  262. Selection List Entry ===== Key = 10425 Value = 10425  
  263. Selection List Entry ===== Key = 10117 Value = 10117  
  264. Selection List Entry ===== Key = 10118 Value = 10118  
  265. Selection List Entry ===== Key = 10119 Value = 10119  
  266. Selection List Entry ===== Key = 10278 Value = 10278  
  267. Selection List Entry ===== Key = 10279 Value = 10279  
  268. Selection List Entry ===== Key = 10276 Value = 10276  
  269. Selection List Entry ===== Key = 10277 Value = 10277  
  270. Selection List Entry ===== Key = 10274 Value = 10274  
  271. Selection List Entry ===== Key = 10275 Value = 10275  
  272. Selection List Entry ===== Key = 10177 Value = 10177  
  273. Selection List Entry ===== Key = 10178 Value = 10178  
  274. Selection List Entry ===== Key = 10110 Value = 10110  
  275. Selection List Entry ===== Key = 10175 Value = 10175  
  276. Selection List Entry ===== Key = 10111 Value = 10111  
  277. Selection List Entry ===== Key = 10176 Value = 10176  
  278. Selection List Entry ===== Key = 10112 Value = 10112  
  279. Selection List Entry ===== Key = 10113 Value = 10113  
  280. Selection List Entry ===== Key = 10114 Value = 10114  
  281. Selection List Entry ===== Key = 10179 Value = 10179  
  282. Selection List Entry ===== Key = 10115 Value = 10115  
  283. Selection List Entry ===== Key = 10116 Value = 10116  
  284. Selection List Entry ===== Key = 10413 Value = 10413  
  285. Selection List Entry ===== Key = 10412 Value = 10412  
  286. Selection List Entry ===== Key = 10411 Value = 10411  
  287. Selection List Entry ===== Key = 10181 Value = 10181  
  288. Selection List Entry ===== Key = 10410 Value = 10410  
  289. Selection List Entry ===== Key = 10180 Value = 10180  
  290. Selection List Entry ===== Key = 10183 Value = 10183  
  291. Selection List Entry ===== Key = 10182 Value = 10182  
  292. Selection List Entry ===== Key = 10185 Value = 10185  
  293. Selection List Entry ===== Key = 10184 Value = 10184  
  294. Selection List Entry ===== Key = 10282 Value = 10282  
  295. Selection List Entry ===== Key = 10281 Value = 10281  
  296. Selection List Entry ===== Key = 10419 Value = 10419  
  297. Selection List Entry ===== Key = 10284 Value = 10284  
  298. Selection List Entry ===== Key = 10418 Value = 10418  
  299. Selection List Entry ===== Key = 10283 Value = 10283  
  300. Selection List Entry ===== Key = 10417 Value = 10417  
  301. Selection List Entry ===== Key = 10416 Value = 10416  
  302. Selection List Entry ===== Key = 10415 Value = 10415  
  303. Selection List Entry ===== Key = 10280 Value = 10280  
  304. Selection List Entry ===== Key = 10414 Value = 10414  
  305. Selection List Entry ===== Key = 10289 Value = 10289  
  306. Selection List Entry ===== Key = 10128 Value = 10128  
  307. Selection List Entry ===== Key = 10129 Value = 10129  
  308. Selection List Entry ===== Key = 10285 Value = 10285  
  309. Selection List Entry ===== Key = 10286 Value = 10286  
  310. Selection List Entry ===== Key = 10287 Value = 10287  
  311. Selection List Entry ===== Key = 10288 Value = 10288  
  312. Selection List Entry ===== Key = 10186 Value = 10186  
  313. Selection List Entry ===== Key = 10122 Value = 10122  
  314. Selection List Entry ===== Key = 10187 Value = 10187  
  315. Selection List Entry ===== Key = 10123 Value = 10123  
  316. Selection List Entry ===== Key = 10188 Value = 10188  
  317. Selection List Entry ===== Key = 10120 Value = 10120  
  318. Selection List Entry ===== Key = 10189 Value = 10189  
  319. Selection List Entry ===== Key = 10121 Value = 10121  
  320. Selection List Entry ===== Key = 10126 Value = 10126  
  321. Selection List Entry ===== Key = 10127 Value = 10127  
  322. Selection List Entry ===== Key = 10124 Value = 10124  
  323. Selection List Entry ===== Key = 10125 Value = 10125  
  324. Selection List Entry ===== Key = 10195 Value = 10195  
  325. Selection List Entry ===== Key = 10196 Value = 10196  
  326. Selection List Entry ===== Key = 10399 Value = 10399  
  327. Selection List Entry ===== Key = 10193 Value = 10193  
  328. Selection List Entry ===== Key = 10194 Value = 10194  
  329. Selection List Entry ===== Key = 10397 Value = 10397  
  330. Selection List Entry ===== Key = 10191 Value = 10191  
  331. Selection List Entry ===== Key = 10398 Value = 10398  
  332. Selection List Entry ===== Key = 10192 Value = 10192  
  333. Selection List Entry ===== Key = 10395 Value = 10395  
  334. Selection List Entry ===== Key = 10396 Value = 10396  
  335. Selection List Entry ===== Key = 10190 Value = 10190  
  336. Selection List Entry ===== Key = 10394 Value = 10394  
  337. Selection List Entry ===== Key = 10309 Value = 10309  
  338. Selection List Entry ===== Key = 10393 Value = 10393  
  339. Selection List Entry ===== Key = 10308 Value = 10308  
  340. Selection List Entry ===== Key = 10392 Value = 10392  
  341. Selection List Entry ===== Key = 10391 Value = 10391  
  342. Selection List Entry ===== Key = 10390 Value = 10390  
  343. Selection List Entry ===== Key = 10305 Value = 10305  
  344. Selection List Entry ===== Key = 10304 Value = 10304  
  345. Selection List Entry ===== Key = 10199 Value = 10199  
  346. Selection List Entry ===== Key = 10307 Value = 10307  
  347. Selection List Entry ===== Key = 10198 Value = 10198  
  348. Selection List Entry ===== Key = 10306 Value = 10306  
  349. Selection List Entry ===== Key = 10197 Value = 10197  
  350. Selection List Entry ===== Key = 10301 Value = 10301  
  351. Selection List Entry ===== Key = 10300 Value = 10300  
  352. Selection List Entry ===== Key = 10303 Value = 10303  
  353. Selection List Entry ===== Key = 10302 Value = 10302  
  354. Selection List Entry ===== Key = 10105 Value = 10105  
  355. Selection List Entry ===== Key = 10104 Value = 10104  
  356. Selection List Entry ===== Key = 10103 Value = 10103  
  357. Selection List Entry ===== Key = 10102 Value = 10102  
  358. Selection List Entry ===== Key = 10101 Value = 10101  
  359. Selection List Entry ===== Key = 10100 Value = 10100  
  360. Selection List Entry ===== Key = 10109 Value = 10109  
  361. Selection List Entry ===== Key = 10108 Value = 10108  
  362. Selection List Entry ===== Key = 10107 Value = 10107  
  363. Selection List Entry ===== Key = 10106 Value = 10106  
  364. Data Set Expression = row["ORDERNUMBER"]  
  365. Name = Parameter3  
  366. Is Required = Yes  
  367. Help Text = null  
  368. Parameter Group = Default  
  369. Display Name = null  
  370. Display Format = >  
  371. Data Type = String  
  372. Prompt Text = null  
  373. ======================Parameter =comboparm  
  374. Hidded = No  
  375. Default Value = val2  
  376. Type = List Box  
  377. Conceal Entry = No  
  378. Hidden = No  
  379. Selection List Entry ===== Key = val1 Value = displaytext1  
  380. Selection List Entry ===== Key = val2 Value = displaytestval2  
  381. Data Set Expression = null  
  382. Name = comboparm  
  383. Is Required = No  
  384. Help Text = null  
  385. Parameter Group = Default  
  386. Display Name = null  
  387. Display Format = Unformatted  
  388. Data Type = String  
  389. Prompt Text = null  
  390. ======================Parameter =radioparm  
  391. Hidded = No  
  392. Default Value = radio1  
  393. Type = List Box  
  394. Conceal Entry = No  
  395. Hidden = No  
  396. Selection List Entry ===== Key = radio1 Value = radio1  
  397. Selection List Entry ===== Key = radio2 Value = displaytextcombo2  
  398. Data Set Expression = null  
  399. Name = radioparm  
  400. Is Required = No  
  401. Help Text = radiohelp  
  402. Parameter Group = Default  
  403. Display Name = null  
  404. Display Format = Unformatted  
  405. Data Type = String  
  406. Prompt Text = null  
  407. ======================Parameter =staticlistbox  
  408. Hidded = No  
  409. Default Value = null  
  410. Type = List Box  
  411. Conceal Entry = No  
  412. Hidden = No  
  413. Selection List Entry ===== Key = listboxstatic1 Value = listboxstatic1  
  414. Selection List Entry ===== Key = listboxstatic2 Value = displaytextlistbox2  
  415. Data Set Expression = null  
  416. Name = staticlistbox  
  417. Is Required = No  
  418. Help Text = null  
  419. Parameter Group = Default  
  420. Display Name = null  
  421. Display Format = Unformatted  
  422. Data Type = String  
  423. Prompt Text = null  
  424. Parameter Group: iii  
  425. ======================Parameter =customernumber  
  426. Hidded = No  
  427. Default Value = defaultvalue  
  428. Type = List Box  
  429. Conceal Entry = No  
  430. Hidden = No  
  431. Data Set Expression = row["CUSTOMERNUMBER"]  
  432. Name = customernumber  
  433. Is Required = No  
  434. Help Text = helptext  
  435. Parameter Group = iii  
  436. Display Name = null  
  437. Display Format = Unformatted  
  438. Data Type = String  
  439. Prompt Text = enter customer number  
  440. ======================Parameter =OrderNumber  
  441. Hidded = No  
  442. Default Value = null  
  443. Type = List Box  
  444. Conceal Entry = No  
  445. Hidden = No  
  446. Data Set Expression = row["ORDERNUMBER"]  
  447. Name = OrderNumber  
  448. Is Required = No  
  449. Help Text = null  
  450. Parameter Group = iii  
  451. Display Name = null  
  452. Display Format = Unformatted  
  453. Data Type = String  
  454. Prompt Text = null  
  455. ======================Parameter =parameter6  
  456. Hidded = No  
  457. Default Value = test  
  458. Type = Text Box  
  459. Conceal Entry = No  
  460. Hidden = No  
  461. Data Set Expression = null  
  462. Name = parameter6  
  463. Is Required = No  
  464. Help Text = null  
  465. Parameter Group = Default  
  466. Display Name = null  
  467. Display Format = <  
  468. Data Type = String  
  469. Prompt Text = null  
  470. ======================Parameter =parameter7  
  471. Hidded = No  
  472. Default Value = 2  
  473. Type = List Box  
  474. Conceal Entry = No  
  475. Hidden = No  
  476. Selection List Entry ===== Key = 3 Value = 3  
  477. Selection List Entry ===== Key = 2 Value = 2  
  478. Selection List Entry ===== Key = 1 Value = 1  
  479. Data Set Expression = null  
  480. Name = parameter7  
  481. Is Required = Yes  
  482. Help Text = null  
  483. Parameter Group = Default  
  484. Display Name = null  
  485. Display Format = Unformatted  
  486. Data Type = Decimal  
  487. Prompt Text = null  
  488. ======================Parameter =DateParameter  
  489. Hidded = No  
  490. Default Value = 12/12/2006  
  491. Type = Text Box  
  492. Conceal Entry = No  
  493. Hidden = No  
  494. Data Set Expression = null  
  495. Name = DateParameter  
  496. Is Required = Yes  
  497. Help Text = null  
  498. Parameter Group = Default  
  499. Display Name = null  
  500. Display Format = Unformatted  
  501. Data Type = Date Time  
  502. Prompt Text = null  
  503. Finished  

下面的例子在控制檯打印了TOC信息:

  1. package REAPI;  
  2.   
  3. import java.util.logging.Level;  
  4.   
  5. import org.eclipse.birt.core.framework.Platform;  
  6. import org.eclipse.birt.report.engine.api.EngineConfig;  
  7. import org.eclipse.birt.report.engine.api.EngineException;  
  8. import org.eclipse.birt.report.engine.api.IReportDocument;  
  9. import org.eclipse.birt.report.engine.api.IReportEngine;  
  10. import org.eclipse.birt.report.engine.api.IReportEngineFactory;  
  11. import org.eclipse.birt.report.engine.api.TOCNode;  
  12.   
  13.   
  14. public class Toc {  
  15.   
  16.     public void runReport() throws EngineException  
  17.     {  
  18.   
  19.         IReportEngine engine=null;  
  20.         EngineConfig config = null;  
  21.   
  22.         try{  
  23.   
  24.             config = new EngineConfig( );             
  25.             config.setBIRTHome("E:\\birt漢化包\\birt-runtime-3_7_2\\ReportEngine");  
  26.             config.setLogConfig(null, Level.FINE);  
  27.             Platform.startup( config );  
  28.             IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );  
  29.             engine = factory.createReportEngine( config );  
  30.         }catch( Exception ex){  
  31.             ex.printStackTrace();  
  32.         }  
  33.   
  34.         IReportDocument document = null;  
  35.         //Open the report design  
  36.         document = engine.openReportDocument("d:\\customers.rptdocument");   
  37.   
  38.   
  39.         // Get the root of the table of contents.  
  40.         TOCNode td = document.findTOC( null );  
  41.         java.util.List children = td.getChildren( );  
  42.         long pNumber;  
  43.         // Loop through the top level table of contents entries.  
  44.         if ( children != null && children.size( ) > 0 ) {  
  45.             for ( int i = 0; i < children.size( ); i++ )     {  
  46.                 // Find the required table of contents entry.  
  47.                 TOCNode child = ( TOCNode ) children.get( i );  
  48.                 //if ( child.getDisplayString( ).equals( "103" ) )      {  
  49.                 // Get the number of the page that contains the data.  
  50.                 pNumber = document.getPageNumber( child.getBookmark( ) );  
  51.                 System.out.println( child.getDisplayString( ) + " Page to print is " + pNumber );  
  52.                 //}  
  53.             }  
  54.         }         
  55.   
  56.         document.close();  
  57.         engine.destroy();  
  58.         Platform.shutdown();  
  59.         System.out.println("Finished");  
  60.     }     
  61.   
  62.   
  63.     /** 
  64.      * @param args 
  65.      */  
  66.     public static void main(String[] args) {  
  67.         try  
  68.         {  
  69.   
  70.             Toc ex = new Toc( );  
  71.             ex.runReport();  
  72.   
  73.         }  
  74.         catch ( Exception e )  
  75.         {  
  76.             e.printStackTrace();  
  77.         }  
  78.     }  
  79.   
  80.   
  81. }  

報表的預覽如下:


運行結果如下:

Australia Page to print is 1

Austria Page to print is 2

Belgium Page to print is 3

Canada Page to print is 4

Denmark Page to print is 5

Finland Page to print is 6

France Page to print is 7

Germany Page to print is 8

Hong Kong Page to print is 9

Ireland Page to print is 10

Israel Page to print is 11

Italy Page to print is 12

Japan Page to print is 13

Netherlands Page to print is 14

New Zealand Page to print is 15

Norway Page to print is 16

Philippines Page to print is 17

Poland Page to print is 18

Portugal Page to print is 19

Russia Page to print is 20

Singapore Page to print is 21

South Africa Page to print is 22

Spain Page to print is 23

Sweden Page to print is 24

Switzerland Page to print is 25

UK Page to print is 26

USA Page to print is 27

Finished

關於報表設計引擎API的結構和使用方法,以及如何在報表引擎中調用報表設計引擎來修改報表,將在下一節詳細講解。

發佈了57 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章