润乾报表动态合并单元格



一些客户希望动态合并单元格,合并的规则是浏览每一行的数据,如果同一行相邻两个单元格的值相同,那么就将这两个单元合并到一起。

这个需求可以通过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对象的单元格。

以下代码用来判断两个连续的单元格的值是否相等,如果相等就构造一个关于这两个单元格的Area对象,再用 ReportUtils.mergeReport()方法将这个对象设置好。最后这个caculate(String raq)方法返回一个设置好的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);
}

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

第三步:编辑执行上面的API代码,查看效果。

在Myeclipse中运行上面的代码就可以看到如下图的效果:

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值