FastReport合并单元格(自己总结)

这个问题攻了多次都没有解决,实在是心力绞碎。。。就是在没有希望的时候。我的好员工(龚蓼)通过百度这个搜索引擎,搜到一篇文章解决了这个问题:

 

--------------------------------------------------------------------------------------------------------------------------------------------------

系统中有一个打印需要实现一个字段的显示合并值,而fastreport只提供了抑制重复值属性(SuppressRepeated),它不能直接 把框线给自动合并,这样虽然隐藏了重复值,但显示上就还是有点不太美观,所以就专门研究出了这样的一个方法。网上虽然也有许多类似的解决方法,不是过于复 杂,就是操作起来很麻烦,这是最最简洁通用的一个写法,所以与大家一起分享。

实现原理: 

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

 

实现方法:

需要合并的字段显示抑制重复值属性(SuppressRepeated)为true。

设置Page1的OnBeforePrint事件,MasterData1的OnBeforePrint事件,代码如下:(代码中红色字体部分注意修改为报表实际的内容)

 

复制代码
var
  iRowCount:Integer;                                                            
  AryHeight:Array of Extended;
      
procedure Page1OnBeforePrint(Sender: TfrxComponent);
var                                                                   
  i,iRepeat: Integer;
  sLastValue,sCurValue: String;                                 
  MyDataSet: TfrxDBDataSet;    
begin            
  MyDataSet := TfrxDBDataSet(Report.GetDataSet('数据集名'));           
  iRowCount := MyDataSet.RecordCount;                  
  SetLength(AryHeight,iRowCount);                                                                
  MyDataSet.First;
  sCurValue := '';                        
  for i := 0 to iRowCount - 1 do begin
    sLastValue := sCurValue;                             
    sCurValue := MyDataSet.DataSet.FieldByName('字段名').AsString;                                     
    if (sLastValue <> '') and (sLastValue = sCurValue) then begin
      iRepeat := iRepeat + 1;                                     
      AryHeight[i] := 0;
      AryHeight[i - iRepeat] := MasterData1.Height * (iRepeat + 1);                                                                    
    end
    else begin
      iRepeat := 0;                 
      AryHeight[i] := MasterData1.Height;                       
    end;                      
    MyDataSet.Next;                    
  end;    
      
end;

procedure MasterData1OnBeforePrint(Sender: TfrxComponent);
begin
  if AryHeight[<Line#> - 1] = 0 then begin          
    Memo15.Visible := False;
  end
  else begin
    Memo15.Visible := True;                                   
    Memo15.Height := AryHeight[<Line#> - 1];
  end;                  
end;

 

begin

end.
复制代码

 

本文来自博客园,作者:del88,转载请注明原文链接:https://www.cnblogs.com/del88/archive/2013/05/25/3099486.html

posted @ 2025-06-18 10:19  @网上邻居  阅读(219)  评论(0)    收藏  举报