[翻譯] FastReport TfrxReport組件使用


一:加載和保存報表

報表默認保存在項目窗體文件中,大多數情況下,沒有更多的操作要深圳市, 因此,你不需要采取特別措施來載入報告.如果你決定保存報表到文件或是數據庫中 (這樣更靈活, 比如修改報表不用重編譯程序), 你可以使用 “TfrxReport” 組件是加載和保存方法:

function LoadFromFile(const FileName: String; ExceptionIfNotFound: Boolean = False): Boolean;

按文件名加載報表. 第二個參數如果是“True” 並且報表文件沒找到, 將報異常. 如要報表加載完成,返回 “True.”

procedure LoadFromStream(Stream: TStream);

從流中加載一個報表

procedure SaveToFile(const FileName: String);

保存報表到指定的文件

procedure SaveToStream(Stream: TStream);

保存報表到流

報表文件的擴展名默認為“FR3”.

舉例:

frxReport1.LoadFromFile('c:\1.fr3');

frxReport1.SaveToFile('c:\2.fr3');

二:設計報表

顯示報表設計窗體,調用“TfrxReport.DesignReport” 方法. 需要引用相關單元。

方法"DesignReport" 有兩個默認參數。

procedure DesignReport(Modal: Boolean = True; MDIChild: Boolean = False);

第一個參數控制是否以模態方式顯示窗體。第二個參數控制是否為MDI子窗體。

舉例:

frxReport1.DesignReport;

三:運行(顯示)一個報表

使用“TfrxReport”組件以下的兩個方法啟動一個報表:

procedure ShowReport(ClearLastReport: Boolean = True);

啟動一個報表,並顯示預覽窗口. 如果參數“ClearLastReport” 等於“False” 報表將添加到先前創建好的報表后面, 否則先前的報表將清除 (默認).

function PrepareReport(ClearLastReport: Boolean = True): Boolean;

啟動一個報表,沒有預覽窗口. 參數跟方法“ShowReport” 一樣. 報表創建完成,返回“True”。

 

大多數情況, 更多使用第一種方法. 會顯示預覽窗口, 繼續構建新報表.

參數“ClearLastReport” 方便控制以下情況,當需要添加其他報表時 。(批量打印報表等情況)

舉例:

frxReport1.ShowReport;

四:預覽報表

預覽窗口顯示報表有2個方法: 一個是調用 “TfrxReport.ShowReport” 方法, 一個是調用“TfrxReport.ShowPreparedReport” 方法. 第二個方法中報表構建的動作不執行,但報表已經完成顯示. 這意味着,你可以事先構建報表借助“preparereport”方法, 或加載先前從文件加載的報表。

舉例:

if frxReport1.PrepareReport then

frxReport1.ShowPreparedReport;

在這種情況下, 報表首先構建完成, 之后顯示在預覽窗體. 構建一個大報表可能花很多時間, 這就是為什么使用“showreport”(准同步方法),比使用“PrepareReport/ShowPreparedReport”更好. 可以指定預覽方式,通過“tfrxreport.previewoptions”屬性進行設置。

五:打印報表

大多數情況, 你從預覽窗口打印報表. 手動打印報表,可以調用“TfrxReport.Print”方法, 舉例:

frxReport1.LoadFromFile(...);

frxReport1.PrepareReport;

frxReport1.Print;

在同一時間, 彈出對話框,可以設置打印參數,. 你可以設置不顯示此對話框,設置“TfrxReport.PrintOptions” 屬性。

六:加載和保存已完成的報表

報表可以在預覽窗口中執行此操作. 也可以手動執行 “TfrxReport.PreviewPages” 方法:

function LoadFromFile(const FileName: String; ExceptionIfNotFound: Boolean = False): Boolean;

procedure SaveToFile(const FileName: String);

procedure LoadFromStream(Stream: TStream);

procedure SaveToStream(Stream: TStream);

配置和參數等與相應的TfrxReport方法一樣。一個文件中包含已完成的報告,其擴展名默認為"fp3"。

舉例:

frxReport1.PreviewPages.LoadFromFile('c:\1.fp3');

frxReport1.ShowPreparedReport;

注意,此種完成的報表在加載完畢后,其預覽是通過“showpreparedreport”方法!

七:導出報表

可以在預覽窗口執行此操作. 也可以手動調用“TfrxReport.Export” 方法. 方法的參數要指定要導出的文件類型,如:

frxReport1.Export(frxHTMLExport1);

導出組件必須可用 (組件放在窗體上) 並設置正確.

八:創建一個自定義的預覽窗體

FastReport顯示報表在一個標准的預覽窗體中。 可以創建自定義的預覽窗體,使用組件“TfrxPreview”。

使用組件時有兩個典型問題. 組件沒有處理相關按鍵(光標, PgUp, PgDown etc) 和鼠標滾動。 讓TfrxPreview 的按鍵可以正常工作,把組件設置成當前的焦點 (比如在窗體的 OnShow 事件寫代碼處理),如下:

frxPreview.SetFocus;

讓TfrxPreview 組件可以響應鼠標滾動操作, 你需要創建 OnMouseWheel 事件處理,並調用 TfrxPreview.MouseWheelScroll 方法,如下:

procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState;

WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);

begin

frxPreview1.MouseWheelScroll(WheelDelta);

end;

九:建立混合型的報表

某些情況下,在一次打印中需要幾個報表,或多個報表放在一個預覽窗口. FastReport中提供了工具,允許在已有報表上添加新的報表. «TfrxReport.PrepareReport» 方法有一個布爾型的參數選項 «ClearLastReport» , 默認等於 «True» .此參數定義是否清除之前構建的報表. 以下代碼演示加載多個報表:

frxReport1.LoadFromFile('1.fr3');

frxReport1.PrepareReport;

frxReport1.LoadFromFile('2.fr3');

frxReport1.PrepareReport(False);

frxReport1.ShowPreparedReport;

我們加載第一個報表,並構建他,但不顯示. 當我們用同一對象構建第二個報表時,參數 «ClearLastReport» 等於 «False». 這樣允許第二個報表添加到之前報表的后,最后在預覽窗口中顯示構建好的報表。

A:混合報表中的頁碼

你可以使用«Page,» «Page#,» «TotalPages,» 和«TotalPages#» 等系統變量來顯示頁碼,在混合報表中,這些變量以意義如下:

Page – 當前報表的頁碼

Page# - 整批報表的頁碼(意即全局的)

TotalPages – 當前報表的所有頁匯總 (a report must be a two-pass one)

TotalPages# - 整批報表的頁碼匯總.

B:混合報表中組合頁

正如上面所說, 報表設計頁面中的 PrintOnPreviousPage  屬性讓你打印時接在前一頁, 即 使用前一頁的空白空間. 在混合型報表中, 允許你從前一報表的空白開始創建新的報表. 要執行此操作,你在設計每個連續報表的第一頁時,打開 PrintOnPreviousPage 屬性。

十:交互式報表

交互式報表,在預覽窗口所有報表對象定義一個鼠標單擊的響應。例如,用戶可以單擊數據行,並且運行新的報表顯示數據行的明細信息。

任何報表都可以成為交互式報表. 要執行此操作,你需要創建一個 TfrxReport.OnClickObject 事件處理,舉例如下:

procedure TForm1.frxReport1ClickObject(Page: TfrxPage; View: TfrxView;Button: TMouseButton; Shift: TShiftState; var Modified: Boolean);

begin

if View.Name = 'Memo1' then

ShowMessage('Memo1 contents:' + #13#10 + TfrxMemoView(View).Text);

if View.Name = 'Memo2' then

begin

TfrxMemoView(View).Text := InputBox('Edit', 'Edit Memo2 text:', TfrxMemoView(View).Text);

Modified := True;

end;

end;

在事件«OnClickObject» 中, 你還可以做以下事情:

- 修改頁或報表對象的內容(因此, «Modified» 標志應該指定,因此該修改將考慮在內);

- 調用«TfrxReport.PrepareReport» 方法重建報表。

在上面例子中, Memo2修改了內容,所以設置了Modified := True.

以同樣的方式, 可以定義不同的響應方式; 比如你可以, 運行一個新的報表. 需要注意以下幾點。在FastReport 3 版本中, 一個TfrxReport組件只能在一個預覽窗體中顯示 (不像FastReport 2.x 版本). 這就是為什么要運行報表要在一個單獨的TfrxReport對象,或在同一個中,但當前的報表必須清除。

在可單擊的對象上給用戶一個提示信息,我們可以修改光標,設置對象的cursor 屬性。

可點擊的對象有一個細節問題可. 在簡單的報表中可以使用對象的名字或內容,然而,在復雜情況下不能使用. 例如,«Memo1» 的內容是'12'. 明細表根據這個獲取不能數據。 這就是為什么你需要主鍵。 FastReport 允許指定一個字符串, 包含任意思數據 (在這個例子中包含一個主鍵),所有對象使用«TagStr» 屬性保存這個字符串。

讓我們以 FastReportDemo.exe - 'Simple list' demo為例做說明. 這是一個公司客戶的列表, 包含有 «client’s name,» «address,» «contact perso,» etc. 數據源是«Customer.db» 表,來自數據庫DBDEMOS。 表的主鍵是«CustNo» 字段, 沒有顯示在報表上. 我們決定所有對象用這個鍵關聯到記錄, 意即用主鍵獲取數據。要執行此操作, 在Master Data Band 上的所有對象的«TagStr» 屬性,輸入以下值:

[Customers."CustNo"]

構建報表期間, «TagStr» 屬性內容以同樣的方式計算, 如同文本對象那樣計算; 意即變量的值在所有變量的位置被替換, 在這個特殊情況下的一個變量是被封閉在方括號中的。 類型將包含在«TagStr» 屬性中, 一個簡單的轉換從字符串到整數,將給我們一個主鍵的值,這樣可以找到需要的記錄。

如果主鍵是組合的 (比如包含了多個字段) ,«TagStr» 屬性內容像下面這樣:

[Table1."Field1"];[Table1."Field2"]

報表構建以后,«TagStr» 屬性包含值:'1000;1', 從中得到主鍵的值是不難的。

十一:在窗體中通過代碼訪問報表對象

FastReport報表對象(如report page, band, memo-object) 不能從你的代碼中直接訪問. 需要訪問,通«TfrxReport.FindObject» 方法找到對象:

var

Memo1: TfrxMemoView;

Memo1 := frxReport1.FindObject('Memo1') as TfrxMemoView;   //這里能用as  ?

找到之后可以訪問對象的屬性和方法。訪問page對象,通過 «TfrxReport.Pages» 屬性:

var

Page1: TfrxReportPage;

Page1 := frxReport1.Pages[1] as TfrxReportPage;

十二:在窗體中通過代碼創建報表

作為一個規則, 多數報表用設計器創建,然而,有些情況(比如報表是未知的) 需要手動創建報表,在代碼中。

手動創建報表, 應該按以下順序步驟:

- clear the report component  清除報表

- add data sources  添加數據源

- add the "Data" page  添加Data 頁

- add report’s page 添加一個或多個report 頁

- add bands on a page 在頁上添加一個或多個band

- set bands’ properties, and then connect them to the data  設置band的屬性連接到數據

- add objects on each band  在每個band上添加需要的對象

- set objects’ properties, and then connect them to the data  設置對象的屬性,連接到需要的數據字段

讓我們看看一個簡單«list»類型報表的創建,假設我們有以下組件: frxReport1: TfrxReport 和frxDBDataSet1: TfrxDBDataSet (the last one is connected to data from the DBDEMOS, the «Customer.db» table). 報表將只有一頁,包含«Report Title» 和«Master Data» bands.在«Report Title» band 有一個"Hello FastReport!" 文本, 在«Master Data» 包含"CustNo" 字段。

var

DataPage: TfrxDataPage;

Page: TfrxReportPage;

Band: TfrxBand;

DataBand: TfrxMasterData;

Memo: TfrxMemoView;

{ clear a report }

frxReport1.Clear;

{ add a dataset to the list of ones accessible for a report }

frxReport1.DataSets.Add(frxDBDataSet1);

{ add the "Data" page }

DataPage := TfrxDataPage.Create(frxReport1);

{ add a page }

Page := TfrxReportPage.Create(frxReport1);

{ create a unique name }

Page.CreateUniqueName;

{ set sizes of fields, paper and orientation by default }

Page.SetDefaults;

{ modify paper’s orientation }

Page.Orientation := poLandscape;

{ add a report title band}

Band := TfrxReportTitle.Create(Page);

Band.CreateUniqueName;

{ it is sufficient to set the «Top» coordinate and height for a band }

{ both coordinates are in pixels }

Band.Top := 0;

Band.Height := 20;

{ add an object to the report title band }

Memo := TfrxMemoView.Create(Band);

Memo.CreateUniqueName;

Memo.Text := 'Hello FastReport!';

Memo.Height := 20;

{ this object will be stretched according to band’s width }

Memo.Align := baWidth;

{ add the masterdata band }

DataBand := TfrxMasterData.Create(Page);

DataBand.CreateUniqueName;

DataBand.DataSet := frxDBDataSet1;

{ the Top coordinate should be greater than the previously added band’s top + height}

DataBand.Top := 100;

DataBand.Height := 20;

{ add an object on master data }

Memo := TfrxMemoView.Create(DataBand);

Memo.CreateUniqueName;

{ connect to data }

Memo.DataSet := frxDBDataSet1;

Memo.DataField := 'CustNo';

Memo.SetBounds(0, 0, 100, 20);

{ adjust the text to the right object’s margin }

Memo.HAlign := haRight;

{ show the report }

frxReport1.ShowReport;

 

讓我們解釋一些細節:

要在報表中使用數據源, 必須把數據源添加到DataSets,本例中調用了«frxReport1.DataSets.Add(frxDBDataSet1)» 。否則, 不能工作。

內部數據如TfrxADOTable可以插入報表的“Data”頁,這樣的數據集可以放置到“Data”頁。

Page.SetDefaults 不是必須的, 本例中使用 À4 格式,邊距為 0 mm. SetDefaults 設置了10mm 邊距,頁大小和對齊方及默認打印機。

在頁中添加帶, 你應該確保他們不互相重疊. 設置足夠的«Top» 和«Height» 坐標。 這里不能修改«Left» 和«Width» 坐標, 因為一個帶有頁寬度(在垂直帶時–你應該設置Left 和Width ,忽略 Top 和 Height)。需要注意的是,在頁面上的位置的順序是非常重要的,總是以同樣的方式定位帶,在設計器中也以同樣的方式。

對象的坐標和大小以像素為單位。由於«Left,» «Top,» «Width,» and «Height» 屬性是 «Extended» 類型,你可以設置成一個非整形值。 下面的常量被定義為將像素轉換為厘米和英寸(frxClass單元):

fr01cm = 3.77953;

fr1cm = 37.7953;

fr01in = 9.6;

fr1in = 96;

例如, 一個band的高等於 5 mm,你可以設置如下:

Band.Height := fr01cm * 5;

Band.Height := fr1cm * 0.5;

 

十三:在窗體中通過代碼創建對話式報表

十四:修改報表的屬性

十五:在代碼的幫助下創建報表

十六:打印一個數組

The primary example’s code is located in the «FastReport Demos\PrintArray» ( "FastReport Demos\BCB Demos\PrintArray") directory. 讓我們解釋幾個細節.

To print an array, we use a report with one «Master Data» band, which will be presented as many times, as there are elements in the array. To do this, place a «TfrxUserDataSet» component on the form, and then set it’s properties (it is possible to do it in a code, as shown in our example):

RangeEnd := reCount

RangeEndCount := a number of elements in an array

After that, we connect the data-band to the «TfrxUserDataSet» component. To represent the array element, place a text object with the [element] line inside the «Master Data» band. The «element» variable is filled using a «TfrxReport.OnGetValue» event.

十七:打印一個TStringList

The primary example’s code is located in the «FastReport Demos\PrintStringList» ( «FastReport Demos\BCB Demos\PrintStringList») directory. The method is the same, as in the example with an array.

十八:打印一個文件

十九:打印一個TStringGrid

二十:打印一個TTable和TQuery

二十一:報表繼承

二十二:多線程

FastReport可以在不同的線程獨立運作,但有一些特點:

- 即使在不同的線程,你不能創建TfrxDBDataSet, 因為全局列表"global list" 被用於搜索,當訪問會發生在第一次創建TfrxDBDataSet對象時(你可以關閉使用全局列表,默認情況下它是激活的);

- 如果在報表執行過程中有一些對象屬性做了變化(比如在腳本中:Memo1.Left := Memo1.Left + 10), 你需要記住在接下來的操作中,如果TfrxReport.EngineOptions.DestroyForms := False 報表模板將准備修改並需要重新加載或者使用TfrxReport.EngineOptions.DestroyForms := True。 在更新過程中,您不能在線程中使用交互報表, 因為腳本對象在更新后被刪除, 這就是為什么在某些情況下,使用TfrxReport.EngineOptions.DestroyForms:=False和更新自己的模板在下一個構建周期中。

If necessary the global list due to which you can search the needed copies of TfrxDBDataSet can be switched off.

{DestroyForms can be switched off, if every time you renew a report from a file or from a current}

FReport.EngineOptions.DestroyForms := False;

FReport.EngineOptions.SilentMode := True;

{ This property switches off the search through global list}

FReport.EngineOptions.UseGlobalDataSetList := False;

{EnabledDataSets plays local list role, you should install it before the template is loaded}

FReport.EnabledDataSets.Add(FfrxDataSet);

FReport.LoadFromFile(ReportName);

FReport.PrepareReport;

(能不用多線程盡量不用)

二十三:緩存報表

The reports and it's data can be cached both in memory ( for speed increasing ) and in file on the disk ( for saving RAM recourses). There are several types of caching in Fast Report:

- TfrxReport.EngineOptions.UseFileCache - if the property is installed in True, than the whole text and objects of built report are saved in temporary file on disk, at that TfrxReport.EngineOptions.MaxMemoSize indicates how many MB are meant for the template in RAM .

- TfrxReport.PreviewOptions.PagesInCache - the number of pages which can be kept in cache memory greatly increases preview speed , but spends much memory ( especially when there are pictures in a template).

- TfrxReport.PreviewOptions.PictureCacheInFile - if the property is on, than all the pictures of built report are saved in temporary file on a disk, that greatly reduces memory use in reports with a large amount of pictures, but it reduces the speed.

二十四:MDI窗體樣式

FastReport可以創建MDI風格的預覽和設計窗體。 The source code of the example is in FastReport Demos\MDI Designer catalogue.

值得一提的是,建議每個預覽窗口或設計窗口創建自己的TfrxReport ,否則所有的窗口都會指向同一個TfrxReport。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM