birt 报表 api 使用

第一步,初始化ReportDesignHandle和ElementFactory的对象,前者代表生成的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

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值