I. Using the tfrxreport component
1. Load and store reports
By default, the report form is stored in the same DFM file as the project form. In most cases, you do not have to attach a report in special ways. If you decide to store a report form or database
Blob field (it provides great elasticity and you can modify it in a non-compiled program). You must use the loading and storage methods provided by "tfrxreport.
Function loadfromfile (const filename: string; predictionifnotfound: Boolean = false): Boolean;
Load a report from a given name file. If the second parameter is "true" and the file is not found, an exception is triggered. If the file is successfully loaded, "true" is returned ".
Procedure loadfromstream (Stream: tstream );
Load a report from a stream.
Procedure savetofile (const filename: string );
Store reports with special names.
Procedure savetostream (Stream: tstream );
Store the report in a stream.
For example:
Pascal:
Frxreport1.loadfromfile ('C:/1. fr3 ');
Frxreport1.savetofile ('C:/2. fr3 ');
C ++:
Frxreport1-> loadfromfile ("C: // 1. fr3 ");
Frxreport1-> savetofile ("C: // 2. fr3 ");
2. Design reports
Call the report designer using the "tfrxreport. designreport" method. You must include the report designer in your project (required condition: either use the "tfrxdesigner" component or add the "frxdesgn" unit to uses
List)
The "desigreport" method accepts two default parameters:
Procedure designreport (modal: Boolean = true; mdichild: Boolean = false );
The modal parameter determines whether the designer is modal. The mdichild parameter allows the designer form to be used as an MDI subform.
For example:
Frxreport1.designreport;
3. Run the report
Apply one of the following two "tfrxreport" methods to start a report:
Procedure showreport (clearlastreport: Boolean = true );
Start the report and display it in the preview form. If the "clearlastreport" parameter is set to "false", a previous report result is added to the report. Otherwise, the previous report structure is cleared.
Function preparereport (clearlastreport: Boolean = true): Boolean;
Start the report. The preview form is not opened. The parameter value assignment is the same as the "showreport" method. If the report is successfully constructed, "true" is returned ".
In most cases, the first method is more convenient. When a report is constructed, a preview form is displayed immediately.
When you need to add another report to the previous report, the "clearlastreport" parameter is more convenient. (This technology is used to print reports in batches ).
For example:
Frxreport1.showreport;
4. Preview a report
It is possible to display a report in two ways: Call the "tfrxreport. showreport" method (previously described), or use the "tfrxreport. showpreparedreport" method to help implement the report. In the second
In this case, the report structure is not executed, but a completed report is displayed. That is to say, you can either construct a report with the help of the "preparedreport" method, or load the report from the file before the construction (view "load/storage
Generate Reports ")
For example:
Pascal:
If frxreport1.preparereport then
Frxreport1.showpreparedreport;
C ++:
If (frxreport1-> preparereport (true ))
Frxreport1-> showpreparedreport ();
In this case, the report constructor is completed first and displayed in the preview form. It may take a lot of time to construct a large report, that is, why the non-same-time synchronization method of "showreport" is better
"Preparereport/showpreparedreport" method. You can use the default value of "tfrxreport. previewoptions" to set the preview parameter value. (The translation is not good enough. Please refer to the original article)
Reference: in this case, report construction is finished first, and after that it is displayed in the preview window. Construction of a large report can take
A lot of time, and that is why it is better to use the "showreport anisochronous" method, than the "preparereport/showpreparedreport" One. One can assign
Preview settings by default via the "tfrxreport. previewoptions" property. [SPAN] 5. print the report
In most cases, you can print a report from the preview form. To manually print a report, you should use the "tfrxreport. Print" method, for example:
Frxreport1.loadfromfile (...);
Frxreport1.preparereport;
Frxreport1.print;
In addition, you can set the print parameters in the displayed Print dialog box, or use the default setting value. In the cancel Print dialog box, see "tfrxreport. printoptions" attribute help.
6. Load and store reports
This function can be executed in the preview window. You can also run the command manually. For more information, see tfrxreport. previewpages:
Function loadfromfile (const filename: string; predictionifnotfound: Boolean = false): Boolean;
Procedure savetofile (const filename: string );
Procedure loadfromstream (Stream: tstream );
Procedure savetostream (Stream: tstream );
Assignment and parameterization are similar to the corresponding tfrxreport method. The file contains completed reports. By default, "fp3" is used as the extension.
For example:
Pascal:
Frxreport1.previewpages. loadfromfile ('C:/1. fp3 ');
Frxreport1.showpreparedreport;
C ++:
Frxreport1-> previewpages-> loadfromfile ("C: // 1. fp3 ");
Frxreport1-> showpreparedreport ();
Note: After the completed report is loaded, the preview method is executed using the "showpreparedreport" method.
7. Export a report
It can be executed in the preview window. You can also manually export the file type you want to export through the "ffrxreport. Export" method and the parameters in this method:
Frxreport1.export (frxhtmlexport1 );
The export filter component must be valid (you must put them on the form in your project) and adjusted correctly.
The export filter component must be available (you must put it on the form of your project) and be adjusted correctly.
8. Create a custom preview form
Fastreport displays the report in the standard preview window. If it is not suitable for you for some reason, you can create a custom preview form. For this purpose, we need to design the "tfrxreport" group in the fastreport component panel.
. To display reports, the tfrxreport. Preview method should be connected to this component.
When using the tfrxpreview component, there are two typical problems. He does not process buttons (arrows, pgup, pgdown, etc.) and scroll wheel (if any ). To allow tfrxpreview to work with the button, set the focus to him (
Yes, for example, in the onshow event handle of the form)
Frxpreview. setfocus;
To make tfrxpreview work with the scroll wheel, you must create an onmousewheel event handle and call the tfrxpreview. mousewheelscroll method.
Procedure tform1.formmousewheel (Sender: tobject; shift: tshiftstate;
Wheeldelta: integer; mousepos: tpoint; var handled: Boolean );
Begin
Frxpreview1.mousewheelscroll (wheeldelta );
End;
9. Create a composite report (batch printing)
In some cases, you need to organize several reports to print immediately, or encapsulate and present several reports in a print preview form. To do this, some tools in fastreport allow creating a new report to be appended to
An existing report. The "tfrxreport. preparereport" method has a Boolean parameter of the "clearlasreport" option, which is equal to true by default. This parameter defines whether it is necessary to clear a report when it is created.
Previous Report. The following method shows how to create a batch report from two reports:
Pascal:
Frxreport1.loadfromfile ('1. fr3 ');
Frxreport1.preparereport;
Frxreport1.loadfromfile ('2. fr3 ');
Frxreport1.preparereport (false );
Frxreport1.showpreparedreport;
C ++:
Frxreport1-> loadfromfile ("1. fr3 ");
Frxreport1-> preparereport (true );
Frxreport1-> loadfromfile ("2. fr3 ");
Frxreport1-> preparereport (false );
Frxreport1-> showpreparedreport ();
We load and create the first report, but it is not displayed. Then we load the second report to the same tfrxreport object and use the "clearlastreport" parameter to make it equal to false. This allows the second report.
Attached to a previously created report. Next, we will display a completed report in the preview window.
9.1 page number in the composite report
You can use "page", "page #", "totalpages", and "totalpages #" system variables to display the number of pages or the total number of pages. In a composite report, these variables work in the following ways:
Page-current report page number
Page #-number of pages in a batch report
Totalpages-Total number of pages of the current report (the report must be twice)
Totalpages #-Total number of pages in a batch report
9.2 merge to meet the report page number
As mentioned above, the "printonprevouspage" method on the report design page allows you to join the report with the remaining space on the previous page when printing the report. In a composite report, you are allowed to be left on the last page of the previous report.
Create a new report. To execute this operation, enable the "printonpreviusepage" attribute [SPAN] on the first design page of each consecutive report. 10. Interactive report
In interactive reports, you can define any report object by clicking the mouse in the preview window. For example, a user can click a data line to run a detailed data with a Selection Line.
Any report can interact. To execute the tfrxreport. onclickobject event handle, you only need to create a tfrxreport. onclickobject event handle. The following code is an example of the event handle:
Pascal:
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;
C ++:
Void _ fastcall tform1: frxreport1clickobject (tfrxview * sender,
Tmousebutton, tshiftstate shift, bool & modified)
{
Tfrxmemoview * memo;
If (memo = dynamic_cast <tfrxmemoview *> (sender ))
{
If (memo-> name = "memo1 ")
Showmessage ("memo1 contents:/n/R" + memo-> text );
If (memo-> name = "memo2 ")
{
Memo-> text = inputbox ("edit", "Edit memo2 text:", Memo-> text );
Modified = true;
}
}
}
On the event handle of an object, you can do the following:
-Modify the content of an object or page and pass the handle (as a result, the modified content will be specially marked, so the modified content should be paid attention );
-The tfrxreport. preparereport method is called because the report is restructured or re-created.
In this example, click "memo1" to display the message of the object content. When you click memo2, a dialog box is displayed. The content of this object may be modified. Set the modified flag to true to allow persistence and display changes.
.
In the same way, click events can be defined as different responses. For example, run a new report. The following annotations are necessary. In fastreport3, a report component can display a unique report in a preview window (unlike
Fastreport2.x ). That is why a report preview window runs a report or the same object in a separate object, but the current report must be erased.
For terminal users, you can click the object prompt to locate them. In the preview window, when the mouse passes through an object that can be clicked, we can modify the mouse cursor. To do this, select an object to be designed on the report designer and set
Its cursor attribute is different from a specific value of crdefault.
More details involve the defined click object. In a simple report, the object name or its content can be defined. However, this does not mean that more examples can be modified. For example, a detailed report should be selected in a data
The row is created. A user clicks the memo1 object whose content is 12. What does the data row Reference this object? That is why you should know the primary key, which is used to clearly identify this row. Fastreport can assign a value to a package
Contains any data (in the case of primary key data). For each report object, this string is stored in the tagstr attribute.
Let's refer to the example of a report, which is included in the fastreportdemo.exe-'simple list' example. This is a list of customers in a company, including data such as customer names, addresses, and contacts. Quantity
The data source is the customer. DB table from the dbdemos demo database. This table has a primary key, the custno field, which does not appear in the report. Our task is to terminate him by referring to any object on the report to record him.
This operation adds a value to the tagstr attribute of all objects, depending on the primary data column:
[MERs. "custno"]
When a report is created, the tagstr attribute content is calculated using the same method, and the content of the text object is also calculated. This means that the variable value replaces the location of all variables. Variable details are enclosed in square brackets. That's why the row Value
It is a similar value such as '123' and '123. After a report is created, the type of the object containing the tagstr attribute depends on the primary data column. A simple conversion from string to integer will provide us with a primary key value, which is also required to record
The primary key.
If the primary key is compound (contains multiple fields), the content of the tagstr attribute may be the following values:
[Table1. "field1"]; [table1. "field2"]
After a report is constructed, the tagstr attribute contains the "1000; 1" type value, which is better than the same.
11. Access Report objects from code
Report objects (such as report page, band, and memo-object) cannot be directly accessed in your code. This means that you cannot address objects by name. For example, when you address a button on your form. Addressing 1
Objects in the tfrxreport. findobject method:
Pascal:
VaR
Memo1: tfrxmemoview;
Memo1: = frxreport1.findobject ('memo1') as tfrxmemoview;
C ++:
Tfrxmemoview * memo =
Dynamic_cast <tfrxmemoview *> (frxreport1-> findobject ("memo1 "));
Then, you can address the attributes and methods of an object. You also use the "tfrxreport. pages" attribute to address the report page.
Pascal:
VaR
Page1: tfrxreportpage;
Page1: = frxreport1.pages [1] As tfrxreportpage;
C ++:
Tfrxreportpage * page1 = dynamic_cast <tfrxreportpage *> (frxreport1-> pages [1]);
12. Create a report from the code
As a rule, you will create a majority of reports in the designer. However, in some cases (for example, when the report form is unknown), it is necessary to manually create a report using code.
To manually create a report, follow these steps:
-Clear the report component
-Add a data source
-Add data page
-Add Report page
-Add a column
-Set the column attributes and connect them to the data.
-Add objects to each column
-Set object attributes and connect them to the data
Let's check the type list for creating a simple report. Suppose we have the following components: frxreport1: tfrxreport and frxdbdataset1: tfrxdbdataset (the last one is connected to dbdemos data,
Customer. DB table ). Our report will contain a page with the report title and primary data bar. On the report title bar, there is an object with the "Hellow fastreport" text. The main data column contains an object that is connected to "custno"
Field object.
Pascal:
VaR
Datapage: tfrxdatapage;
Page: tfrxreportpage;
Band: tfrxband;
Databand: tfrxmasterdata;
Memo: tfrxmemoview;
{Clear report}
Frxreport1.clear;
{Adding a dataset to a report to an accessible list}
Frxreport1.datasets. Add (frxdbdataset1 );
{Add "data" Page}
Datapage: = tfrxdatapage. Create (frxreport1 );
{Add page}
Page: = tfrxreportpage. Create (frxreport1 );
{Create a unique name}
Page. createuniquename;
{Set the default field size, paper and printing direction}
Page. setdefaults;
{Modifying the paper direction}
Page. Orientation: = polandscape;
{Add a report title bar}
Band: = tfrxreporttitle. Create (PAGE );
Band. createuniquename;
{It is sufficient to set? Top? Coordinate and height for a band}
{BOTH coordinates are in pixels}
Band. Top: = 0;
Band. Height: = 20;
{Add an object to the title bar}
Memo: = tfrxmemoview. Create (band );
Memo. createuniquename;
Memo. Text: = 'Hello fastreport! ';
Memo. Height: = 20;
{This object will stretch the coordinates to the width of the column}
Memo. Align: = bawidth;
{Add primary data column}
Databand: = tfrxmasterdata. Create (PAGE );
Databand. createuniquename;
Databand. Dataset: = frxdbdataset1;
{The top adjustment should be larger than the top + height of the previously added column}
Databand. Top := 100;
Databand. Height: = 20;
{Add an object to the primary data bar}
Memo: = tfrxmemoview. Create (databand );
Memo. createuniquename;
{Connection data}
Memo. Dataset: = frxdbdataset1;
Memo. datafield: = 'custno ';
Memo. setbounds (0, 0,100, 20 );
{Adjust text to the object edge on the right}
Memo. halign: = haright;
{Show report}
Frxreport1.showreport;
C ++:
Tfrxdatapage * datapage;
Tfrxreportpage * page;
Tfrxband * band;
Tfrxmasterdata * databand;
Tfrxmemoview * memo;
// Clear the report
Frxreport1-> clear ();
// Add a dataset to the dataset access list on the report.
Frxreport1-> datasets-> Add (frxdbdataset1 );
// Add the "data" Page
Datapage = new tfrxdatapage (frxreport1 );
// Add a page
Page = new tfrxreportpage (frxreport1 );
// Create a non-repeated name
Page-> createuniquename ();
// Set the domain size, paper, and default printing direction
Page-> setdefaults ();
// Modify the printing direction of the paper
Page-> orientation = polandscape;
// Add a report title bar
Band = new tfrxreporttitle (PAGE );
Band-> createuniquename ();
// Set the top coordinates and height for the column
// Contain coordinates on pixels
Band-> Top = 0;
Band-> Height = 20;
// Add an object to the report title bar
Memo = new tfrxmemoview (band );
Memo-> createuniquename ();
Memo-> text = "Hello fastreport! ";
Memo-> Height = 20;
// This object will be extended by column width
Memo-> align = bawidth;
// Add the primary data column
Databand = new tfrxmasterdata (PAGE );
Databand-> createuniquename ();
Databand-> dataset = frxdbdataset1;
// The top coordinate should be greater than the top coordinate + height of the column added to the front
Databand-> Top = 100;
Databand-> Height = 20;
// Add an object to the primary data
Memo = new tfrxmemoview (databand );
Memo-> createuniquename ();
// Connect to the data
Memo-> dataset = frxdbdataset1;
Memo-> datafield = "custno ";
Memo-& gt; setbounds (0, 0,100, 20 );
// Adjust the text to the white space of the object on the right
Memo-> halign = haright;
// Display the report
Frxreport1-> showreport (true );
Let's explain some details:
All datasets used in the report must be added to the data source list. In our example, frxreport1.datasets. Add (frxdbdataset1) is used for execution. Otherwise, the report will not work.
The data page is necessary for inserting an internal dataset, such as tfrxadotable. These datasets can only be placed on the data page.
Calling page. setdefaults is not required, because in this case, both the A4 page setting and the page margin are 0mm. The default value is 10mm margins, and the printer page size and alignment are captured.
While adding columns to the page, make sure they are not overlapped. To execute it, the coordinates on the top and height are similar. Always locate the same position in the designer.
The coordinates and sizes of objects are measured in pixels. Because the left, top, width, and height attributes of all objects have extended types, you can point out non-shaping values. The following constant is used to convert pixels to centimeters and inches:
Fr 01cm = 3.77953;
Fr 1cm = 37.7953;
Fr01in = 9.6;
Fr1in = 96;
For example, if the height of a column is 5mm, the following settings are displayed:
Band. Height: = fr 01cm * 5;
Band. Height: = fr 1cm * 0.5;
12. Create dialog box in code
As we know, a report can contain a dialog box. The following example shows how to create a dialog box with the OK button:
Pascal:
{For working with dialogue objects the following unit shocould be used}
Uses frxdctrl;
VaR
Page: tfrxdialogpage;
Button: tfrxbuttoncontrol;
{Add page}
Page: = tfrxdialogpage. Create (frxreport1 );
{Create a unique name}
Page. createuniquename;
{Set size}
Page. Width: = 200;
Page. Height: = 200;
{Set location}
Page. Position: = poscreencenter;
{Add a button}
Button: = tfrxbuttoncontrol. Create (PAGE );
Button. createuniquename;
Button. Caption: = 'OK ';
Button. modalresult: = mrok;
Button. setbounds (60,140, 75, 25 );
{Show report}
Frxreport1.showreport;
C ++:
// Use the dialog box object to work and use the following units
# Include "frxdctrl. HPP"
Tfrxdialogpage * page;
Tfrxbuttoncontrol * button;
// Add a page
Page = new tfrxdialogpage (frxreport1 );
// Create a unique name
Page-> createuniquename ();
// Set the size
Page-> width = 200;
Page-> Height = 200;
// Set the location
Page-> position = poscreencenter;
// Add a button
Button = new tfrxbuttoncontrol (PAGE );
Button-> createuniquename ();
Button-> caption = "OK ";
Button-> modalresult = mrok;
Button-> setbounds (60,140, 75, 25 );
// Display the report
Frxreport1-> showreport (true );