實現效果概述:當打印一些數據時,我們希望每頁都打印20行,當不夠時,打印出空白表格!
這是怎麼實現的呢?下面我說一下實現原理,報表通過FastReport實現
第一步:報表中需要用到的組建
PAGEHEADER,MasterData,Child(通過右鍵點擊MasterData>Add child band),Footer
在Child中用MEMO畫空白表格,顯示的樣式和MaterData中的數據樣式相同!
第二步:在工具箱左上角有選項卡,選擇CODE選項卡,打開代碼模塊!
第三步:在BEGIN
END.的上面聲明變量
此時代碼模塊的樣子
var
PageLine: integer; //在現在頁列印到第幾行
PageMaxRow: integer=15; //設定每頁列數
Begin
End.
第四步:在工具箱左上角選項卡中,切換到PAGE1選項卡,打開頁面模塊!
第五步:選擇MasterData,在工具箱左上角點Events(事件)→OnBeforePrint,雙擊,進入該事件代碼區域
寫入代碼
Begin
PageLine := <Line> mod PageMaxRow;
if (PageLine = 1) and (<line> > 1) then
Engine.newpage;
child1.visible := False;
End;
第六步:選擇Footer,在工具箱左上角點Events(事件),→OnBeforePrint,雙擊,進入該事件代碼區域
寫入代碼
var
i: integer;
begin
i := iif(PageLine=0, PageMaxRow, PageLine);
child1.visible := True;
while i < PageMaxRow do
begin
i := i + 1;
Engine.ShowBand(Child1); //印空白表格
end;
child1.visible := False;
end;
此時代碼區域的代碼爲:
var
PageLine: integer; //在現在頁列印到第幾行
PageMaxRow: integer=15; //設定每頁列數
procedure MasterData1OnBeforePrint(Sender: TfrxComponent);
begin
PageLine := <Line> mod PageMaxRow;
if (PageLine = 1) and (<line> > 1) then
Engine.newpage;
child1.visible := False;
end;
//Footer1高度設為0
procedure Footer1OnBeforePrint(Sender: TfrxComponent);
var
i: integer;
begin
i := iif(PageLine=0, PageMaxRow, PageLine);
child1.visible := True;
while i < PageMaxRow do
begin
i := i + 1;
Engine.ShowBand(Child1); //印空白表格
end;
child1.visible := False;
end;
begin
end.
需要注意的問題:當打印時,比如數據爲1條,那麼打印出來的效果應該爲一頁15條數據,14行爲空行!
假如出現了兩頁,而且第二頁只顯示了一行或者幾行,此時說明你的MasterData和Child中MEMO的高度太高,不可以裝下15行數據,此時需要調節這兩個組建中MEMO的HEIGHT屬性,直到可以顯示正確結果爲止!(多餘一頁,降低HEIGHT,一頁未顯示完,增加HEIGHT)
最終效果: