效果简述:
根据你传入的参数,动态展现展示列。
例如传入参数:columnNames: "EMPLOYEENUMBER,FIRSTNAME,LASTNAME,EMAIL,OFFICECODE",则显示这五列。
见图 figure1
试验环境:
1:birt-report-designer-all-2_3_1 集成eclipse BIRT设计工具
2:集成环境自带样例数据源:ClassicModels
3:选用集成数据集 select * from CLASSICMODELS.EMPLOYEES
实现步骤:
1:设计简单Birt报表模板(dynamicTableColumns.rptdesign),设置数据源、数据集、画表等,详细不多说了,参见《birt中文手册.chm》。
2:编写java class(DynamicTableColumnHandle.class) 拓展 ReportEventAdapter 实现动态列报表。
3:绑定报表模板eventHandle。
1.1 设计报表模板(dynamicTableColumns.rptdesign)详细格式如见图figure2
2.1 编写 java class(DynamicTableColumnHandle.class)
2.1.1重写initialize方法,解析动态列名。
- @Override
- public void initialize(IReportContext reportContext) {
- // TODO Auto-generated method stub
- try{
- super.initialize(reportContext);
- String columnsStr = (String)reportContext.getParameterValue("columnNames");
- dynamicColumns = doSplit(columnsStr, ","); //split columnsStr
- }catch (Exception e) {
- e.printStackTrace();
- }
2.1.2重写beforeFactory,动态插入展示列,并设置样式。
- @Override
- public void beforeFactory(IReportDesign report, IReportContext reportContext) {
- // TODO Auto-generated method stub
- super.beforeFactory(report, reportContext);
- try{
- ElementFactory elementFactory = reportContext.getDesignHandle().getElementFactory();
- TableHandle mytable = (TableHandle) reportContext.getDesignHandle().findElement("mytable");
- mytable.setWidth("100%");
- RowHandle myheader = (RowHandle) mytable.getHeader( ).get( 0 );//获得表格头
- RowHandle mydetail = (RowHandle) mytable.getDetail().get( 0 );//获得明细行
- ColumnHandle mycolumn0 = (ColumnHandle) mytable.getColumns().get(0);//获取固定列
- mycolumn0.setProperty("width", "2cm");
- mycolumn0.setProperty("backgroundColor", "white");
- // mycolumn0.drop(); //drop the 0 column
- String[] colors = {"red","orange","yellow","green","blue","navy","purple"};
- int i=0;//计列数
- for(String column : dynamicColumns){
- mytable.insertColumn(i+1,1);//循环插入新列,每次加在最后一列的后面
- ColumnHandle mycolumn = (ColumnHandle) mytable.getColumns().get(i+1);
- mycolumn.setProperty("width", "2cm");
- mycolumn.setProperty("backgroundColor", colors[i%7]);
- mycolumn.setProperty("color", "black");
- CellHandle dcell = (CellHandle) mydetail.getCells().get(i+1);//取得明细行单元格
- //设置样式
- dcell.setProperty("borderBottomColor","#000000");
- dcell.setProperty("borderBottomStyle","solid");
- dcell.setProperty("borderBottomWidth","1px");
- dcell.setProperty("borderLeftColor","#000000");
- dcell.setProperty("borderLeftStyle","solid");
- dcell.setProperty("borderLeftWidth","1px");
- dcell.setProperty("borderRightColor","#000000");
- dcell.setProperty("borderRightStyle","solid");
- dcell.setProperty("borderRightWidth","1px");
- dcell.setProperty("borderTopColor","#000000");
- dcell.setProperty("borderTopStyle","solid");
- dcell.setProperty("borderTopWidth","1px");
- DataItemHandle myDataItem2 = elementFactory.newDataItem(column);
- myDataItem2.setResultSetColumn(column);
- dcell.getContent().add(myDataItem2);
- CellHandle hcell = (CellHandle) myheader.getCells( ).get(i+1);
- //设置样式
- hcell.setProperty("borderBottomColor","#000000");
- hcell.setProperty("borderBottomStyle","solid");
- hcell.setProperty("borderBottomWidth","1px");
- hcell.setProperty("borderLeftColor","#000000");
- hcell.setProperty("borderLeftStyle","solid");
- hcell.setProperty("borderLeftWidth","1px");
- hcell.setProperty("borderRightColor","#000000");
- hcell.setProperty("borderRightStyle","solid");
- hcell.setProperty("borderRightWidth","1px");
- hcell.setProperty("borderTopColor","#000000");
- hcell.setProperty("borderTopStyle","solid");
- hcell.setProperty("borderTopWidth","1px");
- LabelHandle myLabel = elementFactory.newLabel(column);
- myLabel.setText(column);
- hcell.getContent().add(myLabel);
- i++;
- }
- pw.println("beforeFactory add tables done");
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
3.1 绑定报表模板的eventHandle,详见下图figure3
参考资料:
1:birt设计器中文手册.chm http://download.csdn.net/user/hob007
2:报表模板及javaClass源文件(birt_API_动态列_实现.rar) http://hob007.download.csdn.net/