《BIRT与WEB整合生成动态表.docx》由会员分享,可在线阅读,更多相关《BIRT与WEB整合生成动态表.docx(8页珍藏版)》请在三一办公上搜索。
1、BIRT与WEB整合生成动态表BIRT与WEB整合生成动态表 有时候生成报表时,表的属性列不固定,这个时候用传统的BIRT构建表满足不了要求,我们可以通过BIRT整合WEB来生成动态表。具体方法如下: 1、 建立数据源和数据集 新建一个 “Scripted Data Source”的数据源;从PieDataSource 数据源中新建一个 Scripted Data Set 数据集。 2、 建立一个空的 Table 项 新建一个1行1列的Table ,拖入适当的布局区域,并绑定一个 数据集 PieDataSet 。 3、 在BIRT 设计页面中绑定JAVA类 1) 在数据集 PieDataSet
2、 的 Event Handler 中绑定一个实现 IScriptedDataSetEventHandlerScriptedDataSetEventHandler 类接)口的自。 2) 点击BIRT的报表设计工具的 layout 页面的空白处,在 Property Editor 的 Event Handler 标签中绑定一个继承 ReportEventAdapter 类的自定义类。 4、 代码编写 1) 数据集的绑定类PieChartDataSetEventHandle代码如下: public class PieChartDataSetEventHandle implements IScript
3、edDataSetEventHandler public void close(IDataSetInstance arg0) throws ScriptException /列属性数据 protected IColumnMetaData meta = null; /表头数量 protected int metaCount = 0; protected IteratorMap ite = null; /表头数据列表 protected List metalist = new ArrayList; /数据键值对列表 protected ListMap pojolist = new /存放 Resu
4、ltVO protected Map hash = new HashMap; ArrayListMap; meta = null; ite = null; SuppressWarnings(unchecked) public boolean describe(IDataSetInstance arg0, public boolean fetch(IDataSetInstance arg0, IUpdatableDataSetRow public void open(IDataSetInstance arg0) throws ScriptException try meta = arg0.get
5、ColumnMetaData; metaCount = meta.getColumnCount; ite = pojolist.iterator; e.printStackTrace; throws ScriptException return false; try return true; Map colmap = ite.next; for (int t = 0; t metalist.size; t+) e.printStackTrace; arg1.setColumnValue(metalist.get(t), throws ScriptException IScriptedDataS
6、etMetaData arg1) metalist.clear; metalist = (List) hash.get(metadata); pojolist = (ListMap) hash.get(result); Iterator iterator = metalist.iterator; while(iterator.hasNext) return true; arg1.addColumn(iterator.next, String.class); arg1) if (ite = null | !ite.hasNext) else colmap.get(String.valueOf(t
7、 + 1); catch (ScriptException e) catch (Exception e) public void afterClose(IReportContext arg0) throws ScriptException public void afterOpen(IDataSetInstance arg0, IReportContext arg1) public void beforeClose(IDataSetInstance arg0, IReportContext arg1) SuppressWarnings(unchecked) public void before
8、Open(IDataSetInstance arg0, IReportContext arg1) public void onFetch(IDataSetInstance arg0, IDataSetRow arg1, IReportContext arg2) throws ScriptException throws ScriptException hash = (Map) arg1.getParameterValue(resultvo); throws ScriptException throws ScriptException 2)PieChartReportEventHandle中的
9、beforeFactory 方法代码: try ElementFactory elementFactory = /获取 Table 的句柄 TableHandle mytable = (TableHandle) /设置 Table 的宽度 mytable.setWidth(100%); PropertyHandle colbinds = mytable.getColumnBindings( ); /取列表头文件 PieResultDao dao=new PieResultDao; Map hash=dao.getResultMap; reportContext.setParameterValu
10、e(resultvo, hash); if(hash!= null&hash.get(metadata)!=null) reportContext.getDesignHandle.getElementFactory; reportContext.getDesignHandle.findElement(PieTable); List list=(List)hash.get(metadata); RowHandle myheader = (RowHandle) RowHandle mydetail = (RowHandle) int i=0;/计列数 /添加数据绑定列 ComputedColumn
11、 cs = cs.setName( column ); cs.setExpression( dataSetRow+column+); colbinds.addItem( cs ); mytable.insertColumn(i,1);/循环插入新列,每次加在最后一/ColumnHandle mycolumn = (ColumnHandle) mytable.getHeader( ).get( 0 );/获得表格头 mytable.getDetail.get(0);/获得明细行 for(String column : list) StructureFactory.createComputedCo
12、lumn( ); 列的后面 mytable.getColumns.get(i); 可以得到表的列添加属性 /mycolumn.setProperty(width, 2cm); /数据列 CellHandle dcell = (CellHandle) /设置数据列样式 dcell.setProperty(borderBottomColor,#000000); dcell.setProperty(borderBottomStyle,solid); dcell.setProperty(borderBottomWidth,1px); dcell.setProperty(borderLeftColor,
13、#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,sol
14、id); dcell.setProperty(borderTopWidth,1px); DataItemHandle data = data.setResultSetColumn( (String)list.get(i); dcell.getContent.add(data);/为单元格设置Data /列头文件 CellHandle hcell = (CellHandle) mydetail.getCells.get(i);/取得明细行单元格 elementFactory.newDataItem(column); myheader.getCells( ).get(i); /设置列头样式 hce
15、ll.setProperty(backgroundColor,#d0d0d0); hcell.setProperty(borderBottomColor,#000000); hcell.setProperty(borderBottomStyle,solid); hcell.setProperty(borderBottomWidth,1px); hcell.setProperty(borderLeftColor,#000000); hcell.setProperty(borderLeftStyle,solid); hcell.setProperty(borderLeftWidth,1px); h
16、cell.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 = myLabel.setText(column); hcell.getContent.add(myLabel); i+; elementFactory.newLabel(column); catch (Exception e) e.printStackTrace;