BCB调用FastReport4合并单元格

注意:需要在控制报表的BCB程序头文件.h中加上类TfrxDBDataSet的头文件 #include "frxCustomDBRTTI.hpp" ,在.cpp文件的头部加上#pragma link "frxCustomDBRTTI"。如果不加则会报没有定义类型TfrxDBDataSet的错误。

合并单元格实现原理:

在报表开始前先计算出需要合并的行的高度数组,在打印前根据这个高度数组设置对应打印对象的高度和显示。

实现方法如下:

设置Page1的OnBeforePrint事件,MasterData1的OnBeforePrint事件,代码如下:

Var 
 iRowCount:Integer;
 AryStatHeight:Array of Extended;
 AryRoadHeight:Array of Extended;
 AryStation:Array of String;
 AryLine:Array of String;       
  
procedure Page1OnBeforePrint(Sender: TfrxComponent);
var 
 i,j,iRepeat: Integer;
 sLastValue,sCurValue: String;
 MyDataSet:TfrxDBDataSet;         
 begin
    MyDataSet := TfrxDBDataSet(Report.GetDataSet('frxdbdtst1'));
    iRowCount := MyDataSet.RecordCount;
    
    //逐记录取值,同一个入口站的则记录要合并的单元格  
    SetLength(AryStatHeight,iRowCount);
    SetLength(AryStation,iRowCount);        
    MyDataSet.First;  
    sCurValue := '';
    j := 1;        
    for i := 0 to iRowCount - 1 do
    begin
       sLastValue := sCurValue;
       sCurValue := MyDataSet.DataSet.FieldByName('EnRoadID').AsString + MyDataSet.DataSet.FieldByName('EnStationID').AsString;
       //若有重复单元格,则标记当前重复单元格的高度为0,第一个重复单元格的高度为重复单元格高度的和
       if (sLastValue <> '') and (sLastValue = sCurValue) 
          and ((i + 36 - 31) mod 36<>0) then         //    
       begin
          iRepeat := iRepeat + 1;
          AryStatHeight[i] := 0;
          AryStatHeight[i - iRepeat] := MasterData1.Height * (iRepeat + 1);
       end               
       else
       begin
          iRepeat := 0;
          AryStatHeight[i] := MasterData1.Height;
       end;
       //若有重复单元格,则标记该重复单元格的行号为空                        
       if (sLastValue <> '') and (sLastValue = sCurValue) then  
       begin
          AryStation[i] := '';           
       end  
       else
       begin                     
          AryStation[i] := IntToStr(j);
          j := j + 1;                     
       end;           
       MyDataSet.Next;
    end;

    //逐记录取值,同个入口路段的则记录要合并的单元格
    SetLength(AryRoadHeight,iRowCount);          
    MyDataSet.First;
    sCurValue := '';                   
    for i := 0 to iRowCount - 1 do
    begin
       sLastValue := sCurValue;
       sCurValue := MyDataSet.DataSet.FieldByName('EnRoadID').AsString;
       //若有重复单元格,则标记当前重复单元格的高度为0,第一个重复单元格的高度为重复单元格高度的和
       if (sLastValue <> '') and (sLastValue = sCurValue) and ((i + 36 - 31) mod 36<>0) then
       begin
          iRepeat := iRepeat + 1;
          AryRoadHeight[i] := 0;
          AryRoadHeight[i - iRepeat] := MasterData1.Height * (iRepeat + 1);
       end  
       else
       begin
          iRepeat := 0;
          AryRoadHeight[i] := MasterData1.Height;                      
       end;                                                                                       
       MyDataSet.Next;
    end;
    
    //重新控制报表显示行号
    SetLength(AryLine,iRowCount);        
    MyDataSet.First;
    sCurValue := '';
    j := 1;                  
    for i := 0 to iRowCount - 1 do
    begin
       sLastValue := sCurValue;
       sCurValue := MyDataSet.DataSet.FieldByName('EnRoadID').AsString;
       //若有重复单元格,则标记该重复单元格的行号为空,非重复单元格则行号递增
       if (sLastValue <> '') and (sLastValue = sCurValue) then
       begin
          AryLine[i] := '';           
       end  
       else
       begin                     
          AryLine[i] := IntToStr(j);
          j := j + 1;                     
       end;                                                                                       
       MyDataSet.Next;
    end;        
 end;     

procedure MasterData1OnBeforePrint(Sender: TfrxComponent);
begin
    //同一入口站合并
    if AryStatHeight[<Line#> - 1] = 0 then
    begin
       Memo56.Visible := False;          
       Memo57.Visible := False;
       Memo59.Visible := False; 
    end
    else
    begin
       Memo56.Visible := True;
       Memo56.Height := AryStatHeight[<Line#> - 1];          
       Memo57.Visible := True;
       Memo57.Height := AryStatHeight[<Line#> - 1];
       Memo59.Visible := True;
       Memo59.Height := AryStatHeight[<Line#> - 1];            
    end;
    //同一入口路段合并
    if AryRoadHeight[<Line#> - 1] = 0 then
    begin                       
       Memo48.Visible := False;
       Memo49.Visible := False;
       Memo61.Visible := False;
       Memo62.Visible := False;
       Memo1.Visible := False; 
       Memo2.Visible := False;
    end
    else
    begin
       Memo48.Visible := True;
       Memo48.Height := AryRoadHeight[<Line#> - 1];
       Memo49.Visible := True;
       Memo49.Height := AryRoadHeight[<Line#> - 1];
       Memo61.Visible := True;
       Memo61.Height := AryRoadHeight[<Line#> - 1];
       Memo62.Visible := True;
       Memo62.Height := AryRoadHeight[<Line#> - 1];
       Memo1.Visible := True;
       Memo1.Height := AryRoadHeight[<Line#> - 1];
       Memo2.Visible := True;
       Memo2.Height := AryRoadHeight[<Line#> - 1];           
    end;

    if AryStation[<Line#> - 1] = '' then
    begin                         
       Memo56.Font.Color:=clWhite;
       Memo57.Font.Color:=clWhite;
       Memo59.Font.Color:=clWhite;           
    end
    else              
    begin                        
       Memo56.Font.Color:=clBlack;
       Memo57.Font.Color:=clBlack;
       Memo59.Font.Color:=clBlack;           
    end;
    
    if AryLine[<Line#> - 1] = '' then
    begin
       Memo48.Memo.Clear();
       Memo49.Font.Color:=clWhite;
       Memo61.Font.Color:=clWhite;
       Memo62.Font.Color:=clWhite;
       Memo1.Font.Color:=clWhite;
       Memo2.Font.Color:=clWhite;             
    end
    else              
    begin
       Memo48.Memo.Clear();
       Memo48.Memo.Add(AryLine[<Line#> - 1]);              
       Memo49.Font.Color:=clBlack;        
       Memo61.Font.Color:=clBlack;
       Memo62.Font.Color:=clBlack;
       Memo1.Font.Color:=clBlack;
       Memo2.Font.Color:=clBlack;             
    end;        
end;

begin

end.

流程:

第一步.准备好要合并单元格的报表;

第二步.双击事件的‘OneBeforePrint’对应CODE代码(确保完成第三步,否则代码无法生效,完成后直接预览),首先是Page1的事件设置,   其次是MasterDate1的事件设置;

第三步.在CODE页写入代码。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
:首先是要分组,因为如果不分组,想合并的列与其它列会自动打印。这样,只能使用FR自带的隐藏重复数据的功能,但很多类似报表都是要求合并行居中的,这点FR就不能自动实现了。 2:要全并的列放在分组脚中,其它放在分组数据BAND中,这样,打印完分组数据BAND后,动态改变分组脚中要合并列的TOP和HEIGHT就可以了。而且这样打印,因为要合并的行实际上只打印了一遍,因此应该效率更高。所以这就需要在打印分组数据BAND后有一个可记录此分组已打印高度的功能。 3:代码解释 procedure MCOnAfterData(Sender: TfrxComponent); begin if =1 then MC.Tag:=Int(MC.Height) else MC.Tag:=MC.Tag+Int(MC.Height); end; 因为FR的在每次分组后重新计数,使用MC.Tag保存现在数据BAND上的所有不合并行的总高度。当然也可以使用一个变量,但放着这么多TAG不用,多浪费呀。况且使用TAG的语法也比使用变量简单,使用变量使程序看上去很难懂。 if >=1 then 这样的写法看起来多痛苦呀。 procedure MBBOnAfterData(Sender: TfrxComponent); begin MBB.Tag:=GetStandHeight(MBB.CalcHeight); //这一句是为了保持行高的一致性,如果不需要直接 MBB.Tag:=MBB.CalcHeight; //这样可能计算的行高不是标准行高的整数倍。 MBB.Visible:=False; end; procedure MD1OnBeforePrint(Sender: TfrxComponent); begin if =1 then MBB.Visible:=True; end; 上二句只是为了不多次统计CalcHeight,但不能直接设可视性为假,否则不会触发MBBOnAfterData事件。 计算此例中可能会很高的列的计算高度,这是在宽度已经确定的情况下计算的。 因为此例中的第二列单行高度可能大于其它列的总高度。如果没有此例中的特殊情况,可以省略。 4:如果此例中没有第二列单行高度可能大于其它列的总高度的话,MBB是不需要的。 5:如果不是要求空行为多行多列显示的话,GFOnBeforePrint也是不需要的,只需要在GF上放二个MEMOVIEW,宽高和左边距分别与上方的二个相同,调用我例子中注释掉的代码改变它们的TOP和HEIGHT即可。 6:TAG是所有Tcomponent的属性呀,这是我最常使用的一个属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值