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 applicationIPlatformContext 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。
在Web应用程序上创建一个报表引擎,如下:
// Example class to create the report enginepublic 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 enginereportEngine = BirtEngine.getBirtEngine( request.getSession( ).getServletContext( ));// Set up the engineEngineConfig 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 taskHashMap 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, parameterValuestask.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 tasktask.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 ====> MyCustomerTableResultSetName ====> innertableResultSetName ====> innertable_1ResultSetName ====> innertable_2ResultSetName ====> innertable_3ResultSetName ====> innertable_4ResultSetName ====> innertable_5ResultSetName ====> innertable_6ResultSetName ====> innertable_7ResultSetName ====> innertable_8ResultSetName ====> innertable_9ResultSetName ====> innertable_10ResultSetName ====> innertable_11ResultSetName ====> innertable_12ResultSetName ====> innertable_13ResultSetName ====> innertable_14ResultSetName ====> innertable_15ResultSetName ====> innertable_16ResultSetName ====> innertable_17ResultSetName ====> innertable_18ResultSetName ====> innertable_19ResultSetName ====> innertable_20ResultSetName ====> innertable_21ResultSetName ====> innertable_22ResultSetName ====> innertable_23ResultSetName ====> innertable_24ResultSetName ====> innertable_25ResultSetName ====> innertable_26ResultSetName ====> innertable_27ResultSetName ====> innertable_28ResultSetName ====> innertable_29ResultSetName ====> innertable_30ResultSetName ====> innertable_31ResultSetName ====> innertable_32ResultSetName ====> innertable_33ResultSetName ====> innertable_34ResultSetName ====> innertable_35ResultSetName ====> innertable_36ResultSetName ====> innertable_37ResultSetName ====> innertable_38ResultSetName ====> innertable_39ResultSetName ====> innertable_40ResultSetName ====> innertable_41ResultSetName ====> innertable_42ResultSetName ====> innertable_43ResultSetName ====> innertable_44ResultSetName ====> innertable_45ResultSetName ====> innertable_46ResultSetName ====> innertable_47ResultSetName ====> innertable_48ResultSetName ====> innertable_49ResultSetName ====> innertable_50ResultSetName ====> innertable_51ResultSetName ====> innertable_52ResultSetName ====> innertable_53ResultSetName ====> innertable_54ResultSetName ====> innertable_55ResultSetName ====> innertable_56ResultSetName ====> innertable_57ResultSetName ====> innertable_58ResultSetName ====> innertable_59ResultSetName ====> innertable_60ResultSetName ====> innertable_61ResultSetName ====> innertable_62ResultSetName ====> innertable_63ResultSetName ====> innertable_64ResultSetName ====> innertable_65ResultSetName ====> innertable_66ResultSetName ====> innertable_67ResultSetName ====> innertable_68ResultSetName ====> innertable_69ResultSetName ====> innertable_70ResultSetName ====> innertable_71ResultSetName ====> innertable_72ResultSetName ====> innertable_73ResultSetName ====> innertable_74ResultSetName ====> innertable_75ResultSetName ====> innertable_76ResultSetName ====> innertable_77ResultSetName ====> innertable_78ResultSetName ====> innertable_79ResultSetName ====> innertable_80ResultSetName ====> innertable_81ResultSetName ====> innertable_82ResultSetName ====> innertable_83ResultSetName ====> innertable_84ResultSetName ====> innertable_85ResultSetName ====> innertable_86ResultSetName ====> innertable_87ResultSetName ====> innertable_88ResultSetName ====> innertable_89ResultSetName ====> innertable_90ResultSetName ====> innertable_91ResultSetName ====> innertable_92ResultSetName ====> innertable_93ResultSetName ====> innertable_94ResultSetName ====> innertable_95ResultSetName ====> innertable_96ResultSetName ====> innertable_97ResultSetName ====> innertable_98ResultSetName ====> innertable_99ResultSetName ====> innertable_100ResultSetName ====> innertable_101ResultSetName ====> innertable_102ResultSetName ====> innertable_103ResultSetName ====> innertable_104ResultSetName ====> innertable_105ResultSetName ====> innertable_106ResultSetName ====> innertable_107ResultSetName ====> innertable_108ResultSetName ====> innertable_109ResultSetName ====> innertable_110ResultSetName ====> innertable_111ResultSetName ====> innertable_112ResultSetName ====> innertable_113ResultSetName ====> innertable_114ResultSetName ====> innertable_115ResultSetName ====> innertable_116ResultSetName ====> innertable_117ResultSetName ====> innertable_118ResultSetName ====> innertable_119ResultSetName ====> innertable_120ResultSetName ====> innertable_121Cloumn Count =13Cloumn Name =CUSTOMERNUMBERCloumn Type =IntegerCloumn Name =CUSTOMERNAMECloumn Type =StringCloumn Name =CONTACTLASTNAMECloumn Type =StringCloumn Name =CONTACTFIRSTNAMECloumn Type =StringCloumn Name =PHONECloumn Type =StringCloumn Name =ADDRESSLINE1Cloumn Type =StringCloumn Name =ADDRESSLINE2Cloumn Type =StringCloumn Name =CITYCloumn Type =StringCloumn Name =STATECloumn Type =StringCloumn Name =POSTALCODECloumn Type =StringCloumn Name =COUNTRYCloumn Type =StringCloumn Name =SALESREPEMPLOYEENUMBERCloumn Type =IntegerCloumn Name =CREDITLIMITCloumn Type =Float02 9936 8555 , 107800.0+61 2 9495 8555 , 93300.0(604) 555-3392 , 90300.0(514) 555-8054 , 48700.0(604) 555-4555 , 89600.086 21 3555 , 120800.088.60.1555 , 53800.061.77.6555 , 61100.0+33 1 46 62 7555 , 68100.030.59.8555 , 77900.00372-555188 , 0.0069-0555984 , 0.00221-5554327 , 120400.0089-0877555 , 0.0+852 2251 1555 , 58600.0011-4988555 , 113000.0035-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.00897-034555 , 141300.0(171) 555-2282 , 92700.0(198) 555-8888 , 93900.06175557555 , 68700.06265557265 , 90700.03105552373 , 11000.07605558146 , 105000.06175558555 , 123700.0Finished
下面这个案例运行报表,并导出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 =parameter1Hidded = NoDefault Value = 1Type = Text BoxConceal Entry = NoHidden = NoData Set Expression = nullName = parameter1Is Required = YesHelp Text = nullParameter Group = DefaultDisplay Name = nullDisplay Format = #,##0.00%Data Type = DecimalPrompt Text = "This i prompt text"Parameter Group: ParmGroup1======================Parameter =Parameter2Hidded = NoDefault Value = parm2Type = Text BoxConceal Entry = NoHidden = NoData Set Expression = nullName = Parameter2Is Required = NoHelp Text = nullParameter Group = ParmGroup1Display Name = nullDisplay Format = UnformattedData Type = StringPrompt Text = null======================Parameter =Parameter3Hidded = NoDefault Value = 10101Type = List BoxConceal Entry = NoHidden = NoSelection List Entry ===== Key = 10346 Value = 10346Selection List Entry ===== Key = 10207 Value = 10207Selection List Entry ===== Key = 10347 Value = 10347Selection List Entry ===== Key = 10208 Value = 10208Selection List Entry ===== Key = 10344 Value = 10344Selection List Entry ===== Key = 10205 Value = 10205Selection List Entry ===== Key = 10345 Value = 10345Selection List Entry ===== Key = 10206 Value = 10206Selection List Entry ===== Key = 10342 Value = 10342Selection List Entry ===== Key = 10343 Value = 10343Selection List Entry ===== Key = 10340 Value = 10340Selection List Entry ===== Key = 10209 Value = 10209Selection List Entry ===== Key = 10341 Value = 10341Selection List Entry ===== Key = 10200 Value = 10200Selection List Entry ===== Key = 10203 Value = 10203Selection List Entry ===== Key = 10204 Value = 10204Selection List Entry ===== Key = 10348 Value = 10348Selection List Entry ===== Key = 10201 Value = 10201Selection List Entry ===== Key = 10349 Value = 10349Selection List Entry ===== Key = 10202 Value = 10202Selection List Entry ===== Key = 10333 Value = 10333Selection List Entry ===== Key = 10334 Value = 10334Selection List Entry ===== Key = 10335 Value = 10335Selection List Entry ===== Key = 10336 Value = 10336Selection List Entry ===== Key = 10330 Value = 10330Selection List Entry ===== Key = 10331 Value = 10331Selection List Entry ===== Key = 10332 Value = 10332Selection List Entry ===== Key = 10337 Value = 10337Selection List Entry ===== Key = 10338 Value = 10338Selection List Entry ===== Key = 10339 Value = 10339Selection List Entry ===== Key = 10320 Value = 10320Selection List Entry ===== Key = 10321 Value = 10321Selection List Entry ===== Key = 10324 Value = 10324Selection List Entry ===== Key = 10229 Value = 10229Selection List Entry ===== Key = 10325 Value = 10325Selection List Entry ===== Key = 10322 Value = 10322Selection List Entry ===== Key = 10227 Value = 10227Selection List Entry ===== Key = 10323 Value = 10323Selection List Entry ===== Key = 10228 Value = 10228Selection List Entry ===== Key = 10328 Value = 10328Selection List Entry ===== Key = 10225 Value = 10225Selection List Entry ===== Key = 10329 Value = 10329Selection List Entry ===== Key = 10226 Value = 10226Selection List Entry ===== Key = 10326 Value = 10326Selection List Entry ===== Key = 10223 Value = 10223Selection List Entry ===== Key = 10327 Value = 10327Selection List Entry ===== Key = 10224 Value = 10224Selection List Entry ===== Key = 10221 Value = 10221Selection List Entry ===== Key = 10222 Value = 10222Selection List Entry ===== Key = 10220 Value = 10220Selection List Entry ===== Key = 10310 Value = 10310Selection List Entry ===== Key = 10311 Value = 10311Selection List Entry ===== Key = 10216 Value = 10216Selection List Entry ===== Key = 10312 Value = 10312Selection List Entry ===== Key = 10217 Value = 10217Selection List Entry ===== Key = 10313 Value = 10313Selection List Entry ===== Key = 10218 Value = 10218Selection List Entry ===== Key = 10314 Value = 10314Selection List Entry ===== Key = 10219 Value = 10219Selection List Entry ===== Key = 10315 Value = 10315Selection List Entry ===== Key = 10212 Value = 10212Selection List Entry ===== Key = 10316 Value = 10316Selection List Entry ===== Key = 10213 Value = 10213Selection List Entry ===== Key = 10317 Value = 10317Selection List Entry ===== Key = 10214 Value = 10214Selection List Entry ===== Key = 10318 Value = 10318Selection List Entry ===== Key = 10215 Value = 10215Selection List Entry ===== Key = 10319 Value = 10319Selection List Entry ===== Key = 10210 Value = 10210Selection List Entry ===== Key = 10211 Value = 10211Selection List Entry ===== Key = 10244 Value = 10244Selection List Entry ===== Key = 10243 Value = 10243Selection List Entry ===== Key = 10242 Value = 10242Selection List Entry ===== Key = 10241 Value = 10241Selection List Entry ===== Key = 10248 Value = 10248Selection List Entry ===== Key = 10247 Value = 10247Selection List Entry ===== Key = 10246 Value = 10246Selection List Entry ===== Key = 10245 Value = 10245Selection List Entry ===== Key = 10389 Value = 10389Selection List Entry ===== Key = 10388 Value = 10388Selection List Entry ===== Key = 10249 Value = 10249Selection List Entry ===== Key = 10387 Value = 10387Selection List Entry ===== Key = 10386 Value = 10386Selection List Entry ===== Key = 10385 Value = 10385Selection List Entry ===== Key = 10384 Value = 10384Selection List Entry ===== Key = 10382 Value = 10382Selection List Entry ===== Key = 10383 Value = 10383Selection List Entry ===== Key = 10380 Value = 10380Selection List Entry ===== Key = 10381 Value = 10381Selection List Entry ===== Key = 10240 Value = 10240Selection List Entry ===== Key = 10231 Value = 10231Selection List Entry ===== Key = 10230 Value = 10230Selection List Entry ===== Key = 10233 Value = 10233Selection List Entry ===== Key = 10232 Value = 10232Selection List Entry ===== Key = 10235 Value = 10235Selection List Entry ===== Key = 10234 Value = 10234Selection List Entry ===== Key = 10237 Value = 10237Selection List Entry ===== Key = 10236 Value = 10236Selection List Entry ===== Key = 10378 Value = 10378Selection List Entry ===== Key = 10239 Value = 10239Selection List Entry ===== Key = 10377 Value = 10377Selection List Entry ===== Key = 10238 Value = 10238Selection List Entry ===== Key = 10379 Value = 10379Selection List Entry ===== Key = 10374 Value = 10374Selection List Entry ===== Key = 10373 Value = 10373Selection List Entry ===== Key = 10376 Value = 10376Selection List Entry ===== Key = 10375 Value = 10375Selection List Entry ===== Key = 10370 Value = 10370Selection List Entry ===== Key = 10371 Value = 10371Selection List Entry ===== Key = 10372 Value = 10372Selection List Entry ===== Key = 10269 Value = 10269Selection List Entry ===== Key = 10268 Value = 10268Selection List Entry ===== Key = 10267 Value = 10267Selection List Entry ===== Key = 10266 Value = 10266Selection List Entry ===== Key = 10265 Value = 10265Selection List Entry ===== Key = 10264 Value = 10264Selection List Entry ===== Key = 10263 Value = 10263Selection List Entry ===== Key = 10365 Value = 10365Selection List Entry ===== Key = 10364 Value = 10364Selection List Entry ===== Key = 10363 Value = 10363Selection List Entry ===== Key = 10362 Value = 10362Selection List Entry ===== Key = 10369 Value = 10369Selection List Entry ===== Key = 10368 Value = 10368Selection List Entry ===== Key = 10367 Value = 10367Selection List Entry ===== Key = 10366 Value = 10366Selection List Entry ===== Key = 10360 Value = 10360Selection List Entry ===== Key = 10361 Value = 10361Selection List Entry ===== Key = 10261 Value = 10261Selection List Entry ===== Key = 10262 Value = 10262Selection List Entry ===== Key = 10260 Value = 10260Selection List Entry ===== Key = 10257 Value = 10257Selection List Entry ===== Key = 10359 Value = 10359Selection List Entry ===== Key = 10256 Value = 10256Selection List Entry ===== Key = 10259 Value = 10259Selection List Entry ===== Key = 10258 Value = 10258Selection List Entry ===== Key = 10253 Value = 10253Selection List Entry ===== Key = 10252 Value = 10252Selection List Entry ===== Key = 10255 Value = 10255Selection List Entry ===== Key = 10254 Value = 10254Selection List Entry ===== Key = 10352 Value = 10352Selection List Entry ===== Key = 10351 Value = 10351Selection List Entry ===== Key = 10354 Value = 10354Selection List Entry ===== Key = 10353 Value = 10353Selection List Entry ===== Key = 10356 Value = 10356Selection List Entry ===== Key = 10355 Value = 10355Selection List Entry ===== Key = 10358 Value = 10358Selection List Entry ===== Key = 10357 Value = 10357Selection List Entry ===== Key = 10350 Value = 10350Selection List Entry ===== Key = 10250 Value = 10250Selection List Entry ===== Key = 10251 Value = 10251Selection List Entry ===== Key = 10152 Value = 10152Selection List Entry ===== Key = 10151 Value = 10151Selection List Entry ===== Key = 10150 Value = 10150Selection List Entry ===== Key = 10130 Value = 10130Selection List Entry ===== Key = 10400 Value = 10400Selection List Entry ===== Key = 10402 Value = 10402Selection List Entry ===== Key = 10401 Value = 10401Selection List Entry ===== Key = 10404 Value = 10404Selection List Entry ===== Key = 10291 Value = 10291Selection List Entry ===== Key = 10403 Value = 10403Selection List Entry ===== Key = 10290 Value = 10290Selection List Entry ===== Key = 10406 Value = 10406Selection List Entry ===== Key = 10405 Value = 10405Selection List Entry ===== Key = 10408 Value = 10408Selection List Entry ===== Key = 10295 Value = 10295Selection List Entry ===== Key = 10407 Value = 10407Selection List Entry ===== Key = 10294 Value = 10294Selection List Entry ===== Key = 10293 Value = 10293Selection List Entry ===== Key = 10409 Value = 10409Selection List Entry ===== Key = 10292 Value = 10292Selection List Entry ===== Key = 10298 Value = 10298Selection List Entry ===== Key = 10299 Value = 10299Selection List Entry ===== Key = 10296 Value = 10296Selection List Entry ===== Key = 10297 Value = 10297Selection List Entry ===== Key = 10139 Value = 10139Selection List Entry ===== Key = 10159 Value = 10159Selection List Entry ===== Key = 10135 Value = 10135Selection List Entry ===== Key = 10136 Value = 10136Selection List Entry ===== Key = 10157 Value = 10157Selection List Entry ===== Key = 10137 Value = 10137Selection List Entry ===== Key = 10158 Value = 10158Selection List Entry ===== Key = 10138 Value = 10138Selection List Entry ===== Key = 10155 Value = 10155Selection List Entry ===== Key = 10131 Value = 10131Selection List Entry ===== Key = 10156 Value = 10156Selection List Entry ===== Key = 10132 Value = 10132Selection List Entry ===== Key = 10153 Value = 10153Selection List Entry ===== Key = 10133 Value = 10133Selection List Entry ===== Key = 10154 Value = 10154Selection List Entry ===== Key = 10134 Value = 10134Selection List Entry ===== Key = 10161 Value = 10161Selection List Entry ===== Key = 10141 Value = 10141Selection List Entry ===== Key = 10160 Value = 10160Selection List Entry ===== Key = 10140 Value = 10140Selection List Entry ===== Key = 10163 Value = 10163Selection List Entry ===== Key = 10162 Value = 10162Selection List Entry ===== Key = 10168 Value = 10168Selection List Entry ===== Key = 10148 Value = 10148Selection List Entry ===== Key = 10169 Value = 10169Selection List Entry ===== Key = 10149 Value = 10149Selection List Entry ===== Key = 10146 Value = 10146Selection List Entry ===== Key = 10147 Value = 10147Selection List Entry ===== Key = 10164 Value = 10164Selection List Entry ===== Key = 10144 Value = 10144Selection List Entry ===== Key = 10165 Value = 10165Selection List Entry ===== Key = 10145 Value = 10145Selection List Entry ===== Key = 10166 Value = 10166Selection List Entry ===== Key = 10142 Value = 10142Selection List Entry ===== Key = 10167 Value = 10167Selection List Entry ===== Key = 10143 Value = 10143Selection List Entry ===== Key = 10422 Value = 10422Selection List Entry ===== Key = 10170 Value = 10170Selection List Entry ===== Key = 10421 Value = 10421Selection List Entry ===== Key = 10424 Value = 10424Selection List Entry ===== Key = 10423 Value = 10423Selection List Entry ===== Key = 10174 Value = 10174Selection List Entry ===== Key = 10173 Value = 10173Selection List Entry ===== Key = 10420 Value = 10420Selection List Entry ===== Key = 10172 Value = 10172Selection List Entry ===== Key = 10171 Value = 10171Selection List Entry ===== Key = 10273 Value = 10273Selection List Entry ===== Key = 10272 Value = 10272Selection List Entry ===== Key = 10271 Value = 10271Selection List Entry ===== Key = 10270 Value = 10270Selection List Entry ===== Key = 10425 Value = 10425Selection List Entry ===== Key = 10117 Value = 10117Selection List Entry ===== Key = 10118 Value = 10118Selection List Entry ===== Key = 10119 Value = 10119Selection List Entry ===== Key = 10278 Value = 10278Selection List Entry ===== Key = 10279 Value = 10279Selection List Entry ===== Key = 10276 Value = 10276Selection List Entry ===== Key = 10277 Value = 10277Selection List Entry ===== Key = 10274 Value = 10274Selection List Entry ===== Key = 10275 Value = 10275Selection List Entry ===== Key = 10177 Value = 10177Selection List Entry ===== Key = 10178 Value = 10178Selection List Entry ===== Key = 10110 Value = 10110Selection List Entry ===== Key = 10175 Value = 10175Selection List Entry ===== Key = 10111 Value = 10111Selection List Entry ===== Key = 10176 Value = 10176Selection List Entry ===== Key = 10112 Value = 10112Selection List Entry ===== Key = 10113 Value = 10113Selection List Entry ===== Key = 10114 Value = 10114Selection List Entry ===== Key = 10179 Value = 10179Selection List Entry ===== Key = 10115 Value = 10115Selection List Entry ===== Key = 10116 Value = 10116Selection List Entry ===== Key = 10413 Value = 10413Selection List Entry ===== Key = 10412 Value = 10412Selection List Entry ===== Key = 10411 Value = 10411Selection List Entry ===== Key = 10181 Value = 10181Selection List Entry ===== Key = 10410 Value = 10410Selection List Entry ===== Key = 10180 Value = 10180Selection List Entry ===== Key = 10183 Value = 10183Selection List Entry ===== Key = 10182 Value = 10182Selection List Entry ===== Key = 10185 Value = 10185Selection List Entry ===== Key = 10184 Value = 10184Selection List Entry ===== Key = 10282 Value = 10282Selection List Entry ===== Key = 10281 Value = 10281Selection List Entry ===== Key = 10419 Value = 10419Selection List Entry ===== Key = 10284 Value = 10284Selection List Entry ===== Key = 10418 Value = 10418Selection List Entry ===== Key = 10283 Value = 10283Selection List Entry ===== Key = 10417 Value = 10417Selection List Entry ===== Key = 10416 Value = 10416Selection List Entry ===== Key = 10415 Value = 10415Selection List Entry ===== Key = 10280 Value = 10280Selection List Entry ===== Key = 10414 Value = 10414Selection List Entry ===== Key = 10289 Value = 10289Selection List Entry ===== Key = 10128 Value = 10128Selection List Entry ===== Key = 10129 Value = 10129Selection List Entry ===== Key = 10285 Value = 10285Selection List Entry ===== Key = 10286 Value = 10286Selection List Entry ===== Key = 10287 Value = 10287Selection List Entry ===== Key = 10288 Value = 10288Selection List Entry ===== Key = 10186 Value = 10186Selection List Entry ===== Key = 10122 Value = 10122Selection List Entry ===== Key = 10187 Value = 10187Selection List Entry ===== Key = 10123 Value = 10123Selection List Entry ===== Key = 10188 Value = 10188Selection List Entry ===== Key = 10120 Value = 10120Selection List Entry ===== Key = 10189 Value = 10189Selection List Entry ===== Key = 10121 Value = 10121Selection List Entry ===== Key = 10126 Value = 10126Selection List Entry ===== Key = 10127 Value = 10127Selection List Entry ===== Key = 10124 Value = 10124Selection List Entry ===== Key = 10125 Value = 10125Selection List Entry ===== Key = 10195 Value = 10195Selection List Entry ===== Key = 10196 Value = 10196Selection List Entry ===== Key = 10399 Value = 10399Selection List Entry ===== Key = 10193 Value = 10193Selection List Entry ===== Key = 10194 Value = 10194Selection List Entry ===== Key = 10397 Value = 10397Selection List Entry ===== Key = 10191 Value = 10191Selection List Entry ===== Key = 10398 Value = 10398Selection List Entry ===== Key = 10192 Value = 10192Selection List Entry ===== Key = 10395 Value = 10395Selection List Entry ===== Key = 10396 Value = 10396Selection List Entry ===== Key = 10190 Value = 10190Selection List Entry ===== Key = 10394 Value = 10394Selection List Entry ===== Key = 10309 Value = 10309Selection List Entry ===== Key = 10393 Value = 10393Selection List Entry ===== Key = 10308 Value = 10308Selection List Entry ===== Key = 10392 Value = 10392Selection List Entry ===== Key = 10391 Value = 10391Selection List Entry ===== Key = 10390 Value = 10390Selection List Entry ===== Key = 10305 Value = 10305Selection List Entry ===== Key = 10304 Value = 10304Selection List Entry ===== Key = 10199 Value = 10199Selection List Entry ===== Key = 10307 Value = 10307Selection List Entry ===== Key = 10198 Value = 10198Selection List Entry ===== Key = 10306 Value = 10306Selection List Entry ===== Key = 10197 Value = 10197Selection List Entry ===== Key = 10301 Value = 10301Selection List Entry ===== Key = 10300 Value = 10300Selection List Entry ===== Key = 10303 Value = 10303Selection List Entry ===== Key = 10302 Value = 10302Selection List Entry ===== Key = 10105 Value = 10105Selection List Entry ===== Key = 10104 Value = 10104Selection List Entry ===== Key = 10103 Value = 10103Selection List Entry ===== Key = 10102 Value = 10102Selection List Entry ===== Key = 10101 Value = 10101Selection List Entry ===== Key = 10100 Value = 10100Selection List Entry ===== Key = 10109 Value = 10109Selection List Entry ===== Key = 10108 Value = 10108Selection List Entry ===== Key = 10107 Value = 10107Selection List Entry ===== Key = 10106 Value = 10106Data Set Expression = row["ORDERNUMBER"]Name = Parameter3Is Required = YesHelp Text = nullParameter Group = DefaultDisplay Name = nullDisplay Format = >Data Type = StringPrompt Text = null======================Parameter =comboparmHidded = NoDefault Value = val2Type = List BoxConceal Entry = NoHidden = NoSelection List Entry ===== Key = val1 Value = displaytext1Selection List Entry ===== Key = val2 Value = displaytestval2Data Set Expression = nullName = comboparmIs Required = NoHelp Text = nullParameter Group = DefaultDisplay Name = nullDisplay Format = UnformattedData Type = StringPrompt Text = null======================Parameter =radioparmHidded = NoDefault Value = radio1Type = List BoxConceal Entry = NoHidden = NoSelection List Entry ===== Key = radio1 Value = radio1Selection List Entry ===== Key = radio2 Value = displaytextcombo2Data Set Expression = nullName = radioparmIs Required = NoHelp Text = radiohelpParameter Group = DefaultDisplay Name = nullDisplay Format = UnformattedData Type = StringPrompt Text = null======================Parameter =staticlistboxHidded = NoDefault Value = nullType = List BoxConceal Entry = NoHidden = NoSelection List Entry ===== Key = listboxstatic1 Value = listboxstatic1Selection List Entry ===== Key = listboxstatic2 Value = displaytextlistbox2Data Set Expression = nullName = staticlistboxIs Required = NoHelp Text = nullParameter Group = DefaultDisplay Name = nullDisplay Format = UnformattedData Type = StringPrompt Text = nullParameter Group: iii======================Parameter =customernumberHidded = NoDefault Value = defaultvalueType = List BoxConceal Entry = NoHidden = NoData Set Expression = row["CUSTOMERNUMBER"]Name = customernumberIs Required = NoHelp Text = helptextParameter Group = iiiDisplay Name = nullDisplay Format = UnformattedData Type = StringPrompt Text = enter customer number======================Parameter =OrderNumberHidded = NoDefault Value = nullType = List BoxConceal Entry = NoHidden = NoData Set Expression = row["ORDERNUMBER"]Name = OrderNumberIs Required = NoHelp Text = nullParameter Group = iiiDisplay Name = nullDisplay Format = UnformattedData Type = StringPrompt Text = null======================Parameter =parameter6Hidded = NoDefault Value = testType = Text BoxConceal Entry = NoHidden = NoData Set Expression = nullName = parameter6Is Required = NoHelp Text = nullParameter Group = DefaultDisplay Name = nullDisplay Format = <Data Type = StringPrompt Text = null======================Parameter =parameter7Hidded = NoDefault Value = 2Type = List BoxConceal Entry = NoHidden = NoSelection List Entry ===== Key = 3 Value = 3Selection List Entry ===== Key = 2 Value = 2Selection List Entry ===== Key = 1 Value = 1Data Set Expression = nullName = parameter7Is Required = YesHelp Text = nullParameter Group = DefaultDisplay Name = nullDisplay Format = UnformattedData Type = DecimalPrompt Text = null======================Parameter =DateParameterHidded = NoDefault Value = 12/12/2006Type = Text BoxConceal Entry = NoHidden = NoData Set Expression = nullName = DateParameterIs Required = YesHelp Text = nullParameter Group = DefaultDisplay Name = nullDisplay Format = UnformattedData Type = Date TimePrompt Text = nullFinished
下面的例子在控制台打印了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的结构和使用方法,以及如何在报表引擎中调用报表设计引擎来修改报表,将在下一节详细讲解。