FastReport之单元格纵向合并

最近在做一个报表,客户坚持要报表实现行合并,使用了TextObject的Duplicates属性,设置为Merge。但是效果不太好,相邻的相似就合并,显得报表很杂乱,效果如下:

而客户想要的效果是这样的:

网上搜了一下关于fastreport单元格合并的文章,有说改源码的,有说写脚本的,还有人卖修改后的控件的,总之没有找到现成的方法。所以在客户的压力下不得不自己想办法解决,首先想着改源码,可是没有源码啊!!!反编译出来的又太杂乱,编译错误一大堆,所以就改为从脚本入手了。

其实要实现客户这种效果,只要解决不同“层级”间即使内容相同也不合并就可以了。怎样使不同“层级”的单元格,即使内容相同也不合并呢,这里用Duplicates属性设置为Merge显然不能实现了,所以我想到了之前做项目遇到过的不可见字符的问题。在ASCII码中有很多特殊字符,比如“(char)0”,它就是一个不显示的字符,如果相邻“层级”的数据按奇偶行的模式分别在前面加上空字符串("")和不可见字符(((char)0).ToString()),岂不是在不同“层级”间就不合并了!

    private void Text5_BeforePrint(object sender, EventArgs e)
    {
      DataSourceBase dsb= Report.GetDataSource("BOMLIST");
      if(lasttext1==""||dsb["LAYER"].ToString()!=lasttext1)
      {
        lasttext1=dsb["LAYER"].ToString();
        lastadd = lastadd==""?((char)0).ToString():"";
      }
      Text5.Text=lastadd+Text5.Text;
    }

因为不同“层级”间“层级”和“物料编号”不会相同,所以选择最左边会出现相同的“物料名称”的BeforePrint事件来做这一动作。由于报表数据加载是从左到右、自上而下,所以后面的TextObject只需要把lastadd加上就可以了。

详细代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;

namespace FastReport
{
  public class ReportScript
  {

    private string lasttext1="";
    private string lastadd="";
    private int loadcount=0;
    private void Text9_BeforePrint(object sender, EventArgs e)
    {
        Text9.Text=lastadd+Text9.Text;
    }

    private void Text5_BeforePrint(object sender, EventArgs e)
    {
      DataSourceBase dsb= Report.GetDataSource("BOMLIST");
      if(lasttext1==""||dsb["LAYER"].ToString()!=lasttext1)
      {
        lasttext1=dsb["LAYER"].ToString();
        lastadd = lastadd==""?((char)0).ToString():"";
      }
      Text5.Text=lastadd+Text5.Text;
    }

    private void Text7_BeforePrint(object sender, EventArgs e)
    {
       Text7.Text=lastadd+Text7.Text;
    }

    private void Text19_BeforePrint(object sender, EventArgs e)
    {
        Text19.Text=lastadd+Text19.Text;
    }

    private void Text21_BeforePrint(object sender, EventArgs e)
    {
       Text21.Text=lastadd+Text21.Text;
    }

    private void Text23_BeforePrint(object sender, EventArgs e)
    {
        Text23.Text=lastadd+Text23.Text;
    }

    private void Text25_BeforePrint(object sender, EventArgs e)
    {
        Text25.Text=lastadd+Text25.Text;
    }

    private void Text27_BeforePrint(object sender, EventArgs e)
    {
        Text27.Text=lastadd+Text27.Text;
    }

    private void Text29_BeforePrint(object sender, EventArgs e)
    {
         Text29.Text=lastadd+Text29.Text;
    }

    private void Text31_BeforePrint(object sender, EventArgs e)
    {
        Text31.Text=lastadd+Text31.Text;
    }
  }
}

这样就可以实现客户想要的效果了。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值