BIRT开发心得

来源:互联网 发布:北京圣思园java视频 编辑:程序博客网 时间:2024/06/11 21:26

一、报表模板设计环境

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

原创粉丝点击