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.*
一般报表设计器在产生一份包含图表的设计文档的时候,都要调用图表的设计和展示引擎,但图表引擎不仅仅包含在报表设计器的类库中,报表在展示阶段如果包含图表,或者需要修改图表的时候,也会调用图表的引擎。
构建一个报表的必须工作:
创建和配置报表引擎
单个报表引擎可以通过创建多个设计器来生成多个报表。通过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包中。
下面给出一个简单的例子:
该报表的设计如下:
数据集上有事件处理绑定:
需要在报表的资源中导入事件处理包:
预览如下:
报表引擎的源码如下:
- package REAPI;
- import java.util.logging.Level;
- import org.eclipse.birt.core.framework.Platform;
- import org.eclipse.birt.report.engine.api.EXCELRenderOption;
- import org.eclipse.birt.report.engine.api.EngineConfig;
- import org.eclipse.birt.report.engine.api.EngineConstants;
- import org.eclipse.birt.report.engine.api.EngineException;
- import org.eclipse.birt.report.engine.api.HTMLRenderOption;
- import org.eclipse.birt.report.engine.api.IReportEngine;
- import org.eclipse.birt.report.engine.api.IReportEngineFactory;
- import org.eclipse.birt.report.engine.api.IReportRunnable;
- import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
- import org.eclipse.birt.report.engine.api.PDFRenderOption;
- public class RunAndRenderTask {
- public void runReport() throws EngineException
- {
- IRunAndRenderTask task=null;
- IReportEngine engine=null;
- EngineConfig config = null;
- try{
- //System.setProperty("java.io.tmpdir", "c:/temp/test/testsampledb");
- config = new EngineConfig( );
- config.setLogConfig("d:\\dwn.txt", Level.INFO);
- config.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, this.getClass().getClassLoader());
- config.getAppContext().put(EngineConstants.WEBAPP_CLASSPATH_KEY, "d:\\eventjar.jar");
- config.setBIRTHome("E:\\birt汉化包\\birt-runtime-3_7_2\\ReportEngine");
- //config.setLogConfig(null, Level.FINEST);
- Platform.startup( config );
- IReportEngineFactory factory = (IReportEngineFactory) Platform
- .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
- engine = factory.createReportEngine( config );
- IReportRunnable design = null;
- //Open the report design
- design = engine.openReportDesign("d:\\dataseteventhandler.rptdesign");
- task = engine.createRunAndRenderTask(design);
- //task.setParameterValue("Top Count", (new Integer(5)));
- task.validateParameters();
- HTMLRenderOption options = new HTMLRenderOption();
- options.setImageDirectory("d:\\");
- options.setOutputFileName("d:\\eventhandlerjar.html");
- options.setOutputFormat("html");
- //PDFRenderOption options = new PDFRenderOption();
- //options.setOutputFileName("d:\\topn.pdf");
- //options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
- //options.setOutputFormat("pdf");
- //EXCELRenderOption options = new EXCELRenderOption();
- //options.setOutputFormat("xls");
- //options.setOutputFileName("d:\\customers.xls");
- //options.setWrappingText(true);
- task.setRenderOption(options);
- task.run();
- task.close();
- engine.destroy();
- }catch( Exception ex){
- ex.printStackTrace();
- }
- finally
- {
- if ( !task.getErrors( ).isEmpty( ) )
- {
- for ( Object e : task.getErrors( ) )
- {
- ( (Exception) e ).printStackTrace( );
- }
- }
- Platform.shutdown( );
- System.out.println("Finished");
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- try
- {
- RunAndRenderTask ex = new RunAndRenderTask( );
- ex.runReport();
- }
- catch ( Exception e )
- {
- e.printStackTrace();
- }
- }
- }
运行,会看到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),平台上下文,资源文件,临时资源文件,脚本等,这些都是可选的参数,详细信息可以参考EngineConfig的API。
不同运行环境的差异
应用程序是独立运行的还是以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 handler。HTMLCompleteImageHander已经提供了足够的图像文件接口因此一般情况下不需要我们进行扩展。
启动平台
设置完成平台上下文环境,你可以通过org.eclipse.birt.core.framework.Platform类启动一个平台。 Platform是一个Eclipse OSGI平台的包装类,提供一个同步的静态方法startup( )用来启动平台,该操作的开销比较大,因此你的应用程序最好只调用一次。平台使用结束后调用Platform.shutdown( )关闭。如果你是在Web应用程序里使用报表引擎,请在servlet的init方法中调用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对象创建报表引擎。
实例
在独立运行的应用程序中创建一个报表引擎,如下:
- // Create an EngineConfig object.
- EngineConfig config = new EngineConfig( );
- // Set up the path to your BIRT home directory.
- config.setBIRTHome("E:\\birt汉化包\\birt-runtime-3_7_2\\ReportEngine");
- // Explicitly set up the stand-alone application
- IPlatformContext context = new PlatformFileContext( );
- config.setEngineContext( context );
- // Start the platform for a non-RCP application.
- Platform.startup( config );
- IReportEngineFactory factory = ( IReportEngineFactory ) Platform.createFactoryObject
- ( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
- // Set up writing images or charts embedded in HTML output.
- HTMLRenderOption ho = new HTMLRenderOption( );
- ho.setImageHandler( new HTMLCompleteImageHandler( ));
- config.setEmitterConfiguration( RenderOptionBase.OUTPUT_FORMAT_HTML, ho );
- // Create the engine.
- IReportEngine engine = factory.createReportEngine( config );
说明:上面一个在windows系统上的独立运行的应用程序,它使用BIRT run-time提供的BIRT home,报表输出格式是HTML。
- 在<span style="font-family:Calibri;">Web</span><span style="font-family:宋体;">应用程序上创建一个报表引擎,如下:</span>
- // Example class to create the report engine
- public class BirtEngine {
- private static IReportEngine birtEngine = null;
- public static synchronized IReportEngine getBirtEngine( ServletContext sc ) {
- if (birtEngine == null) {
- EngineConfig config = new EngineConfig( );
- config.setBIRTHome( "" );
- IPlatformContext context = new PlatformServletContext( sc );
- config.setPlatformContext( context );
- try{
- Platform.startup( config );
- IReportEngineFactory factory = ( IReportEngineFactory )
- Platform.createFactoryObject(
- IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
- birtEngine = factory.createReportEngine( config );
- }catch ( Exception e ) { e.printStackTrace( ); }
- }
- return birtEngine;
- }
- }
说明:上面的代码是一个用单例模式实现的报表引擎的创建的类,EngineConfig配置了两个参数 BIRTHome和PlatformContext。
在上面例子的基础上进行更多的配置
- // In a different class, get the report engine
- reportEngine = BirtEngine.getBirtEngine( request.getSession( ).getServletContext( ));
- // Set up the engine
- EngineConfig config = reportEngine.getConfig( );
- HTMLRenderOption ho = new HTMLRenderOption( );
- ho.setImageHandler( new HTMLServerImageHandler( ));
- ho.setImageDirectory("output/image");
- ho.setBaseImageURL("http://myhost/prependme?image=");
- config.setEmitterConfiguration( RenderOptionBase.OUTPUT_FORMAT_HTML, ho );
使用日志环境调试应用程序
BIRT报表引擎使用java.util.logging的Logger和Level类来处理引擎平台的日志信息。你在Eclipse中运行一个 应用程序运行信息默认的是显示在控制台上的。在非Eclipse环境中运行信息的输出就要有运行环境决定了。默认的日志级别是Level.INFO,你可 以通过改变日志级别来减少系统内部的日志信息。
使用EngineConfig.setLogConfig( )可以让日志输出到磁盘文件中。该方法需要两个参数,第一个参数是日志输出的目录,BIRT 引擎会在指定目录中创建一个名称格式为ReportEngine
_YYYY_MM_DD_hh_mm_ss.log的日志文件。第二个参数是日志信息的最低级别,级别越高输出的日志信息越少。ReportEngine.changeLogLevel( )方法可以让你修改日志级别。
如何使用BIRT日志 如下:
- // Set up the location and level of the logging output.
- config.setLogConfig( "C:/Temp", Level.ERROR );
- engine.changeLogLevel( Level.INFO );
二、打开报表资源文件
BIRT引擎可以通过报表设计文件和报表文档两种方式生成报表,引擎还可以从报表设计文件生成报表文档。
你可以通过openReportDesign( )方法打开一个报表设计文件文件,他实例化一个IReportRunnable对象,支持文件路径或一个输入流。
你可以通过openReportDocument( )方法打开一个报表设计文件文件,他实例化一个IReportDocument对象,也支持文件路径或一个输入流。
使用这些方法要处理EngineException。
理解IReportRunnable对象
IReportRunnable对象提供到报表设计的基本属性的直接接口。报表设计属性的属性名是静态字符串变量,如:IReportRunnable.AUTHOR。可以通过getProperty( )方法去访问这些属性。下面的代码展示了如何连接到报表设计文件:
- String designName = "./SimpleReport.rptdesign";
- IReportRunnable runnable = null;
- try {
- runnable = engine.openReportDesign( designName );
- }catch ( EngineException e ) {
- System.err.println( "Design " + designName + " not found!" );
- engine.destroy( );
- System.exit( -1 );
- }
- // Get the value of a simple property.
- String author = ( String ) runnable.getProperty( IReportRunnable.AUTHOR );
理解IReportDocument对象
IReportDocument对象提供了访问报表数据和报表结构的接口,它提供了方法来访问表格内容,书签,页面信息等。
使用findTOC( )访问表格内容,该方法使用TOCNode参数返回一个TOCNode对象。使用null参数就可以访问根表格。
调用getTOCTree( )返回一个ITOCTree对象获取更详细的Table信息。调用TOCNode.getChildren( )方法获取table的子节点,返回值是一个TOCNode对象列表。
通过TOCNode对象你可以得到表格中的内容和书签。你可以使用书签对象作为参数调用getPageNumber( )方法获取书签连接的页面的页数。通过这些信息你可以在以格式化的报表中显示特定的页面。
下面的代码展示了如何打开一个报表文档,并在页面上查找元素:
- String dName = "./SimpleReport.rptdocument";
- IReportDocument doc = null;
- try {
- doc = engine.openReportDocument( dName );
- } catch ( EngineException e ) {
- System.err.println( "Document " + dName + " not found!" );
- engine.destroy( );
- System.exit( -1 );
- }
- // Get the root of the table of contents.
- TOCNode td = doc.findTOC( null );
- java.util.List children = td.getChildren( );
- long pNumber;
- // Loop through the top level table of contents entries.
- if ( children != null && children.size( ) > 0 ) {
- for ( int i = 0; i < children.size( ); i++ ) {
- // Find the required table of contents entry.
- TOCNode child = ( TOCNode ) children.get( i );
- if ( child.getDisplayString( ).equals( "103" ) ) {
- // Get the number of the page that contains the data.
- pNumber = doc.getPageNumber( child.getBookmark( ) );
- System.out.println( "Page to print is " + pNumber );
- }
- }
- }
三、用程序处理报表参数
参数是报表生成之前想报表设计文件输入的报表元素。报表的应用程序可以访问报表的参数的属性,最常用的属性是name和value,你可以使用参数名或通用编码来获取参数。
为报表设计创建一个参数定义任务
一个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( ),它返回一个数据类型是IScalarParameterDefn的ArrayList对象。
获取报表参数的默认值
这个任务是可选的。调用IGetParameterDefinitionTask.getDefaultValue( )获取一个已知参数的默认值,它返回的是一个Object。可以通过调用IScalarParameterDefn.getDataType( )来获取Object的有效类型,它返回一个IScalarParameterDefn定义的int类型的静态变量。调用 IGetParameterDefinitionTask.getDefaultValues( )来获取报表设计的所有参数的默认值,它返回一个HashMap对象存储了从参数名到默认值的映射。
使用常量列表提供的效值
许多报表参数只接受常量列表的值,这些值可能是静态常量,也可能是数据库里查出来的数据list。使用 IGetParameterDefinitionTask.getSelectionList( )可以返回参数所能接受的参数(IParameterSelectionChoice)的集合,如果集合为null,则可以接受任何值。 IParameterSelectionChoice的getLabel( )方法返回现实的文本,getValue( )返回值。
获取每个报表参数的属性
这个任务是可选的。使用IScalarParameterDefn的方法可以获取表参数的属性。应用程序使用属性生成用户自定义接口。例如,获取参数的数据类型使用getDataType( )方法。
为参数设置值
调用IGetParameterDefinitionTask.setParameterValue( )为参数设置值。如果你是通过应用程为日期数值等参数返回一个字符串,要把他们转化成和本地无关的格式。调用方法 ReportParameterConverter.parse( )方法可以完成这项工作。getParameterValues( )方法返回一个HashMap包含了已经设置的所有参数。
实例
下面的代码展示了如何设置一个已知参数名的参数值:
- // Create a parameter definition task.
- IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask( runnable );
- // Instantiate a scalar parameter.
- IScalarParameterDefn param = (IScalarParameterDefn)
- task.getParameterDefn( "customerID" );
- // Get the default value of the parameter. In this case,
- // the data type of the parameter, customerID, is Double.
- int customerID = ((Double) task.getDefaultValue( param )).intValue( );
- // Get a value for the parameter. This example assumes that
- // this step creates a correctly typed object, inputValue.
- // Set the value of the parameter.
- task.setParameterValue( "customerID", inputValue );
- // Get the values set by the application for all parameters.
- HashMap parameterValues = task.getParameterValues( );
- // Close the parameter definition task.
- task.close( );
下面的代码展示了如何使用报表参数集合,例子中使用ReportParameterConverter将字符串转化成用户接口接受的参数格式。
- // Create a parameter definition task.
- IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask( runnable );
- // Create a collection of the parameters in the report design.
- Collection params = task.getParameterDefns( false );
- // Get the default values of the parameters.
- HashMap parameterValues = task.getDefaultValues( );
- // Get values for the parameters. Later code in this example
- // assumes that this step creates a HashMap object,
- // inputValues. The keys in the HashMap are the parameter
- // names and the values are those that the user provided.
- // Iterate through the report parameters, setting the values
- // in standard locale-independent format.
- Iterator iterOuter = params.iterator( );
- ReportParameterConverter cfgConverter =
- new ReportParameterConverter( "", Locale.getDefault() );
- while ( iterOuter.hasNext( ) ) {
- IParameterDefnBase param = (IParameterDefnBase) iterOuter.next( );
- String value = (String) inputValues.get( param.getName( ));
- if ( value != null ) {
- parameterValues.put( param.getName( ),
- cfgConverter.parse( value, param.getDataType( ) ) );
- }
- }
- // Close the parameter definition task.
- task.close( );
使用级联参数
级联参数参数是一组可供用户选择的参数值的集合。第一个参数的选择会影响到第二个参数中的值。参数使用一个或多个查询来从数据集把数据展示给用户。参数定义任务根据前面的选择以行为单位过滤参数组中的数据。
使用报表引擎实现级联参数,要进行如下工作:
■使用IGetParameterDefinitionTask.evaluateQuery( )方法为级联参数准备数据,它需要参数group的名字作为参数。
■调用IGetParameterDefinitionTask.getSelectionListForCascadingGroup( )获取参数组的第一个参数的值,它需要两个参数,参数名和对象数组,对第一个参数来说这个数组是空的。该方法返回一个 IParameterSelectionChoice集合。
■ 再次调用getSelectionListForCascadingGroup( )方法获取后面的参数,这次Object[ ]中放的是前面获取的参数值。
下面的代码展示了如何通过查询实现级联参数:
- // Create a grouped collection of the design’s parameters.
- Collection params = task.getParameterDefns( true );
- // Iterate through the parameters to find the cascading group.
- Iterator iter = params.iterator( );
- while ( iter.hasNext( ) ) {
- IParameterDefnBase param = (IParameterDefnBase) iter.next();
- if ( param.getParameterType() ==
- IParameterDefnBase.CASCADING_PARAMETER_GROUP ) {
- ICascadingParameterGroup group = (ICascadingParameterGroup) param;
- Iterator i2 = group.getContents( ).iterator( );
- // Run the query for the cascading parameters.
- task.evaluateQuery( group.getName() );
- Object[ ] userValues = new Object[group.getContents( ).size( )];
- // Get the report parameters in the cascading group.
- int i = 0;
- while ( i2.hasNext( ) ) {
- IScalarParameterDefn member = (IScalarParameterDefn) i2.next( );
- // Get the values for the parameter.
- Object[ ] setValues = new Object[i];
- if ( i > 0 ) System.arraycopy( userValues, 0, setValues, 0, i );
- Collection c = task.getSelectionListForCascadingGroup(
- group.getName(),setValues );
- // Iterate through the values for the parameter.
- Iterator i3 = c.iterator();
- while ( i3.hasNext( ) ) {
- IParameterSelectionChoice s =
- ( IParameterSelectionChoice ) i3.next( );
- String choiceValue = s.getValue( );
- String choiceLabel = s.getLabel( );
- }
- // Get the value for the parameter from the list of
- // choices. This example does not provide the code for
- // this task.
- userValues[i] = inputChoiceValue;
- i++;
- }
- }
- }
四、报表生成的准备工作
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-in的CLASSPATH
■ 上下文环境中定义的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支持把以前实例化的对象拿过来重用。调用报表引擎前你要在应用程序中持有这个已经在内存中存在的对象。如果在脚本环境中使用,你传递对象给引擎后,脚本可以通过对象名在必要的是后获取对象。
可以通过EngineConfig或task得上下文环境为引擎提供对象。如下:
- MyObject mo = new MyObject( );
- config = new EngineConfig( );
- // Get the application context from the config or the task
- HashMap hm = config.getAppContext( );
- //HashMap hm = task.getAppContext( );
- hm.put( "MyCreatedObject", mo );
- config.setAppContext( hm );
- // To refer to this object in a BIRT script
- // 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( )设置。
■ 浮动的页脚,主页面的页脚默认直接显示在内容的下方。传一个boolean值false给方法 HTMLRenderOption.setPageFooterFloatFlag( ),强制页脚在页面底部的固定位置显示。这个设置将在生成的HTML中添加一个固定高度的DIV。
■主页的页边距,HTMLRenderOption.setOutputMasterPageMargins( )设置主页的页边距是否影响到HTML页面。
■ HTML分页,生成HTML时也支持分页。设置HTMLRenderOption.setHtmlPagination( )可以让HTML页面支持分页。
下面的实例展示了在IRunAndRenderTask对象上使用rendering选项:
- // Create a run and render task object.
- IRunAndRenderTask task = engine.createRunAndRenderTask( runnable );
- // Set values for all parameters in a HashMap, parameterValues
- task.setParameterValues( parameterValues );
- // Validate parameter values.
- boolean parametersAreGood = task.validateParameters( );
- // Set the name of an output file.
- HTMLRenderOption options = new HTMLRenderOption( );
- String output = name.replaceFirst( ".rptdesign", ".html" );
- options.setOutputFileName( output );
- options.setImageDirectory( "image" );
- options.setHtmlRtLFlag( true );
- options.setEmbeddable( false );
- // Apply the rendering options to the task.
- task.setRenderOption( options );
- // Run and close the task
- task.run();
- task.close();
设置PDF rendering选项
生成PDF格式的报表需要在报表中嵌入字体,或者使用非标准路径的字体,应用程序支持PDF rendering以下设置:
■ 字体路径,如果使用自定义路径下的字体,使用setFontDirectory( )进行设置。
■ 嵌入的字体,调用PDFRenderOption.setEmbededFont( )可以使用嵌入字体。
五、生成报表
IRunAndRenderTask和IRunTask对象的run方法可以生成报表,同时要处理run方法抛出的EngineException异常。
下面的代码展示了如何生成一个报表:
- try {
- task.run( );
- System.out.println( "Created Report " + output + "." );
- }catch ( EngineException e1 ) {
- System.err.println( "Report " + name + " run failed." );
- System.err.println( e1.toString( ) );
- }
- engine.destroy( );
六、取消报表运行任务
BIRT报表引擎支持检查引擎任务的状态,并取消这个任务。通常使用不同的进程来执行这些操作。
使用EngineTask.getStatus( )方法可以检查报表的运行状态,返回值有下面这些:
■ IEngineTask.STATUS_NOT_STARTED. 任务没有启动
■ IEngineTask.STATUS_RUNNING. 任务正在运行中
■ IEngineTask.STATUS_SUCCEEDED. 任务正常结束
■ IEngineTask.STATUS_FAILED. 任务没有正常执行
■ IEngineTask.STATUS_CANCELLED. 任务被取消
调用IEngineTask.cancel( )方法可以取消任务。如果一个正在运行的任务被取消了,它将停止生成报表,如果任务正在查询数据库,则数据库要继续执行完查询任务。
下面就是一个取消任务的例子:
- private class CancelReport extends Thread
- {
- private IEngineTask eTask;
- public CancelReport( String myName, IEngineTask task ) {
- super( myName );
- eTask = task;
- }
- public void cancel( ) {
- try {
- Thread.currentThread( ).sleep( 100 );
- eTask.cancel( );
- System.out.println( "#### Report cancelled #####" );
- }catch( Exception e )
- {
- e.printStackTrace();
- }
- }
- }
- CancelReport cancelThread = new CancelReport( "cancelReport", task);
- cancelThread.cancel( );
下面是一个运行并导出部分列数据的代码:
- package REAPI;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.logging.Level;
- import org.eclipse.birt.core.framework.Platform;
- import org.eclipse.birt.data.engine.api.IConditionalExpression;
- import org.eclipse.birt.data.engine.api.IFilterDefinition;
- import org.eclipse.birt.data.engine.api.querydefn.ConditionalExpression;
- import org.eclipse.birt.data.engine.api.querydefn.FilterDefinition;
- import org.eclipse.birt.data.engine.core.DataException;
- import org.eclipse.birt.report.engine.api.EngineConfig;
- import org.eclipse.birt.report.engine.api.EngineException;
- import org.eclipse.birt.report.engine.api.IDataExtractionTask;
- import org.eclipse.birt.report.engine.api.IDataIterator;
- import org.eclipse.birt.report.engine.api.IExtractionResults;
- import org.eclipse.birt.report.engine.api.IReportDocument;
- import org.eclipse.birt.report.engine.api.IReportEngine;
- import org.eclipse.birt.report.engine.api.IReportEngineFactory;
- import org.eclipse.birt.report.engine.api.IResultMetaData;
- import org.eclipse.birt.report.engine.api.IResultSetItem;
- public class DataExtract {
- static void readReport() throws EngineException
- {
- IReportEngine engine=null;
- EngineConfig config = null;
- try{
- config = new EngineConfig( );
- config.setBIRTHome("E:\\birt汉化包\\birt-runtime-3_7_2\\ReportEngine");
- config.setLogConfig(null, Level.OFF);
- Platform.startup( config );
- IReportEngineFactory factory = (IReportEngineFactory) Platform
- .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
- engine = factory.createReportEngine( config );
- }catch( Exception ex){
- ex.printStackTrace();
- }
- IReportDocument iReportDocument = engine.openReportDocument("d:\\customers.rptdocument");
- //Setup data extraction task
- IDataExtractionTask task = engine.createDataExtractionTask(iReportDocument);
- /**
- * returns the metadata corresponding to the data stored in the report
- * document. Could specify a component.
- */
- ArrayList resultSetList = (ArrayList)task.getResultSetList( );
- IResultSetItem resultItem = null;
- Iterator it = resultSetList.iterator();
- while (it.hasNext()) {
- resultItem = (IResultSetItem)it.next();
- System.out.println("ResultSetName ====> " + resultItem.getResultSetName( ));
- }
- resultItem = (IResultSetItem)resultSetList.get( 0 );
- String dispName = resultItem.getResultSetName( );
- //Name Tables for ease
- task.selectResultSet( dispName );
- //Return Only customers between 201-300
- IFilterDefinition[] FilterExpression = new IFilterDefinition[2];
- FilterExpression[0] = new FilterDefinition( new ConditionalExpression( "row[\"CUSTOMERNUMBER\"]",
- IConditionalExpression.OP_GE, "201", null ) );
- FilterExpression[1] = new FilterDefinition( new ConditionalExpression( "row[\"CUSTOMERNUMBER\"]",
- IConditionalExpression.OP_LT, "300", null ) );
- task.setFilters( FilterExpression );
- IExtractionResults iExtractResults = task.extract();
- IDataIterator iData = null;
- try{
- if ( iExtractResults != null )
- {
- iData = iExtractResults.nextResultIterator( );
- //Get metadata on retrieved results
- IResultMetaData irmd = iData.getResultMetaData();
- if ( iData != null ){
- int colCount = irmd.getColumnCount();
- System.out.println("Cloumn Count =" + colCount );
- for( int j=0; j< colCount; j++){
- System.out.println("Cloumn Name =" + irmd.getColumnName(j) );
- System.out.println("Cloumn Type =" + irmd.getColumnTypeName(j) );
- }
- while ( iData.next( ) )
- {
- //Just disply the first two columns
- Object objColumn1;
- Object objColumn2;
- try{
- objColumn1 = iData.getValue(4);
- }catch(DataException e){
- objColumn1 = new String("");
- }
- try{
- objColumn2 = iData.getValue(12);
- }catch(DataException e){
- objColumn2 = new String("");
- }
- System.out.println( objColumn1 + " , " + objColumn2 );
- }
- iData.close();
- }
- }
- }
- catch( Exception e){
- e.printStackTrace();
- }
- task.close();
- engine.destroy();
- Platform.shutdown();
- System.out.println("Finished");
- System.exit(0);
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- try
- {
- readReport( );
- }
- catch ( Exception e )
- {
- e.printStackTrace();
- }
- }
- }
其中customers.rptdesign的设计如下:
预览如下:
该源码打印了结果集中的所有表名,从第一个表中获取了结果集列属性信息,导出了客户号在201到300之间客户第4列和第12列信息
运行结果如下:
- ResultSetName ====> MyCustomerTable
- ResultSetName ====> innertable
- ResultSetName ====> innertable_1
- ResultSetName ====> innertable_2
- ResultSetName ====> innertable_3
- ResultSetName ====> innertable_4
- ResultSetName ====> innertable_5
- ResultSetName ====> innertable_6
- ResultSetName ====> innertable_7
- ResultSetName ====> innertable_8
- ResultSetName ====> innertable_9
- ResultSetName ====> innertable_10
- ResultSetName ====> innertable_11
- ResultSetName ====> innertable_12
- ResultSetName ====> innertable_13
- ResultSetName ====> innertable_14
- ResultSetName ====> innertable_15
- ResultSetName ====> innertable_16
- ResultSetName ====> innertable_17
- ResultSetName ====> innertable_18
- ResultSetName ====> innertable_19
- ResultSetName ====> innertable_20
- ResultSetName ====> innertable_21
- ResultSetName ====> innertable_22
- ResultSetName ====> innertable_23
- ResultSetName ====> innertable_24
- ResultSetName ====> innertable_25
- ResultSetName ====> innertable_26
- ResultSetName ====> innertable_27
- ResultSetName ====> innertable_28
- ResultSetName ====> innertable_29
- ResultSetName ====> innertable_30
- ResultSetName ====> innertable_31
- ResultSetName ====> innertable_32
- ResultSetName ====> innertable_33
- ResultSetName ====> innertable_34
- ResultSetName ====> innertable_35
- ResultSetName ====> innertable_36
- ResultSetName ====> innertable_37
- ResultSetName ====> innertable_38
- ResultSetName ====> innertable_39
- ResultSetName ====> innertable_40
- ResultSetName ====> innertable_41
- ResultSetName ====> innertable_42
- ResultSetName ====> innertable_43
- ResultSetName ====> innertable_44
- ResultSetName ====> innertable_45
- ResultSetName ====> innertable_46
- ResultSetName ====> innertable_47
- ResultSetName ====> innertable_48
- ResultSetName ====> innertable_49
- ResultSetName ====> innertable_50
- ResultSetName ====> innertable_51
- ResultSetName ====> innertable_52
- ResultSetName ====> innertable_53
- ResultSetName ====> innertable_54
- ResultSetName ====> innertable_55
- ResultSetName ====> innertable_56
- ResultSetName ====> innertable_57
- ResultSetName ====> innertable_58
- ResultSetName ====> innertable_59
- ResultSetName ====> innertable_60
- ResultSetName ====> innertable_61
- ResultSetName ====> innertable_62
- ResultSetName ====> innertable_63
- ResultSetName ====> innertable_64
- ResultSetName ====> innertable_65
- ResultSetName ====> innertable_66
- ResultSetName ====> innertable_67
- ResultSetName ====> innertable_68
- ResultSetName ====> innertable_69
- ResultSetName ====> innertable_70
- ResultSetName ====> innertable_71
- ResultSetName ====> innertable_72
- ResultSetName ====> innertable_73
- ResultSetName ====> innertable_74
- ResultSetName ====> innertable_75
- ResultSetName ====> innertable_76
- ResultSetName ====> innertable_77
- ResultSetName ====> innertable_78
- ResultSetName ====> innertable_79
- ResultSetName ====> innertable_80
- ResultSetName ====> innertable_81
- ResultSetName ====> innertable_82
- ResultSetName ====> innertable_83
- ResultSetName ====> innertable_84
- ResultSetName ====> innertable_85
- ResultSetName ====> innertable_86
- ResultSetName ====> innertable_87
- ResultSetName ====> innertable_88
- ResultSetName ====> innertable_89
- ResultSetName ====> innertable_90
- ResultSetName ====> innertable_91
- ResultSetName ====> innertable_92
- ResultSetName ====> innertable_93
- ResultSetName ====> innertable_94
- ResultSetName ====> innertable_95
- ResultSetName ====> innertable_96
- ResultSetName ====> innertable_97
- ResultSetName ====> innertable_98
- ResultSetName ====> innertable_99
- ResultSetName ====> innertable_100
- ResultSetName ====> innertable_101
- ResultSetName ====> innertable_102
- ResultSetName ====> innertable_103
- ResultSetName ====> innertable_104
- ResultSetName ====> innertable_105
- ResultSetName ====> innertable_106
- ResultSetName ====> innertable_107
- ResultSetName ====> innertable_108
- ResultSetName ====> innertable_109
- ResultSetName ====> innertable_110
- ResultSetName ====> innertable_111
- ResultSetName ====> innertable_112
- ResultSetName ====> innertable_113
- ResultSetName ====> innertable_114
- ResultSetName ====> innertable_115
- ResultSetName ====> innertable_116
- ResultSetName ====> innertable_117
- ResultSetName ====> innertable_118
- ResultSetName ====> innertable_119
- ResultSetName ====> innertable_120
- ResultSetName ====> innertable_121
- Cloumn Count =13
- Cloumn Name =CUSTOMERNUMBER
- Cloumn Type =Integer
- Cloumn Name =CUSTOMERNAME
- Cloumn Type =String
- Cloumn Name =CONTACTLASTNAME
- Cloumn Type =String
- Cloumn Name =CONTACTFIRSTNAME
- Cloumn Type =String
- Cloumn Name =PHONE
- Cloumn Type =String
- Cloumn Name =ADDRESSLINE1
- Cloumn Type =String
- Cloumn Name =ADDRESSLINE2
- Cloumn Type =String
- Cloumn Name =CITY
- Cloumn Type =String
- Cloumn Name =STATE
- Cloumn Type =String
- Cloumn Name =POSTALCODE
- Cloumn Type =String
- Cloumn Name =COUNTRY
- Cloumn Type =String
- Cloumn Name =SALESREPEMPLOYEENUMBER
- Cloumn Type =Integer
- Cloumn Name =CREDITLIMIT
- Cloumn Type =Float
- 02 9936 8555 , 107800.0
- +61 2 9495 8555 , 93300.0
- (604) 555-3392 , 90300.0
- (514) 555-8054 , 48700.0
- (604) 555-4555 , 89600.0
- 86 21 3555 , 120800.0
- 88.60.1555 , 53800.0
- 61.77.6555 , 61100.0
- +33 1 46 62 7555 , 68100.0
- 30.59.8555 , 77900.0
- 0372-555188 , 0.0
- 069-0555984 , 0.0
- 0221-5554327 , 120400.0
- 089-0877555 , 0.0
- +852 2251 1555 , 58600.0
- 011-4988555 , 113000.0
- 035-640555 , 119600.0
- +47 2212 1555 , 95100.0
- +612 9411 1555 , 0.0
- (93) 203 4555 , 60300.0
- (91) 745 6555 , 0.0
- +41 26 425 50 01 , 0.0
- 0897-034555 , 141300.0
- (171) 555-2282 , 92700.0
- (198) 555-8888 , 93900.0
- 6175557555 , 68700.0
- 6265557265 , 90700.0
- 3105552373 , 11000.0
- 7605558146 , 105000.0
- 6175558555 , 123700.0
- Finished
下面这个案例运行报表,并导出html文件:
- package REAPI;
- import java.util.logging.Level;
- import org.eclipse.birt.core.framework.Platform;
- import org.eclipse.birt.report.engine.api.EXCELRenderOption;
- import org.eclipse.birt.report.engine.api.EngineConfig;
- import org.eclipse.birt.report.engine.api.EngineException;
- import org.eclipse.birt.report.engine.api.HTMLRenderOption;
- import org.eclipse.birt.report.engine.api.IPDFRenderOption;
- import org.eclipse.birt.report.engine.api.IRenderOption;
- import org.eclipse.birt.report.engine.api.IRenderTask;
- import org.eclipse.birt.report.engine.api.IReportDocument;
- import org.eclipse.birt.report.engine.api.IReportEngine;
- import org.eclipse.birt.report.engine.api.IReportEngineFactory;
- import org.eclipse.birt.report.engine.api.PDFRenderOption;
- import org.eclipse.birt.report.engine.api.RenderOption;
- public class RenderTask {
- public void runReport() throws EngineException
- {
- IReportEngine engine=null;
- EngineConfig config = null;
- try{
- config = new EngineConfig( );
- config.setBIRTHome("E:\\birt汉化包\\birt-runtime-3_7_2\\ReportEngine");
- config.setLogConfig(null, Level.FINE);
- Platform.startup( config );
- IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
- engine = factory.createReportEngine( config );
- }catch( Exception ex){
- ex.printStackTrace();
- }
- IReportDocument document = null;
- //Open the report design
- document = engine.openReportDocument("d:\\ps.rptdocument");
- IRenderOption options = new RenderOption();
- options.setOutputFormat("html");
- options.setOutputFileName("d:\\ps.html");
- options.setOption( IRenderOption.HTML_PAGINATION,
- Boolean.TRUE );
- //
- // if( options.getOutputFormat().equalsIgnoreCase("html")){
- // HTMLRenderOption htmlOptions = new HTMLRenderOption( options);
- // htmlOptions.setImageDirectory("output/image");
- // //set this if you want your image source url to be altered
- // //htmlOptions.setBaseImageURL("http://myhos/prependme?image=");
- // htmlOptions.setHtmlRtLFlag(false);
- // htmlOptions.setEmbeddable(false);
- // }else if( options.getOutputFormat().equalsIgnoreCase("pdf") ){
- //
- // PDFRenderOption pdfOptions = new PDFRenderOption( options );
- // /* CLIP_CONTENT: clip the content
- // * FIT_TO_PAGE_SIZE: scale the content to fit into the page
- // * OUTPUT_TO_MULTIPLE_PAGES: divided the content into multiple pages
- // * ENLARGE_PAGE_SIZE: enlarge the page size to contain all the content.
- // */
- // pdfOptions.setOption( IPDFRenderOption.PAGE_OVERFLOW, Integer.valueOf( PDFRenderOption.FIT_TO_PAGE_SIZE ) );
- //
- // }
- IRenderTask task = engine.createRenderTask(document);
- task.setRenderOption(options);
- //task.setPageRange("1-3");
- //task.setPageNumber(0);
- //task.setReportlet("chart3");
- task.render();
- task.close();
- document.close();
- engine.destroy();
- Platform.shutdown();
- System.out.println("Finished");
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- try
- {
- RenderTask ex = new RenderTask( );
- ex.runReport();
- }
- catch ( Exception e )
- {
- e.printStackTrace();
- }
- System.exit(0);
- }
- }
报表的设计如下:
运行源码,可以看到D盘导出的文件:
而下面的这段源码,打印了报表的所有参数:
- package REAPI;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.logging.Level;
- import org.eclipse.birt.core.framework.Platform;
- import org.eclipse.birt.report.engine.api.EngineConfig;
- import org.eclipse.birt.report.engine.api.EngineException;
- import org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask;
- import org.eclipse.birt.report.engine.api.IParameterDefnBase;
- import org.eclipse.birt.report.engine.api.IParameterGroupDefn;
- import org.eclipse.birt.report.engine.api.IParameterSelectionChoice;
- import org.eclipse.birt.report.engine.api.IReportEngine;
- import org.eclipse.birt.report.engine.api.IReportEngineFactory;
- import org.eclipse.birt.report.engine.api.IReportRunnable;
- import org.eclipse.birt.report.engine.api.IScalarParameterDefn;
- import org.eclipse.birt.report.model.api.CascadingParameterGroupHandle;
- import org.eclipse.birt.report.model.api.ScalarParameterHandle;
- public class ReportParameters {
- @SuppressWarnings("unchecked")
- static void executeReport() throws EngineException
- {
- HashMap parmDetails = new HashMap();
- IReportEngine engine=null;
- EngineConfig config = null;
- try{
- config = new EngineConfig( );
- config.setBIRTHome("E:\\birt汉化包\\birt-runtime-3_7_2\\ReportEngine");
- config.setLogConfig(null, Level.OFF);
- Platform.startup( config );
- IReportEngineFactory factory = (IReportEngineFactory) Platform
- .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
- engine = factory.createReportEngine( config );
- }catch( Exception ex){
- ex.printStackTrace();
- }
- //Open a report design
- IReportRunnable design = engine.openReportDesign("d:\\parameters.rptdesign");
- IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask( design );
- Collection params = task.getParameterDefns( true );
- //task.getSelectionListForCascadingGroup();
- Iterator iter = params.iterator( );
- while ( iter.hasNext( ) )
- {
- IParameterDefnBase param = (IParameterDefnBase) iter.next( );
- if ( param instanceof IParameterGroupDefn )
- {
- IParameterGroupDefn group = (IParameterGroupDefn) param;
- System.out.println( "Parameter Group: " + group.getName( ) );
- // Do something with the parameter group.
- // Iterate over group contents.
- Iterator i2 = group.getContents( ).iterator( );
- while ( i2.hasNext( ) )
- {
- IScalarParameterDefn scalar = (IScalarParameterDefn) i2.next( );
- //System.out.println("\t" + scalar.getName());
- parmDetails.put( scalar.getName(), loadParameterDetails( task, scalar, design, group));
- }
- }
- else
- {
- IScalarParameterDefn scalar = (IScalarParameterDefn) param;
- //System.out.println(param.getName());
- parmDetails.put( scalar.getName(),loadParameterDetails( task, scalar, design, null));
- }
- }
- task.close();
- engine.destroy();
- Platform.shutdown();
- System.out.println("Finished");
- }
- private static HashMap loadParameterDetails(IGetParameterDefinitionTask task, IScalarParameterDefn scalar, IReportRunnable report, IParameterGroupDefn group){
- HashMap parameter = new HashMap();
- if( group == null){
- parameter.put("Parameter Group", "Default");
- }else{
- parameter.put("Parameter Group", group.getName());
- }
- parameter.put("Name", scalar.getName());
- parameter.put("Help Text", scalar.getHelpText());
- parameter.put("Display Name", scalar.getDisplayName());
- //this is a format code such as > for UPPERCASE
- parameter.put("Display Format", scalar.getDisplayFormat());
- if( scalar.isHidden() ){
- parameter.put("Hidden", "Yes");
- }else{
- parameter.put("Hidden", "No");
- }
- if( scalar.isHidden() ){
- parameter.put("Hidden", "Yes");
- }else{
- parameter.put("Hidded", "No");
- }
- if( scalar.isRequired() ){
- parameter.put("Is Required", "Yes");
- }else{
- parameter.put("Is Required", "No");
- }
- if( scalar.isValueConcealed() ){
- parameter.put("Conceal Entry", "Yes"); //ie passwords etc
- }else{
- parameter.put("Conceal Entry", "No");
- }
- switch (scalar.getControlType()) {
- case IScalarParameterDefn.TEXT_BOX: parameter.put("Type", "Text Box"); break;
- case IScalarParameterDefn.LIST_BOX: parameter.put("Type", "List Box"); break;
- case IScalarParameterDefn.RADIO_BUTTON: parameter.put("Type", "List Box"); break;
- case IScalarParameterDefn.CHECK_BOX: parameter.put("Type", "List Box"); break;
- default: parameter.put("Type", "Text Box");break;
- }
- switch (scalar.getDataType()) {
- case IScalarParameterDefn.TYPE_STRING: parameter.put("Data Type", "String"); break;
- case IScalarParameterDefn.TYPE_FLOAT: parameter.put("Data Type", "Float"); break;
- case IScalarParameterDefn.TYPE_DECIMAL: parameter.put("Data Type", "Decimal"); break;
- case IScalarParameterDefn.TYPE_DATE_TIME: parameter.put("Data Type", "Date Time"); break;
- case IScalarParameterDefn.TYPE_BOOLEAN: parameter.put("Data Type", "Boolean"); break;
- default: parameter.put("Data Type", "Any"); break;
- }
- ScalarParameterHandle parameterHandle = ( ScalarParameterHandle ) scalar.getHandle();
- parameter.put("Default Value", scalar.getDefaultValue());
- parameter.put("Prompt Text", scalar.getPromptText());
- parameter.put("Data Set Expression", parameterHandle.getValueExpr());
- if(scalar.getControlType() != IScalarParameterDefn.TEXT_BOX)
- {
- //System.out.println("dynamic parameter");
- if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle ){
- Collection sList = Collections.EMPTY_LIST;
- if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle )
- {
- String groupName = parameterHandle.getContainer( ).getName( );
- //used for Cascading parms see IGetParameterDefinitionTask.java code for comments
- //task.evaluateQuery( groupName );
- //Need to load this for calls to get next level.
- //This just gets the first level
- Object [] keyValueTmp = new Object[1];
- sList = task.getSelectionListForCascadingGroup( groupName, keyValueTmp );
- for ( Iterator sl = sList.iterator( ); sl.hasNext( ); )
- {
- IParameterSelectionChoice sI = (IParameterSelectionChoice) sl.next( );
- Object value = sI.getValue( );
- Object label = sI.getLabel( );
- System.out.println( label + "--" + value);
- }
- }
- }else{
- Collection selectionList = task.getSelectionList( scalar.getName() );
- if ( selectionList != null )
- {
- HashMap dynamicList = new HashMap();
- for ( Iterator sliter = selectionList.iterator( ); sliter.hasNext( ); )
- {
- IParameterSelectionChoice selectionItem = (IParameterSelectionChoice) sliter.next( );
- Object value = selectionItem.getValue( );
- String label = selectionItem.getLabel( );
- //System.out.println( label + "--" + value);
- //Display label unless null then display value. Value is the what should get passed to the report.
- dynamicList.put(value,label);
- }
- parameter.put("Selection List", dynamicList);
- }
- }
- }
- Iterator iter = parameter.keySet().iterator();
- System.out.println("======================Parameter =" + scalar.getName());
- while (iter.hasNext()) {
- String name = (String) iter.next();
- if( name.equals("Selection List")){
- HashMap selList = (HashMap)parameter.get(name);
- Iterator selIter = selList.keySet().iterator();
- while (selIter.hasNext()) {
- Object lbl = selIter.next();
- System.out.println( "Selection List Entry ===== Key = " + lbl + " Value = " + selList.get(lbl));
- }
- }else{
- System.out.println( name + " = " + parameter.get(name));
- }
- }
- return parameter;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- try
- {
- executeReport( );
- }
- catch ( Exception e )
- {
- e.printStackTrace();
- }
- }
- }
运行结果如下:
- ======================Parameter =parameter1
- Hidded = No
- Default Value = 1
- Type = Text Box
- Conceal Entry = No
- Hidden = No
- Data Set Expression = null
- Name = parameter1
- Is Required = Yes
- Help Text = null
- Parameter Group = Default
- Display Name = null
- Display Format = #,##0.00%
- Data Type = Decimal
- Prompt Text = "This i prompt text"
- Parameter Group: ParmGroup1
- ======================Parameter =Parameter2
- Hidded = No
- Default Value = parm2
- Type = Text Box
- Conceal Entry = No
- Hidden = No
- Data Set Expression = null
- Name = Parameter2
- Is Required = No
- Help Text = null
- Parameter Group = ParmGroup1
- Display Name = null
- Display Format = Unformatted
- Data Type = String
- Prompt Text = null
- ======================Parameter =Parameter3
- Hidded = No
- Default Value = 10101
- Type = List Box
- Conceal Entry = No
- Hidden = No
- Selection List Entry ===== Key = 10346 Value = 10346
- Selection List Entry ===== Key = 10207 Value = 10207
- Selection List Entry ===== Key = 10347 Value = 10347
- Selection List Entry ===== Key = 10208 Value = 10208
- Selection List Entry ===== Key = 10344 Value = 10344
- Selection List Entry ===== Key = 10205 Value = 10205
- Selection List Entry ===== Key = 10345 Value = 10345
- Selection List Entry ===== Key = 10206 Value = 10206
- Selection List Entry ===== Key = 10342 Value = 10342
- Selection List Entry ===== Key = 10343 Value = 10343
- Selection List Entry ===== Key = 10340 Value = 10340
- Selection List Entry ===== Key = 10209 Value = 10209
- Selection List Entry ===== Key = 10341 Value = 10341
- Selection List Entry ===== Key = 10200 Value = 10200
- Selection List Entry ===== Key = 10203 Value = 10203
- Selection List Entry ===== Key = 10204 Value = 10204
- Selection List Entry ===== Key = 10348 Value = 10348
- Selection List Entry ===== Key = 10201 Value = 10201
- Selection List Entry ===== Key = 10349 Value = 10349
- Selection List Entry ===== Key = 10202 Value = 10202
- Selection List Entry ===== Key = 10333 Value = 10333
- Selection List Entry ===== Key = 10334 Value = 10334
- Selection List Entry ===== Key = 10335 Value = 10335
- Selection List Entry ===== Key = 10336 Value = 10336
- Selection List Entry ===== Key = 10330 Value = 10330
- Selection List Entry ===== Key = 10331 Value = 10331
- Selection List Entry ===== Key = 10332 Value = 10332
- Selection List Entry ===== Key = 10337 Value = 10337
- Selection List Entry ===== Key = 10338 Value = 10338
- Selection List Entry ===== Key = 10339 Value = 10339
- Selection List Entry ===== Key = 10320 Value = 10320
- Selection List Entry ===== Key = 10321 Value = 10321
- Selection List Entry ===== Key = 10324 Value = 10324
- Selection List Entry ===== Key = 10229 Value = 10229
- Selection List Entry ===== Key = 10325 Value = 10325
- Selection List Entry ===== Key = 10322 Value = 10322
- Selection List Entry ===== Key = 10227 Value = 10227
- Selection List Entry ===== Key = 10323 Value = 10323
- Selection List Entry ===== Key = 10228 Value = 10228
- Selection List Entry ===== Key = 10328 Value = 10328
- Selection List Entry ===== Key = 10225 Value = 10225
- Selection List Entry ===== Key = 10329 Value = 10329
- Selection List Entry ===== Key = 10226 Value = 10226
- Selection List Entry ===== Key = 10326 Value = 10326
- Selection List Entry ===== Key = 10223 Value = 10223
- Selection List Entry ===== Key = 10327 Value = 10327
- Selection List Entry ===== Key = 10224 Value = 10224
- Selection List Entry ===== Key = 10221 Value = 10221
- Selection List Entry ===== Key = 10222 Value = 10222
- Selection List Entry ===== Key = 10220 Value = 10220
- Selection List Entry ===== Key = 10310 Value = 10310
- Selection List Entry ===== Key = 10311 Value = 10311
- Selection List Entry ===== Key = 10216 Value = 10216
- Selection List Entry ===== Key = 10312 Value = 10312
- Selection List Entry ===== Key = 10217 Value = 10217
- Selection List Entry ===== Key = 10313 Value = 10313
- Selection List Entry ===== Key = 10218 Value = 10218
- Selection List Entry ===== Key = 10314 Value = 10314
- Selection List Entry ===== Key = 10219 Value = 10219
- Selection List Entry ===== Key = 10315 Value = 10315
- Selection List Entry ===== Key = 10212 Value = 10212
- Selection List Entry ===== Key = 10316 Value = 10316
- Selection List Entry ===== Key = 10213 Value = 10213
- Selection List Entry ===== Key = 10317 Value = 10317
- Selection List Entry ===== Key = 10214 Value = 10214
- Selection List Entry ===== Key = 10318 Value = 10318
- Selection List Entry ===== Key = 10215 Value = 10215
- Selection List Entry ===== Key = 10319 Value = 10319
- Selection List Entry ===== Key = 10210 Value = 10210
- Selection List Entry ===== Key = 10211 Value = 10211
- Selection List Entry ===== Key = 10244 Value = 10244
- Selection List Entry ===== Key = 10243 Value = 10243
- Selection List Entry ===== Key = 10242 Value = 10242
- Selection List Entry ===== Key = 10241 Value = 10241
- Selection List Entry ===== Key = 10248 Value = 10248
- Selection List Entry ===== Key = 10247 Value = 10247
- Selection List Entry ===== Key = 10246 Value = 10246
- Selection List Entry ===== Key = 10245 Value = 10245
- Selection List Entry ===== Key = 10389 Value = 10389
- Selection List Entry ===== Key = 10388 Value = 10388
- Selection List Entry ===== Key = 10249 Value = 10249
- Selection List Entry ===== Key = 10387 Value = 10387
- Selection List Entry ===== Key = 10386 Value = 10386
- Selection List Entry ===== Key = 10385 Value = 10385
- Selection List Entry ===== Key = 10384 Value = 10384
- Selection List Entry ===== Key = 10382 Value = 10382
- Selection List Entry ===== Key = 10383 Value = 10383
- Selection List Entry ===== Key = 10380 Value = 10380
- Selection List Entry ===== Key = 10381 Value = 10381
- Selection List Entry ===== Key = 10240 Value = 10240
- Selection List Entry ===== Key = 10231 Value = 10231
- Selection List Entry ===== Key = 10230 Value = 10230
- Selection List Entry ===== Key = 10233 Value = 10233
- Selection List Entry ===== Key = 10232 Value = 10232
- Selection List Entry ===== Key = 10235 Value = 10235
- Selection List Entry ===== Key = 10234 Value = 10234
- Selection List Entry ===== Key = 10237 Value = 10237
- Selection List Entry ===== Key = 10236 Value = 10236
- Selection List Entry ===== Key = 10378 Value = 10378
- Selection List Entry ===== Key = 10239 Value = 10239
- Selection List Entry ===== Key = 10377 Value = 10377
- Selection List Entry ===== Key = 10238 Value = 10238
- Selection List Entry ===== Key = 10379 Value = 10379
- Selection List Entry ===== Key = 10374 Value = 10374
- Selection List Entry ===== Key = 10373 Value = 10373
- Selection List Entry ===== Key = 10376 Value = 10376
- Selection List Entry ===== Key = 10375 Value = 10375
- Selection List Entry ===== Key = 10370 Value = 10370
- Selection List Entry ===== Key = 10371 Value = 10371
- Selection List Entry ===== Key = 10372 Value = 10372
- Selection List Entry ===== Key = 10269 Value = 10269
- Selection List Entry ===== Key = 10268 Value = 10268
- Selection List Entry ===== Key = 10267 Value = 10267
- Selection List Entry ===== Key = 10266 Value = 10266
- Selection List Entry ===== Key = 10265 Value = 10265
- Selection List Entry ===== Key = 10264 Value = 10264
- Selection List Entry ===== Key = 10263 Value = 10263
- Selection List Entry ===== Key = 10365 Value = 10365
- Selection List Entry ===== Key = 10364 Value = 10364
- Selection List Entry ===== Key = 10363 Value = 10363
- Selection List Entry ===== Key = 10362 Value = 10362
- Selection List Entry ===== Key = 10369 Value = 10369
- Selection List Entry ===== Key = 10368 Value = 10368
- Selection List Entry ===== Key = 10367 Value = 10367
- Selection List Entry ===== Key = 10366 Value = 10366
- Selection List Entry ===== Key = 10360 Value = 10360
- Selection List Entry ===== Key = 10361 Value = 10361
- Selection List Entry ===== Key = 10261 Value = 10261
- Selection List Entry ===== Key = 10262 Value = 10262
- Selection List Entry ===== Key = 10260 Value = 10260
- Selection List Entry ===== Key = 10257 Value = 10257
- Selection List Entry ===== Key = 10359 Value = 10359
- Selection List Entry ===== Key = 10256 Value = 10256
- Selection List Entry ===== Key = 10259 Value = 10259
- Selection List Entry ===== Key = 10258 Value = 10258
- Selection List Entry ===== Key = 10253 Value = 10253
- Selection List Entry ===== Key = 10252 Value = 10252
- Selection List Entry ===== Key = 10255 Value = 10255
- Selection List Entry ===== Key = 10254 Value = 10254
- Selection List Entry ===== Key = 10352 Value = 10352
- Selection List Entry ===== Key = 10351 Value = 10351
- Selection List Entry ===== Key = 10354 Value = 10354
- Selection List Entry ===== Key = 10353 Value = 10353
- Selection List Entry ===== Key = 10356 Value = 10356
- Selection List Entry ===== Key = 10355 Value = 10355
- Selection List Entry ===== Key = 10358 Value = 10358
- Selection List Entry ===== Key = 10357 Value = 10357
- Selection List Entry ===== Key = 10350 Value = 10350
- Selection List Entry ===== Key = 10250 Value = 10250
- Selection List Entry ===== Key = 10251 Value = 10251
- Selection List Entry ===== Key = 10152 Value = 10152
- Selection List Entry ===== Key = 10151 Value = 10151
- Selection List Entry ===== Key = 10150 Value = 10150
- Selection List Entry ===== Key = 10130 Value = 10130
- Selection List Entry ===== Key = 10400 Value = 10400
- Selection List Entry ===== Key = 10402 Value = 10402
- Selection List Entry ===== Key = 10401 Value = 10401
- Selection List Entry ===== Key = 10404 Value = 10404
- Selection List Entry ===== Key = 10291 Value = 10291
- Selection List Entry ===== Key = 10403 Value = 10403
- Selection List Entry ===== Key = 10290 Value = 10290
- Selection List Entry ===== Key = 10406 Value = 10406
- Selection List Entry ===== Key = 10405 Value = 10405
- Selection List Entry ===== Key = 10408 Value = 10408
- Selection List Entry ===== Key = 10295 Value = 10295
- Selection List Entry ===== Key = 10407 Value = 10407
- Selection List Entry ===== Key = 10294 Value = 10294
- Selection List Entry ===== Key = 10293 Value = 10293
- Selection List Entry ===== Key = 10409 Value = 10409
- Selection List Entry ===== Key = 10292 Value = 10292
- Selection List Entry ===== Key = 10298 Value = 10298
- Selection List Entry ===== Key = 10299 Value = 10299
- Selection List Entry ===== Key = 10296 Value = 10296
- Selection List Entry ===== Key = 10297 Value = 10297
- Selection List Entry ===== Key = 10139 Value = 10139
- Selection List Entry ===== Key = 10159 Value = 10159
- Selection List Entry ===== Key = 10135 Value = 10135
- Selection List Entry ===== Key = 10136 Value = 10136
- Selection List Entry ===== Key = 10157 Value = 10157
- Selection List Entry ===== Key = 10137 Value = 10137
- Selection List Entry ===== Key = 10158 Value = 10158
- Selection List Entry ===== Key = 10138 Value = 10138
- Selection List Entry ===== Key = 10155 Value = 10155
- Selection List Entry ===== Key = 10131 Value = 10131
- Selection List Entry ===== Key = 10156 Value = 10156
- Selection List Entry ===== Key = 10132 Value = 10132
- Selection List Entry ===== Key = 10153 Value = 10153
- Selection List Entry ===== Key = 10133 Value = 10133
- Selection List Entry ===== Key = 10154 Value = 10154
- Selection List Entry ===== Key = 10134 Value = 10134
- Selection List Entry ===== Key = 10161 Value = 10161
- Selection List Entry ===== Key = 10141 Value = 10141
- Selection List Entry ===== Key = 10160 Value = 10160
- Selection List Entry ===== Key = 10140 Value = 10140
- Selection List Entry ===== Key = 10163 Value = 10163
- Selection List Entry ===== Key = 10162 Value = 10162
- Selection List Entry ===== Key = 10168 Value = 10168
- Selection List Entry ===== Key = 10148 Value = 10148
- Selection List Entry ===== Key = 10169 Value = 10169
- Selection List Entry ===== Key = 10149 Value = 10149
- Selection List Entry ===== Key = 10146 Value = 10146
- Selection List Entry ===== Key = 10147 Value = 10147
- Selection List Entry ===== Key = 10164 Value = 10164
- Selection List Entry ===== Key = 10144 Value = 10144
- Selection List Entry ===== Key = 10165 Value = 10165
- Selection List Entry ===== Key = 10145 Value = 10145
- Selection List Entry ===== Key = 10166 Value = 10166
- Selection List Entry ===== Key = 10142 Value = 10142
- Selection List Entry ===== Key = 10167 Value = 10167
- Selection List Entry ===== Key = 10143 Value = 10143
- Selection List Entry ===== Key = 10422 Value = 10422
- Selection List Entry ===== Key = 10170 Value = 10170
- Selection List Entry ===== Key = 10421 Value = 10421
- Selection List Entry ===== Key = 10424 Value = 10424
- Selection List Entry ===== Key = 10423 Value = 10423
- Selection List Entry ===== Key = 10174 Value = 10174
- Selection List Entry ===== Key = 10173 Value = 10173
- Selection List Entry ===== Key = 10420 Value = 10420
- Selection List Entry ===== Key = 10172 Value = 10172
- Selection List Entry ===== Key = 10171 Value = 10171
- Selection List Entry ===== Key = 10273 Value = 10273
- Selection List Entry ===== Key = 10272 Value = 10272
- Selection List Entry ===== Key = 10271 Value = 10271
- Selection List Entry ===== Key = 10270 Value = 10270
- Selection List Entry ===== Key = 10425 Value = 10425
- Selection List Entry ===== Key = 10117 Value = 10117
- Selection List Entry ===== Key = 10118 Value = 10118
- Selection List Entry ===== Key = 10119 Value = 10119
- Selection List Entry ===== Key = 10278 Value = 10278
- Selection List Entry ===== Key = 10279 Value = 10279
- Selection List Entry ===== Key = 10276 Value = 10276
- Selection List Entry ===== Key = 10277 Value = 10277
- Selection List Entry ===== Key = 10274 Value = 10274
- Selection List Entry ===== Key = 10275 Value = 10275
- Selection List Entry ===== Key = 10177 Value = 10177
- Selection List Entry ===== Key = 10178 Value = 10178
- Selection List Entry ===== Key = 10110 Value = 10110
- Selection List Entry ===== Key = 10175 Value = 10175
- Selection List Entry ===== Key = 10111 Value = 10111
- Selection List Entry ===== Key = 10176 Value = 10176
- Selection List Entry ===== Key = 10112 Value = 10112
- Selection List Entry ===== Key = 10113 Value = 10113
- Selection List Entry ===== Key = 10114 Value = 10114
- Selection List Entry ===== Key = 10179 Value = 10179
- Selection List Entry ===== Key = 10115 Value = 10115
- Selection List Entry ===== Key = 10116 Value = 10116
- Selection List Entry ===== Key = 10413 Value = 10413
- Selection List Entry ===== Key = 10412 Value = 10412
- Selection List Entry ===== Key = 10411 Value = 10411
- Selection List Entry ===== Key = 10181 Value = 10181
- Selection List Entry ===== Key = 10410 Value = 10410
- Selection List Entry ===== Key = 10180 Value = 10180
- Selection List Entry ===== Key = 10183 Value = 10183
- Selection List Entry ===== Key = 10182 Value = 10182
- Selection List Entry ===== Key = 10185 Value = 10185
- Selection List Entry ===== Key = 10184 Value = 10184
- Selection List Entry ===== Key = 10282 Value = 10282
- Selection List Entry ===== Key = 10281 Value = 10281
- Selection List Entry ===== Key = 10419 Value = 10419
- Selection List Entry ===== Key = 10284 Value = 10284
- Selection List Entry ===== Key = 10418 Value = 10418
- Selection List Entry ===== Key = 10283 Value = 10283
- Selection List Entry ===== Key = 10417 Value = 10417
- Selection List Entry ===== Key = 10416 Value = 10416
- Selection List Entry ===== Key = 10415 Value = 10415
- Selection List Entry ===== Key = 10280 Value = 10280
- Selection List Entry ===== Key = 10414 Value = 10414
- Selection List Entry ===== Key = 10289 Value = 10289
- Selection List Entry ===== Key = 10128 Value = 10128
- Selection List Entry ===== Key = 10129 Value = 10129
- Selection List Entry ===== Key = 10285 Value = 10285
- Selection List Entry ===== Key = 10286 Value = 10286
- Selection List Entry ===== Key = 10287 Value = 10287
- Selection List Entry ===== Key = 10288 Value = 10288
- Selection List Entry ===== Key = 10186 Value = 10186
- Selection List Entry ===== Key = 10122 Value = 10122
- Selection List Entry ===== Key = 10187 Value = 10187
- Selection List Entry ===== Key = 10123 Value = 10123
- Selection List Entry ===== Key = 10188 Value = 10188
- Selection List Entry ===== Key = 10120 Value = 10120
- Selection List Entry ===== Key = 10189 Value = 10189
- Selection List Entry ===== Key = 10121 Value = 10121
- Selection List Entry ===== Key = 10126 Value = 10126
- Selection List Entry ===== Key = 10127 Value = 10127
- Selection List Entry ===== Key = 10124 Value = 10124
- Selection List Entry ===== Key = 10125 Value = 10125
- Selection List Entry ===== Key = 10195 Value = 10195
- Selection List Entry ===== Key = 10196 Value = 10196
- Selection List Entry ===== Key = 10399 Value = 10399
- Selection List Entry ===== Key = 10193 Value = 10193
- Selection List Entry ===== Key = 10194 Value = 10194
- Selection List Entry ===== Key = 10397 Value = 10397
- Selection List Entry ===== Key = 10191 Value = 10191
- Selection List Entry ===== Key = 10398 Value = 10398
- Selection List Entry ===== Key = 10192 Value = 10192
- Selection List Entry ===== Key = 10395 Value = 10395
- Selection List Entry ===== Key = 10396 Value = 10396
- Selection List Entry ===== Key = 10190 Value = 10190
- Selection List Entry ===== Key = 10394 Value = 10394
- Selection List Entry ===== Key = 10309 Value = 10309
- Selection List Entry ===== Key = 10393 Value = 10393
- Selection List Entry ===== Key = 10308 Value = 10308
- Selection List Entry ===== Key = 10392 Value = 10392
- Selection List Entry ===== Key = 10391 Value = 10391
- Selection List Entry ===== Key = 10390 Value = 10390
- Selection List Entry ===== Key = 10305 Value = 10305
- Selection List Entry ===== Key = 10304 Value = 10304
- Selection List Entry ===== Key = 10199 Value = 10199
- Selection List Entry ===== Key = 10307 Value = 10307
- Selection List Entry ===== Key = 10198 Value = 10198
- Selection List Entry ===== Key = 10306 Value = 10306
- Selection List Entry ===== Key = 10197 Value = 10197
- Selection List Entry ===== Key = 10301 Value = 10301
- Selection List Entry ===== Key = 10300 Value = 10300
- Selection List Entry ===== Key = 10303 Value = 10303
- Selection List Entry ===== Key = 10302 Value = 10302
- Selection List Entry ===== Key = 10105 Value = 10105
- Selection List Entry ===== Key = 10104 Value = 10104
- Selection List Entry ===== Key = 10103 Value = 10103
- Selection List Entry ===== Key = 10102 Value = 10102
- Selection List Entry ===== Key = 10101 Value = 10101
- Selection List Entry ===== Key = 10100 Value = 10100
- Selection List Entry ===== Key = 10109 Value = 10109
- Selection List Entry ===== Key = 10108 Value = 10108
- Selection List Entry ===== Key = 10107 Value = 10107
- Selection List Entry ===== Key = 10106 Value = 10106
- Data Set Expression = row["ORDERNUMBER"]
- Name = Parameter3
- Is Required = Yes
- Help Text = null
- Parameter Group = Default
- Display Name = null
- Display Format = >
- Data Type = String
- Prompt Text = null
- ======================Parameter =comboparm
- Hidded = No
- Default Value = val2
- Type = List Box
- Conceal Entry = No
- Hidden = No
- Selection List Entry ===== Key = val1 Value = displaytext1
- Selection List Entry ===== Key = val2 Value = displaytestval2
- Data Set Expression = null
- Name = comboparm
- Is Required = No
- Help Text = null
- Parameter Group = Default
- Display Name = null
- Display Format = Unformatted
- Data Type = String
- Prompt Text = null
- ======================Parameter =radioparm
- Hidded = No
- Default Value = radio1
- Type = List Box
- Conceal Entry = No
- Hidden = No
- Selection List Entry ===== Key = radio1 Value = radio1
- Selection List Entry ===== Key = radio2 Value = displaytextcombo2
- Data Set Expression = null
- Name = radioparm
- Is Required = No
- Help Text = radiohelp
- Parameter Group = Default
- Display Name = null
- Display Format = Unformatted
- Data Type = String
- Prompt Text = null
- ======================Parameter =staticlistbox
- Hidded = No
- Default Value = null
- Type = List Box
- Conceal Entry = No
- Hidden = No
- Selection List Entry ===== Key = listboxstatic1 Value = listboxstatic1
- Selection List Entry ===== Key = listboxstatic2 Value = displaytextlistbox2
- Data Set Expression = null
- Name = staticlistbox
- Is Required = No
- Help Text = null
- Parameter Group = Default
- Display Name = null
- Display Format = Unformatted
- Data Type = String
- Prompt Text = null
- Parameter Group: iii
- ======================Parameter =customernumber
- Hidded = No
- Default Value = defaultvalue
- Type = List Box
- Conceal Entry = No
- Hidden = No
- Data Set Expression = row["CUSTOMERNUMBER"]
- Name = customernumber
- Is Required = No
- Help Text = helptext
- Parameter Group = iii
- Display Name = null
- Display Format = Unformatted
- Data Type = String
- Prompt Text = enter customer number
- ======================Parameter =OrderNumber
- Hidded = No
- Default Value = null
- Type = List Box
- Conceal Entry = No
- Hidden = No
- Data Set Expression = row["ORDERNUMBER"]
- Name = OrderNumber
- Is Required = No
- Help Text = null
- Parameter Group = iii
- Display Name = null
- Display Format = Unformatted
- Data Type = String
- Prompt Text = null
- ======================Parameter =parameter6
- Hidded = No
- Default Value = test
- Type = Text Box
- Conceal Entry = No
- Hidden = No
- Data Set Expression = null
- Name = parameter6
- Is Required = No
- Help Text = null
- Parameter Group = Default
- Display Name = null
- Display Format = <
- Data Type = String
- Prompt Text = null
- ======================Parameter =parameter7
- Hidded = No
- Default Value = 2
- Type = List Box
- Conceal Entry = No
- Hidden = No
- Selection List Entry ===== Key = 3 Value = 3
- Selection List Entry ===== Key = 2 Value = 2
- Selection List Entry ===== Key = 1 Value = 1
- Data Set Expression = null
- Name = parameter7
- Is Required = Yes
- Help Text = null
- Parameter Group = Default
- Display Name = null
- Display Format = Unformatted
- Data Type = Decimal
- Prompt Text = null
- ======================Parameter =DateParameter
- Hidded = No
- Default Value = 12/12/2006
- Type = Text Box
- Conceal Entry = No
- Hidden = No
- Data Set Expression = null
- Name = DateParameter
- Is Required = Yes
- Help Text = null
- Parameter Group = Default
- Display Name = null
- Display Format = Unformatted
- Data Type = Date Time
- Prompt Text = null
- Finished
下面的例子在控制台打印了TOC信息:
- package REAPI;
- import java.util.logging.Level;
- import org.eclipse.birt.core.framework.Platform;
- import org.eclipse.birt.report.engine.api.EngineConfig;
- import org.eclipse.birt.report.engine.api.EngineException;
- import org.eclipse.birt.report.engine.api.IReportDocument;
- import org.eclipse.birt.report.engine.api.IReportEngine;
- import org.eclipse.birt.report.engine.api.IReportEngineFactory;
- import org.eclipse.birt.report.engine.api.TOCNode;
- public class Toc {
- public void runReport() throws EngineException
- {
- IReportEngine engine=null;
- EngineConfig config = null;
- try{
- config = new EngineConfig( );
- config.setBIRTHome("E:\\birt汉化包\\birt-runtime-3_7_2\\ReportEngine");
- config.setLogConfig(null, Level.FINE);
- Platform.startup( config );
- IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
- engine = factory.createReportEngine( config );
- }catch( Exception ex){
- ex.printStackTrace();
- }
- IReportDocument document = null;
- //Open the report design
- document = engine.openReportDocument("d:\\customers.rptdocument");
- // Get the root of the table of contents.
- TOCNode td = document.findTOC( null );
- java.util.List children = td.getChildren( );
- long pNumber;
- // Loop through the top level table of contents entries.
- if ( children != null && children.size( ) > 0 ) {
- for ( int i = 0; i < children.size( ); i++ ) {
- // Find the required table of contents entry.
- TOCNode child = ( TOCNode ) children.get( i );
- //if ( child.getDisplayString( ).equals( "103" ) ) {
- // Get the number of the page that contains the data.
- pNumber = document.getPageNumber( child.getBookmark( ) );
- System.out.println( child.getDisplayString( ) + " Page to print is " + pNumber );
- //}
- }
- }
- document.close();
- engine.destroy();
- Platform.shutdown();
- System.out.println("Finished");
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- try
- {
- Toc ex = new Toc( );
- ex.runReport();
- }
- catch ( Exception e )
- {
- e.printStackTrace();
- }
- }
- }
报表的预览如下:
运行结果如下:
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的结构和使用方法,以及如何在报表引擎中调用报表设计引擎来修改报表,将在下一节详细讲解。