百灵报表(BIRT)接口学习:设计report design

来源:互联网 发布:jsp连接sql的方法 编辑:程序博客网 时间:2024/04/30 07:59

第一步,初始化ReportDesignHandleElementFactory的对象,前者代表生成的report design;后者用来创建所用到的报表元素。

 

IDesignEngine designEngine = new DesignEngine( new DesignConfig( ) );

ReportDesignHandle reportDesignHandle = designEngine.newSessionHandle(

            ULocale.ENGLISH ).createDesign( );

ElementFactory elementFactory = new ElementFactory( reportDesignHandle

            .getModule( ) );

 

第二步,创建Parameter

 

ScalarParameterHandle scalarParameterHandle = elementFactory

            .newScalarParameter( "param" );

scalarParameterHandle.setIsRequired( false );

scalarParameterHandle.setDefaultValue( "Australia" );

reportDesignHandle.getParameters( ).add( scalarParameterHandle );

 

第三步,创建Data Source

 

OdaDataSourceHandle dataSourceHandle = elementFactory.newOdaDataSource(

"data source",

"org.eclipse.birt.report.data.oda.jdbc" );

dataSourceHandle.setProperty(

"odaDriverClass",

"org.eclipse.birt.report.data.oda.sampledb.Driver" );

dataSourceHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" );

dataSourceHandle.setProperty( "odaUser", "ClassicModels" );

// dataSourceHandle.setProperty( "odaPassword", "ClassicModels" );

reportDesignHandle.getDataSources( ).add( dataSourceHandle );

 

第四步,创建Data Set

 

OdaDataSetHandle dataSetHandle = elementFactory.newOdaDataSet(

"data set",

"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );

dataSetHandle.setDataSource( dataSourceHandle.getName( ) );

dataSetHandle.setQueryText( "select * from CLASSICMODELS.CUSTOMERS" );

FilterCondition filterCondition = StructureFactory.createFilterCond( );

filterCondition.setOperator( DesignChoiceConstants.FILTER_OPERATOR_EQ );

filterCondition.setExpr( "row[/"COUNTRY/"]" );

filterCondition.setValue1( "params[/""

            + scalarParameterHandle.getName( ) + "/"].value" );

dataSetHandle.addFilter( filterCondition );

reportDesignHandle.getDataSets( ).add( dataSetHandle );

 

第五步,创建Sytle

 

SharedStyleHandle sharedStyleHandle = elementFactory.newStyle( null );

ColorHandle colorHandle = sharedStyleHandle.getColor( );

colorHandle.setStringValue( "red" );

reportDesignHandle.getStyles( ).add( sharedStyleHandle );

 

第六步,创建Table

 

TableHandle tableHandle = elementFactory.newTableItem(

            "table",

            3,

            1,

            1,

            1 );

 

tableHandle.setProperty(

            StyleHandle.BORDER_TOP_STYLE_PROP,

            DesignChoiceConstants.LINE_STYLE_SOLID );

tableHandle.setProperty(

            StyleHandle.BORDER_LEFT_STYLE_PROP,

            DesignChoiceConstants.LINE_STYLE_SOLID );

tableHandle.setProperty(

            StyleHandle.BORDER_BOTTOM_STYLE_PROP,

            DesignChoiceConstants.LINE_STYLE_SOLID );

tableHandle.setProperty(

            StyleHandle.BORDER_RIGHT_STYLE_PROP,

            DesignChoiceConstants.LINE_STYLE_SOLID );

 

tableHandle.setDataSet( dataSetHandle );

 

ComputedColumn computedColumn1 = StructureFactory

            .createComputedColumn( );

computedColumn1.setName( "CustomerNumber" );

computedColumn1.setDisplayName( "CustomerNumber" );

computedColumn1.setDataType(

DesignChoiceConstants.COLUMN_DATA_TYPE_INTEGER );

computedColumn1.setExpression( "dataSetRow[/"CUSTOMERNUMBER/"]" );

tableHandle.addColumnBinding( computedColumn1, false );

 

ComputedColumn computedColumn2 = StructureFactory

            .createComputedColumn( );

computedColumn2.setName( "CustomerName" );

computedColumn2.setDisplayName( "CustomerName" );

computedColumn2.setDataType(

DesignChoiceConstants.COLUMN_DATA_TYPE_STRING );

computedColumn2.setExpression( "dataSetRow[/"CUSTOMERNAME/"]" );

tableHandle.addColumnBinding( computedColumn2, false );

 

ComputedColumn computedColumn3 = StructureFactory

            .createComputedColumn( );

computedColumn3.setName( "Country" );

computedColumn3.setDisplayName( "Country" );

computedColumn3.setDataType(

DesignChoiceConstants.COLUMN_DATA_TYPE_STRING );

computedColumn3.setExpression( "dataSetRow[/"COUNTRY/"]" );

tableHandle.addColumnBinding( computedColumn3, false );

 

ComputedColumn computedColumn4 = StructureFactory

            .createComputedColumn( );

computedColumn4.setName( "Total Count" );

computedColumn4.setDisplayName( "Total Count" );

computedColumn4.setAggregateFunction( "COUNT" );

computedColumn4.setDataType(

DesignChoiceConstants.COLUMN_DATA_TYPE_INTEGER );

computedColumn4.setExpression( "row[/"CustomerNumber/"]" );

tableHandle.addColumnBinding( computedColumn4, false );

 

RowHandle headerRowHandle = (RowHandle) tableHandle

            .getHeader( )

            .get( 0 );

 

headerRowHandle.setProperty(

            StyleHandle.TEXT_ALIGN_PROP,

            DesignChoiceConstants.TEXT_ALIGN_LEFT );

 

CellHandle headerCellHandle1 = (CellHandle) headerRowHandle

            .getCells( )

            .get( 0 );

LabelHandle labelHandle1 = elementFactory.newLabel( "CustomerNumber" );

labelHandle1.setText( "CustomerNumber" );

headerCellHandle1.getContent( ).add( labelHandle1 );

 

CellHandle headerCellHandle2 = (CellHandle) headerRowHandle

            .getCells( )

            .get( 1 );

LabelHandle labelHandle2 = elementFactory.newLabel( "CustomerName" );

labelHandle2.setText( "CustomerName" );

headerCellHandle2.getContent( ).add( labelHandle2 );

 

CellHandle headerCellHandle3 = (CellHandle) headerRowHandle

            .getCells( )

            .get( 2 );

LabelHandle labelHandle3 = elementFactory.newLabel( "Country" );

            labelHandle3.setText( "Country" );

            headerCellHandle3.getContent( ).add( labelHandle3 );

 

RowHandle detailRowHandle = (RowHandle) tableHandle

            .getDetail( )

            .get( 0 );

 

CellHandle detailCellHandle1 = (CellHandle) detailRowHandle

            .getCells( )

            .get( 0 );

DataItemHandle dataItemHandle1 = elementFactory

            .newDataItem( "CustomerNumber" );

dataItemHandle1.setResultSetColumn( computedColumn1.getName( ) );

detailCellHandle1.getContent( ).add( dataItemHandle1 );

 

CellHandle detailCellHandle2 = (CellHandle) detailRowHandle

            .getCells( )

            .get( 1 );

DataItemHandle dataItemHandle2 = elementFactory

            .newDataItem( "CustomerName" );

dataItemHandle2.setResultSetColumn( computedColumn2.getName( ) );

detailCellHandle2.getContent( ).add( dataItemHandle2 );

 

CellHandle detailCellHandle3 = (CellHandle) detailRowHandle

            .getCells( )

            .get( 2 );

DataItemHandle dataItemHandle3 =

elementFactory.newDataItem( "Country" );

dataItemHandle3.setResultSetColumn( computedColumn3.getName( ) );

detailCellHandle3.getContent( ).add( dataItemHandle3 );

 

RowHandle footerRowHandle = (RowHandle) tableHandle

            .getFooter( )

            .get( 0 );

 

CellHandle footerCellHandle1 = (CellHandle) footerRowHandle

            .getCells( )

            .get( 0 );

LabelHandle labelHandle4 = elementFactory.newLabel( "Total Count" );

labelHandle4.setText( "Total Count" );

footerCellHandle1.getContent( ).add( labelHandle4 );

 

CellHandle footerCellHandle2 = (CellHandle) footerRowHandle

            .getCells( )

            .get( 1 );

DataItemHandle dataItemHandle4 = elementFactory

            .newDataItem( "Total Count" );

dataItemHandle4.setResultSetColumn( computedColumn4.getName( ) );

            footerCellHandle2.getContent( ).add( dataItemHandle4 );

 

tableHandle.setStyle( sharedStyleHandle );

 

reportDesignHandle.getBody( ).add( tableHandle );

 

第七步,输出report design

 

ModuleHandle moduleHandle = reportDesignHandle.getModuleHandle( );

moduleHandle.saveAs( getOutputFolder( ) + File.separator + report );

reportDesignHandle.close( );

 

Web Viewer中预览生成的报表应该可以得到这样的输出:

 

CustomerNumber

CustomerName

Country

114

Australian Collectors, Co.

Australia

276

Anna's Decorations, Ltd

Australia

282

Souveniers And Things Co.

Australia

333

Australian Gift Network, Co

Australia

471

Australian Collectables, Ltd

Australia

Total Count

5

 

 

原创粉丝点击