One: Load and save reports
The report is saved in the Project form file by default, and in most cases there is no more action to be taken to Shenzhen, so you do not need to take special measures to load the report. If you decide to save the report to a file or database (which is more flexible, such as modifying the report without recompiling the program), you can use "Tfrxreport" Components are load and save methods:
function LoadFromFile (const filename:string; Exceptionifnotfound:boolean = False): Boolean;
Load the report by file name. The second parameter, if it is "True" and the report file is not found, will report an exception. Returns "True" if the report load is complete.
Procedure Loadfromstream (Stream:tstream);
Load a report from a stream
Procedure SaveToFile (const filename:string);
Save the report to the specified file
Procedure Savetostream (Stream:tstream);
Save report to stream
The report file's extension defaults to "FR3".
Example:
Frxreport1.loadfromfile (' C:\1.FR3 ');
Frxreport1.savetofile (' C:\2.FR3 ');
Second: Design report
Displays the report design form, calling the "Tfrxreport.designreport" method. You need to reference the relevant unit.
The method "Designreport" has two default parameters.
Procedure Designreport (Modal:boolean = True; Mdichild:boolean = False);
The first parameter controls whether a form is displayed in modal mode. The second parameter controls whether it is an MDI child form.
Example:
Frxreport1.designreport;
Three: Run (show) a report
Start a report using the following two methods of the "Tfrxreport" component:
Procedure Showreport (Clearlastreport:boolean = True);
Starts a report and displays the preview window. If the parameter "Clearlastreport" equals "False", the report is added after the previously created report, otherwise the previous report will be cleared (default).
function Preparereport (Clearlastreport:boolean = True): Boolean;
Start a report with no preview window. The parameters are the same as the method "Showreport". Report creation is complete and returns "True".
In most cases, use the first method more. A preview window will appear to continue building the new report.
The parameter "Clearlastreport" makes it easy to control the following situations when additional reports need to be added. (Batch printing of reports, etc.)
Example:
Frxreport1.showreport;
Four: Preview the report
The Preview window displays a report in 2 ways: one is to call the "Tfrxreport.showreport" method, and the other is to call the "Tfrxreport.showpreparedreport" method. The action of the report build in the second method does not execute, but the report has finished displaying. This means that you can build a report beforehand with the "Preparereport" method, or load a report that was previously loaded from a file.
Example:
If Frxreport1.preparereport Then
Frxreport1.showpreparedreport;
In this case, the report is first built and then displayed in the Preview form. Building a large report can take a lot of time, which is why using "Showreport" (quasi-synchronous method) is better than using "Preparereport/showpreparedreport". You can specify the preview mode, which is set by the Tfrxreport.previewoptions property.
Five: Print the report
In most cases, you print the report from the preview window. To print the report manually, you can call the "Tfrxreport.print" method, for example:
Frxreport1.loadfromfile (...);
Frxreport1.preparereport;
Frxreport1.print;
At the same time, a popup dialog box can be set for printing parameters. You can set the "Tfrxreport.printoptions" property to not show this dialog box.
Six: Load and save completed reports
The report can do this in the preview window. You can also manually execute the "Tfrxreport.previewpages" method:
function LoadFromFile (const filename:string; Exceptionifnotfound:boolean = False): Boolean;
Procedure SaveToFile (const filename:string);
Procedure Loadfromstream (Stream:tstream);
Procedure Savetostream (Stream:tstream);
The configuration and parameters are the same as the corresponding Tfrxreport methods. A file contains a completed report whose extension defaults to "FP3".
Example:
FrxReport1.PreviewPages.LoadFromFile (' c:\1.fp3 ');
Frxreport1.showpreparedreport;
Note that this completed report is loaded and its preview is through the "Showpreparedreport" Method!
Seven: Export a report
You can do this in the preview window. You can also call the "Tfrxreport.export" method manually. parameter of the method to specify the type of file to export, such as:
Frxreport1.export (FRXHTMLEXPORT1);
The export component must be available (the component is placed on the form) and set correctly.
Eight: Create a custom Preview form
Fastreport displays the report in a standard preview form. You can create a custom preview form, using the component "Tfrxpreview".
There are two typical problems when working with components. The component does not handle related keystrokes (cursors, PgUp, Pgdown etc) and mouse scrolling. Let the Tfrxpreview button work properly, set the component to the current focus (such as in the form of OnShow event write code processing), as follows:
Frxpreview.setfocus;
To have the Tfrxpreview component respond to mouse scrolling, you need to create the OnMouseWheel event handler and invoke the Tfrxpreview.mousewheelscroll method, as follows:
Procedure Tform1.formmousewheel (Sender:tobject; Shift:tshiftstate;
Wheeldelta:integer; Mousepos:tpoint; var handled:boolean);
Begin
Frxpreview1.mousewheelscroll (Wheeldelta);
End
Nine: Build a mixed-type report
In some cases, several reports are required in a single print, or multiple reports are placed in a preview window. Tools are available in Fastreport to allow new reports to be added to existing reports. ? Tfrxreport.preparereport? Method has a boolean parameter option? Clearlastreport? , the default equals? True? This parameter defines whether to purge previously built reports. The following code demonstrates loading multiple reports:
Frxreport1.loadfromfile (' 1.FR3 ');
Frxreport1.preparereport;
Frxreport1.loadfromfile (' 2.FR3 ');
Frxreport1.preparereport (False);
Frxreport1.showpreparedreport;
We load the first report and build him, but not the display. When we build a second report with the same object, what are the parameters? Clearlastreport? Equals? False?. This allows the second report to be added to the previous report, and finally the built-in report is displayed in the preview window.
A: page numbers in a mixed report
Can you use? Page,?? page#,?? TotalPages, huh? And? Totalpages#? such as system variables to display page numbers, in mixed reports, these variables have the following meanings:
page– the page number of the current report
page#-the page number of the entire batch of reports (meaning global)
totalpages– All page totals for the current report (a must is a two-pass one)
totalpages#-page totals for the entire batch of reports.
B: Composite pages in mixed reports
As mentioned above, the Printonpreviouspage property in the report Design page allows you to print to the previous page, which uses the empty space of the previous page. In a mixed report, allow you to start creating a new report from a blank in the previous report. To do this, you open the Printonpreviouspage property when you design the first page of each successive report.
Ten: Interactive reports
For interactive reports, all report objects in the Preview window define a mouse-click Response. For example, a user can click on a data row and run a new report to display detail information for the data row.
Any report can be an interactive report. To do this, you need to create a Tfrxreport.onclickobject event handler, for example:
Procedure Tform1.frxreport1clickobject (page:tfrxpage; View:tfrxview; Button:tmousebutton; Shift:tshiftstate; var modified:boolean);
Begin
If View.name = ' Memo1 ' Then
ShowMessage (' Memo1 contents: ' + #13 # # + Tfrxmemoview (View). Text);
If View.name = ' Memo2 ' Then
Begin
Tfrxmemoview (View). Text: = InputBox (' edit ', ' edit Memo2 text: ', Tfrxmemoview (View). Text);
Modified: = True;
End
End
In the event? Onclickobject? , you can also do the following things:
-Modify the contents of a page or report object (so,? Modified? The logo should be specified, so the modification will be taken into account);
Call? Tfrxreport.preparereport? method to rebuild the report.
In the example above, Memo2 modified the content, so modified: = True.
In the same way, different ways of responding can be defined; For example, you can run a new report. The following points need to be noted. In the Fastreport 3 release, a Tfrxreport component can only be displayed in one preview form (unlike the Fastreport 2.x version). This is why you want to run the report in a separate Tfrxreport object, or in the same, but the current report must be cleared.
To give the user a hint on the object that can be clicked, we can modify the cursor to set the object's Cursor property.
Clickable objects have a detail problem to be able to. You can use the name or content of an object in a simple report, however, you cannot use it in complex situations. For example? Memo1? The content is ' 12 '. The schedule gets no data based on this. That's why you need the primary key. Fastreport allows you to specify a string containing any of the meaning data (including a primary key in this example), all objects used? Tagstr? property to save the string.
Let's take FastReportDemo.exe-' Simple List ' demo as an example to illustrate. This is a list of company customers, including the client's name,?? address,?? Contact Perso,? etc. what is a data source? Customer.db? Table from the database Dbdemos. What is the primary key of the table? Custno? field that is not displayed on the report. We decide that all objects use this key to associate to the record, which means to get the data with the primary key. To do this, on all objects on the master Data Band? Tagstr? property, enter the following values:
[Customers.] Custno "]
During the build report,? Tagstr? Property content is calculated in the same way as a text object; meaning that the value of the variable is replaced in the position of all variables, and in this particular case a variable is enclosed in square brackets. Type will be included in the? Tagstr? attribute, a simple conversion from a string to an integer will give us a primary key value so that the required records can be found.
If the primary key is combined (for example, contains more than one field),? Tagstr? The property content looks like this:
[Table1.] Field1 "]; [Table1.] Field2 "]
After the report is built,? Tagstr? The property contains the value: ' 1000;1 ', which is not difficult to get the value of the primary key.
11: Accessing report objects through code in a form
Fastreport report objects, such as Reports page, band, Memo-object, cannot be accessed directly from your code. Need access, pass? Tfrxreport.findobject? method to locate the object:
Var
Memo1:tfrxmemoview;
...
Memo1: = Frxreport1.findobject (' Memo1 ') as Tfrxmemoview; Can we use as?
The properties and methods of the object can be accessed after it is found. Access the Page object, through? Tfrxreport.pages? Property:
Var
Page1:tfrxreportpage;
Page1: = frxreport1.pages[1] as tfrxreportpage;
12: Create a report in a form with code
As a rule, most reports are created with the designer, however, in some cases (such as reports that are unknown) you need to manually create the report in your code.
To create a report manually, you should follow the steps in the following order:
-Clear the Report component
-Add data sources adding datasource
-Add the "data" page for adding data pages
-Add report ' s page adds one or more report pages
-Add bands on a page adds one or more band
-Set bands ' properties, and then connect them to the data settings band
-Add objects on each band adding the desired object to each band
-Set objects ' properties, and then connect them to the data property of the object, connect to the required data fields
Let's take a look at a simple list? Type report creation, assuming we have the following components: Frxreport1:tfrxreport and Frxdbdataset1:tfrxdbdataset (the last one was connected to data From the Dbdemos, the? Customer.db? Table). The report will have only one page, including? Report Title? And? Master Data? Bands. In? Report Title? Band have a "Hello fastreport!" text, in? Master Data? Contains the "Custno" field.
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 is 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 is stretched according to band ' 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;
Let's explain some of the details:
To use a data source in a report, you must add the data source to DataSets, which in this case calls the? FrxReport1.DataSets.Add (FrxDBDataSet1)?. Otherwise, it will not work.
Internal data such as tfrxadotable can be inserted into the "data" page of the report, so that the data set can be placed on the "Data" page.
Page.setdefaults is not required, in this case the À4 format is used, and the margin is 0 mm. setdefaults set 10mm margins, page size, and the aligns and default printer.
Add bands in the page, and you should make sure they don't overlap each other. Set enough? Top? And? Height? Coordinate. Cannot be modified here? Left? And? Width? Coordinates, because one with the page width (when vertical band – you should set left and width, ignoring Top and Height). Note that the order of the positions on the page is very important and always locates the bands in the same way, and in the designer also in the same way.
The coordinates and size of the object in pixels. Because? Left,?? Top,?? Width,? and? Height? property is? Extended? Type, you can set it to a non-shaping value. The following constants are defined as converting pixels to centimeters and inches (frxclass cells):
fr01cm = 3.77953;
fr1cm = 37.7953;
fr01in = 9.6;
fr1in = 96;
For example, a band higher than 5 mm, you can set the following:
Band.height: = fr01cm * 5;
Band.height: = fr1cm * 0.5;
13: Create a dialog report in a form from code
14: Modify the properties of a report
XV: Create a report with the help of your code
16: Print an array
The primary example ' s code is located in the? Fastreport Demos\printarray? ("Fastreport DEMOS\BCB demos\printarray") directory. Let's explain a few details.
To the print an array, the We use a report with one? Master Data? Band, which'll be presented as many times, as there is elements in the array. To does this, place a? Tfrxuserdataset? Component on the form, and then set it's properties (it's possible to does it in a code, as shown with our example):
RangeEnd: = Recount
Rangeendcount: = a number of elements in an array
After this, 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.
17: Print a 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.
18: Print a file
19: Print a Tstringgrid
20: Print a ttable and tquery
21: report Inheritance
22: Multithreading
Fastreport can operate independently on different threads, but there are some features:
-even on different threads, you cannot create tfrxdbdataset, because the global list is used for search, when access occurs the first time the Tfrxdbdataset object is created (you can turn off the use of global list, by default it is active);
-If there are some object properties that have changed during the execution of the report (such as in the script: memo1.left: = Memo1.left + 10), you need to remember that in the next operation, if the TfrxReport.EngineOptions.DestroyForms : = False Report template will be ready to be modified and need to be reloaded or use TfrxReport.EngineOptions.DestroyForms: = True. During the update process, you cannot use interactive reports in threads because the script objects are deleted after the update, which is why, in some cases, using the tfrxreport.engineoptions.destroyforms:= False and update your own templates in the next build cycle.
If necessary the global list due to which you can search the needed copies of Tfrxdbdataset can switched off.
{Destroyforms can is switched off, if every time 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;
(Can not use multi-threading as far as possible)
23: Caching Reports
The reports and it's data can is cached both in memory (for speed increasing) and in file on the disk (for saving RAM R ecourses). There is several types of caching in Fast report:
- TfrxReport.EngineOptions.UseFileCache -If the property was installed in True, than the whole text and objects O F built report is saved in temporary file on disk, at the TfrxReport.EngineOptions.MaxMemoSize indicates how many MB is Meant for the template in RAM.
- TfrxReport.PreviewOptions.PagesInCache -the number of pages which can be kept in cache memory greatly increase S preview speed, but spends much memory (especially when there is pictures in a template).
- TfrxReport.PreviewOptions.PictureCacheInFile -If the property was on, than all the pictures of built Saved in temporary file on a disk, which greatly reduces memory use in reports with a large amount of pictures, but it red UCEs the speed.
24: MDI Form style
Fastreport can create MDI-style previews and design forms. The source code of the example is in Fastreport demos\mdi Designer catalogue.
It is worth mentioning that each preview window or design window is recommended to create its own tfrxreport, otherwise all windows will point to the same tfrxreport.
Translation Fastreport Tfrxreport Components use