Birt报表设计、部署步骤及程序实现

Birt报表设计、部署步骤及程序实现[@more@] Birt报表设计步骤:

1、下载birt all in one 2.2.1.1,包含eclipse,解开下载文件
启动eclipse,必须使用JDK1.5启动eclipse,否则新建工程对话框中没有birt的相关选项


2、新建report project和report
打开data explorer视图,新建一个data sources,定义数据库连接driver/url/用户名/密码,在"Manage Drivers..."中把jdbc的jar加进来


3、新建Report Parameter,设置参数名称、类型,参数值将来通过url传递或使用程序方式赋值


4、新建data sets,设置Query,即preparedStatement语句,设置data set的Pameters,即reparedStatement中的“?”,使之和report parameter关联; 如果data set的Parameter的值不是来自于Report Parameter,例如来自另外一个data set, 可不设置关联,而在报表内容中设置和其它data set字段的关联(见步骤8)

5、如果data set对应的sql是动态生成的,可以设置“property Binding”,在Query Text中输入sql和表达式,其中可以使用Report Parameter和javascript语句,运行时将执行Property Binding中的Query Text,而不执行Query中的preparedStatement,但是Query中的preparedStatement在设计报表时还是有用的,因为可以看到运行结果的字段列表,只要preparedStatement和Query Text返回相同的字段即可

6、将定义好的data set拖动到设计界面上,会自动在报表放置一个Table

7、如要对table的某列合计,从Palette视图中拖一个“Aggregation”到设计界面

8、如要Master-Sleve报表,则在Table中再放置一个Table。例如一个data set是客户基本信息,每个客户一条记录,另一个data set是客户交易明细,每个客户可能有多条记录,则先将客户基本信息的data set拖置设计界面,会自动放置一个Table,然后将客户交易明细的data set拖置前一个Table中适当位置,即生成一个Table中的Table。选中交易明细的Table,打开Property Editor视图,在binding栏中,选择交易明细的data set,按“Dataset Parameter Binding...”,选择“CUST_ID"字段,将其和客户基本信息data set的CUST_ID关联起来(row["CUST_ID"])

9、按需要调整报表的样式,从eclipse工具栏中选择“view report in web viewer”,如果设置了Report Parameter,会出现一个对话框,要求输入Report Parameter的值,按确定后生成报表内容

10、在web viewer中,鼠标右建点按生成的报表,选“属性”,获取url,开一个空白IE,复制url到地址栏,在url最后用“&参数名=参数值”方式,把Report Parameter加进url,按回车,直接出现报表结果,不再出现报表参数输入对话框。还可以pdf/doc/html等格式预览生成的报表

Birt报表部署步骤:

部署设计好的报表很简单,只需要下载Birt runtime,将Birt runtime部署为一个web应用,报表设计文件复制到该应用的根目录即可。为方便起见,以下将Birt runtime部属为一个独立的应用,当然也可以将Birt runtime整合到你的web应用中

1、下载birt runtime 2.2.1.1,解开下载文件
2、将birt rumtime解开后的目录下的部署为一个web应用,使用JDK1.4.2或以上版本
3、将报表设计步骤中生成的设计文件(*.retdesing文件)复制到web应用所在目录
4、确保以下文件在WEB-INF/lib目录下存在,如果没有,从all in one中搜索复制过来
com.ibm.icu_3.6.1.v20070906.jar
coreapi.jar
engineapi.jar
scriptapi.jar
5、将jdbc的jar或zip文件复制到WEB-INFplatformpluginsorg.eclipse.birt.report.data.oda.jdbc_2.2.1.r22x_v20070919drivers目录下
6、删除WEB-INFplatformorg.eclipse.birt.report.data.oda.sampledb_2.2.1.r22x_v20070919目录(示例数据库,没什么用处),否则生成报表时有不必要的警告信息出现
7、启动web server,修改设计步骤第10步获得的url,使IP/端口/web应用名称符合你的Birt runtime部署,在IE中可以查看报表内容

使用Birt提供API以程序方式生成报表结果,例如生成PDF格式的报表结果,代码片段如下:

None.gif import java.util.HashMap;
None.gif import java.util.logging.Level;
None.gif
None.gif import java.io.OutputStream;
None.gif import java.io.FileOutputStream;
None.gif import java.io.ByteArrayOutputStream;
None.gif
None.gif import org.eclipse.birt.core.framework.Platform;
None.gif import org.eclipse.birt.report.engine.api.EngineConfig;
None.gif import org.eclipse.birt.report.engine.api.EngineConstants;
None.gif import org.eclipse.birt.report.engine.api.EngineException;
None.gif import org.eclipse.birt.report.engine.api.IReportEngine;
None.gif import org.eclipse.birt.report.engine.api.IReportEngineFactory;
None.gif import org.eclipse.birt.report.engine.api.IReportRunnable;
None.gif import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
None.gif import org.eclipse.birt.report.engine.api.PDFRenderOption;
None.gif
None.gif public class PDFReportServiceAccess
ExpandedBlockStart.gifContractedBlock.gif ... {
ExpandedSubBlockStart.gifContractedSubBlock.gif/** *//** 初始化的状态 */
InBlock.gifprotected static boolean initStatus = false;
InBlock.gif
InBlock.gifprivate static IReportEngine engine = null;
InBlock.gif
InBlock.gifprivate static EngineConfig config = null;
InBlock.gif
InBlock.gifprivate static IReportRunnable design = null;
InBlock.gif
InBlock.gifprivate static PDFRenderOption ro = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/** *//** 初始化资源 */
InBlock.gifpublic void initilize()
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gifif ( initStatus == true )
InBlock.gifreturn;
InBlock.gif
InBlock.giftry
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif config = new EngineConfig();
InBlock.gif config.setEngineHome( "C:/projects/birt/WEB-INF/platform" ); //birt runtime web应用中的报表引擎目录
InBlock.gif config.setLogConfig( "C:/projects/birt/logs", Level.FINE );
InBlock.gif
InBlock.gif Platform.startup( config );
InBlock.gif IReportEngineFactory factory = ( IReportEngineFactory ) Platform
InBlock.gif .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
InBlock.gif engine = factory.createReportEngine( config );
InBlock.gif engine.changeLogLevel( Level.WARNING );
InBlock.gif
InBlock.gif ro = new PDFRenderOption();
InBlock.gif ro.setBaseURL( "http://localhost:8080/birt" ); //birt runtime web应用
InBlock.gif config.getEmitterConfigs().put( "pdf", ro ); //生成pdf格式
InBlock.gif
InBlock.gif initStatus = true;
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gifcatch ( Exception ex )
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif ex.printStackTrace();
InBlock.gif initStatus = false;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/** *//** 释放资源 */
InBlock.gifpublic void release()
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif engine.shutdown();
InBlock.gif Platform.shutdown();
InBlock.gif
InBlock.gif initStatus = false;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifprotected OutputStream run( String filename, HashMap parameters ) throws EngineException
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif design = engine.openReportDesign( filename );
InBlock.gif
InBlock.gif// Create task to run and render the report,
InBlock.gif IRunAndRenderTask task = engine.createRunAndRenderTask( design );
InBlock.gif HashMap contextMap = new HashMap();
InBlock.gif contextMap.put( EngineConstants.APPCONTEXT_PDF_RENDER_CONTEXT, ro );
InBlock.gif task.setAppContext( contextMap );
InBlock.gif task.setParameterValues( parameters );
InBlock.gif task.validateParameters();
InBlock.gif
InBlock.gif OutputStream os = new ByteArrayOutputStream();
InBlock.gif ro.setOutputStream( os );
InBlock.gif ro.setOutputFormat( "pdf" );
InBlock.gif task.setRenderOption( ro );
InBlock.gif
InBlock.gif task.run();
InBlock.gif task.close();
InBlock.gif
InBlock.gifreturn os;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/** *//**
InBlock.gif * 生成PDF格式报表,以OutputStream格式返回
InBlock.gif *
InBlock.gif * @param filename 报表设计文件名全路径
InBlock.gif * @param parameters
InBlock.gif * 报表参数
InBlock.gif * @return ByteArrayOutputStream
InBlock.gif * @throws EngineException
ExpandedSubBlockEnd.gif*/

InBlock.gifpublic OutputStream call( String filename, HashMap parameters ) throws EngineException
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif initilize();
InBlock.gif OutputStream os = run( filename, parameters );
InBlock.gif release();
InBlock.gif
InBlock.gifreturn os;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/** *//**
InBlock.gif * @param args
ExpandedSubBlockEnd.gif*/

InBlock.gifpublic static void main( String[] args )
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif HashMap parameters = new HashMap();
InBlock.gif//三个Report Parameters,名称必须在报表设计文件中预先定义好
InBlock.gif parameters.put( "begindate", "2004/01/01" );
InBlock.gif parameters.put( "enddate", "2007/12/31" );
InBlock.gif parameters.put( "sql", " where cust_id = 1234567" );
InBlock.gif
InBlock.gif ByteArrayOutputStream bos = null;
InBlock.gif
InBlock.gif PDFReportServiceAccess ebr = new PDFReportServiceAccess();
InBlock.gif
InBlock.gif String filename = "C:/projects/birt/batch_report.rptdesign";
InBlock.giftry
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif bos = ( ByteArrayOutputStream ) ebr.call( filename, parameters );
InBlock.gif OutputStream fis = new FileOutputStream( "c:/test.pdf" );
InBlock.gif bos.writeTo( fis );
ExpandedSubBlockEnd.gif }

InBlock.gifcatch ( Exception e )
ExpandedSubBlockStart.gifContractedSubBlock.gif...{
InBlock.gif e.printStackTrace();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedBlockEnd.gif}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7199667/viewspace-1018820/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7199667/viewspace-1018820/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值