BIRT开发心得

本文详细介绍了BIRT的报表模板设计环境、运行环境的配置、动态配置报表数据源的方法,以及如何向报表中传递参数。同时,文章还提到了BIRT支持的多种输出格式,包括html、pdf、xls、ppt和rtf。

Birt 小结

一、报表模板设计环境

1.使用如下URL下载报表的设计环境:

http://download.eclipse.org/birt/downloads/

       下载birt-report-framework-2.1.2.zip文件,解压缩该文件到eclipse安装目录下。

      

       2.使用如下URL下载 itext-1.3.jar:

http://prdownloads.sourceforge.net/itext/itext-1.3.jar

3.复制itext-1.3.jareclipse/plugins/com.lowagie.itext_*/lib目录下

二、报表的运行环境

1.  http://download.eclipse.org/birt/downloads/ 中下载运行环境:birt-runtime-2.1.2.zip

解压缩birt-runtime-2.1.2.zip,复制birt-runtime-2_1_2/ReportEngine/lib下的所有文件到WEB应用中WEB-INF/lib/目录下,将下载好的itext-1.3.jar也复制到该目录下。

    2.使用Report API运行报表要设置EngineHome,代码如下:

EngineConfig config = new EngineConfig();

config.setEngineHome( "BirtHomePath" );

其中BirtHomePath就是/birt-runtime-2_1_2/ReportEngine目录

3.在server配置中选择Application标签,将WEB应用的classloader mode的值修改为:PARENT_LAST,入图:

                 ss

4.参考代码片断:

                              EngineConfig config = new EngineConfig();

                              config.setEngineHome( "E:/ReportEngine" );

                              ReportEngine engine = new ReportEngine( config );

                            if(engine==null){

                                     System.out.println("null");

}

                              IReportRunnable design=null;

                              try {

                                     design =

                                            engine.openReportDesign(

                                                   "E:/workspace/TestReport/testreport.rptdesign");

                              } catch (EngineException e) {

                                     // TODO Auto-generated catch block

                                     e.printStackTrace();

                              }

                       IRunAndRenderTask task = engine.createRunAndRenderTask(design);

                       

                              HTMLRenderOption options = new HTMLRenderOption();

                              options.setOutputFileName("c:/customers.pdf");

                              options.setOutputFormat("pdf");

                              task.setRenderOption(options);

 

                              try {

                                     task.run();

                              } catch (EngineException e1) {

                                     // TODO Auto-generated catch block

                                     e1.printStackTrace();

                              }

 

                              engine.destroy();

三、动态配置报表数据源 

1.选择Data Explore标签下Data Sources选项,选择其中需要配置的数据源

2.选择beforeOpen事件,输入如下参考代码:

importPackage( Packages.java.io );

importPackage( Packages.java.util );

fin = new java.io.FileInputStream(new String("e:/config.txt"));

props = new java.util.Properties( );

props.load(fin);

 

extensionProperties.odaURL = new String(props.getProperty("url"));

extensionProperties.odaDriverClass = new String(props.getProperty("driver"));

extensionProperties.odaUser = new String(props.getProperty("userid"));

extensionProperties.odaPassword = new String(props.getProperty("password"));

fin.close();

 

e:/config.txt写入数据源配置信息,例如:

url=jdbc:mysql://localhost:3306/test

driver=com.mysql.jdbc.Driver

userid=root

password=111111

3.以上代码中 数据源配置文件的路径可以作为参数传入。修改如下:

             fin = new java.io.FileInputStream(new String(params["propdir"]));

             在报表文件中添加报表参数 propdir

四、向报表中传递参数

1.设置数据集的查询参数(用于SQL语句的参数)

双击需要编辑的数据集,编写SQL语句:如图:

SQL语句中用?设定了查询参数,双击数据集里的Parameters选项添加参数,如图:

添加参数面板中的Linked To Report Parameter是指数据集的查询参数是否要和报表参数对应起来。一般选择和定义好的报表参数对应。

2.  如果报表中定义了3个参数(pcity,ppname,propdir),如图:

 

使用BIRT提供的API写如下代码:

IReportRunnable design=null;

try {

            design = engine.openReportDesign(                                            "E:/workspace/TestReport/testreport.rptdesign");

} catch (EngineException e) {

                                     // TODO Auto-generated catch block

e.printStackTrace();

}

ReportDesignHandle reportHandle =

( ReportDesignHandle ) design.getDesignHandle( );//获取报表处理实例

String []param_strs={"df","fret","e:/config.txt"};//参数

List allp=reportHandle.getAllParameters();//获得所有参数的处理实例

for(int i=0;i<allp.size();i++){

              ScalarParameterHandle phandle=(ScalarParameterHandle)allp.get(i);

              try {                                                                                                                                         phandle.setDefaultValue(param_strs[i]); //设置每个参数的默认值                                                     } catch (SemanticException e2) {                                                                               e2.printStackTrace();

                     }

}

 

3.遍历报表参数

1)设置BIRTHOME,创建报表引擎

EngineConfig config = new EngineConfig();

config.setEngineHome( "E:/ReportEngine" );

ReportEngine engine = new ReportEngine( config );

2)打开报表模板,获得报表处理实例

IreportRunnable design=engine.openReportDesign("E:/workspace/TestReport/testreport.rptdesign");

ReportDesignHandle reportHandle = ( ReportDesignHandle ) design.getDesignHandle( );

3)遍历所有参数

              List allp=reportHandle.getAllParameters();

        System.out.println("size:"+allp.size());

              for(int i=0;i<allp.size();i++){

if((allp.get(i) instanceof ParameterGroupHandle)||(allp.get(i) instanceof CascadingParameterGroupHandle)){//如果是参数组

                ParameterGroupHandle pgh=(ParameterGroupHandle)allp.get(i);

                SlotHandle shp= pgh.getParameters();

                List shplist=shp.getContents();

                System.out.println("param group:"+pgh.getName());

                for(int j=0;j<shplist.size();j++){

                    ScalarParameterHandle sphl=(ScalarParameterHandle)shplist.get(j);

                    System.out.println("   pn:"+sphl.getName()+"||pdt:"+sphl.getDataType()+"||"+sphl.getControlType()+"||"+sphl.getValueType());

                    i++;

                }

                           

              }else {//如果不是参数组

                  ScalarParameterHandle phandle=(ScalarParameterHandle)allp.get(i);                         System.out.println("pn:"+phandle.getName()+"||pdt:"+phandle.getDataType()+"||"+phandle.getControlType()+"||"+phandle.getValueType());

              }

                                         

                     }

              其中:phandle.getName()――获得参数名

                       phandle.getDataType()――获得参数的数据类型

phandle.getControlType()――获得参数的控制类型,text-box,list-box,combo-box,radio-button

              phandle.getValueType()――获得参数值的类型(static,dynamic

 

  4.根据参数名查找参数

ScalarParameterHandle fpcity=(ScalarParameterHandle)reportHandle.findParameter("pcity");//查找参数

fpcity.setDefaultValue("sd");//设置找到的参数的默认值

五、BIRT支持的输出格式

Birt 支持html,pdf,xls,ppt,rtf的格式输出。其中Birt 本身只支持html,pdf。使用如下URL可以下载支持xls,ppt,rtf格式的文件:

https://sourceforge.net/project/showfiles.php?group_id=166446

 

 
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值