集智平台报表动态合并单元格

最近一段时间的工作中,遇到这样一个客户需求,客户希望动态合并单元格,合并的规则就是逐行遍历每一行的数据,如果同一行连续两个单元格的值相同,那么就将这两个单元合并到一起。

这个需求只能通过API来实现,实现的思路为:先将报表计算为IReport对象,然后逐行遍历每个单元格的值,将这个值跟后面单元格的值比较,如果两个值相同,那么就构造一个Area对象,把这两个单元格合并到一起。下面就做一个简单的例子,实现动态的将报表同一行连续两个值相同的单元格合并到一起,并把报表放到JFrame里面展现。

第一步:新建一张报表。

这里为了简便明了,报表没有连接数据源也没有构造数据集,只是将一些数字写到报表模板里。

第二步:编写API代码,实现报表的读入,计算,合并单元格和展现

API的代码如下:

package com.zhengzhong.practise;

import javax.swing.JFrame;

import com.runqian.report4.model.ReportDefine;

import com.runqian.report4.model.engine.ExtCellSet;

import com.runqian.report4.usermodel.Area;

import com.runqian.report4.usermodel.CSReport;

import com.runqian.report4.usermodel.Context;

import com.runqian.report4.usermodel.Engine;

import com.runqian.report4.usermodel.INormalCell;

import com.runqian.report4.usermodel.IReport;

import com.runqian.report4.util.ReportUtils;

public class MergeReport {

/**

@param args

@throws Exception

*/

public IReport caculate(String raq) throws Exception{

ReportDefine rd = (ReportDefine)ReportUtils.read(raq);

ExtCellSet.setLicenseFileName(“D:/安装文件/润乾安装/License4.0[64100621100826_640000]/技术应用中心专用授权Server2010-12-31V4.0Windows.lic”);

Context cxt = new Context();

Engine engine = new Engine(rd,cxt);

IReport ir = engine.calc();

//int j =ir.getRowCount();

for(int i =1;i<=ir.getRowCount();i++){

for(int j =1;j<ir.getColCount() ; j++){

INormalCell cell1 = ir.getCell(i, (short)j);

INormalCell cell2 = ir.getCell(i, (short)(j+1));

//System.out.println(“cell1================”+cell1.getValue().toString());

//System.out.println(“cell2================”+cell2.getValue().toString());

if(cell1.getValue().toString() == cell2.getValue().toString()||cell1.getValue().toString().equals(cell2.getValue().toString())){

Area area=new Area(i,(short)j,i,(short)(j+1));

ReportUtils.mergeReport(ir, area);

}

}

}

return ir

}

public static void main(String[] args) throws Throwable {

// TODO Auto-generated method stub

MergeReport mr =new MergeReport();

IReport ir = mr.caculate(“F:/mergeReport.raq”);

CSReport csr = new CSReport(ir);

JFrame frame = new JFrame();

frame.getContentPane().add(csr.getDisplayPane()); //设置JFrame的相关属性并展现

frame.setSize(600, 400);

frame.setLocation(200, 100);

frame.setVisible(true);

}

}

上面代码中的caculate(String raq)方法作用是读进来需要计算的报表,配置报表引擎,计算出一个IReport对象,然后按行遍历IReport对象的单元格,用以下方法:

INormalCell cell1 = ir.getCell(i, (short)j);

INormalCell cell2 = ir.getCell(i, (short)(j+1));

if(cell1.getValue().toString() == cell2.getValue().toString()||cell1.getValue().toString().equals(cell2.getValue().toString())){

Area area=new Area(i,(short)j,i,(short)(j+1));

ReportUtils.mergeReport(ir, area);

}

判断两个连续的单元格的值是否相等,如果相等就构造一个关于这两个单元格的Area对象,再用ReportUtils.mergeReport()方法将这个对象设置好。最后这个caculate(String raq)方法返回一个设置好的 IReport对象。

main(String[] args)方法的作用是调用上面的calculate方法,返回一个IReport对象,然后把这个IReport对象放到JFrame中展示。

第三步:编辑执行上面的API代码。

这样,动态的将报表同一行连续两个值相同的单元格合并到一起,并把报表放到JFrame里面展现的需求就实现了。