Using Delphi to implement horizontal grouping and printing of reports

Source: Internet
Author: User

The built-in Quickreport System of Delphi provides powerful support for quick report creation. For some simple reports, including vertical grouping and multi-header reports, we can easily create reports without writing a line of code. However, for reports with horizontal grouping shown in figure 1, we also need to write some code to control the report printing format. This article uses the report shown in Figure 1 as an example to introduce the methods and techniques for creating horizontal grouping reports.

First, add a report form qrform1, add the report control quickrep1 on the form, and set its bands attribute to include attributes such as title, header, table details, and table footer. Add the adoconnection1 Connection Control and two adodataset controls on the report form, named adodatasetshqk and adodataset1. Adoconnection1 is connected to the database, and adodatasetshqk is bound to the data table to be displayed and printed. A permanent field must be added to the dataset.
Then, use qrshape to sketch the report header and details. The shape attribute of the horizontal line is set to qrshorline and Height = 1. The shape attribute of the vertical line is set to qrsvertline and width = 1. The vertical line separates fields and generates vertical table lines. The key question here is: how to generate a horizontal grouping of report records? For the purpose of code control, we place the horizontal qrshape100 above the "Serial Number" and "road name" When sketching the details of the report, place the horizontal qrshape200 above the "damage condition" "damage time" "area" and place the horizontal qrshape300 below the entire detailband. In detailband, place qrlabel at "no." and "road name", named qrlabelxh and qrlabeldlmc respectively, and place qrdbtext at "damage condition", "damage time", and "area, corresponding fields.
The principle of horizontal grouping is to judge in the beforeprint event of the report detailband. If the recorded "no." and "road name" are the same as the previous record, do not print qrshape100 (make its width = 0), otherwise print. If the report is printed to the end of the page or all records are printed, the qrshape300 is printed. Otherwise, the qrshape300 width is 0, that is, no printing is performed.
In the design phase, the control attributes are set to quickrep1.dataset = adodatasetrptshqk, adodatasetshqk and adodataset1 connection = adoconnection1, And the commandtext attribute of adodatasetshqk requires the SQL statement select... From ...; The active attribute of adodataset1 is false.
Finally, add the code qrform1.quickrep1. Preview; and qrform1.quickrep1. Print to preview or print the report directly.
Unit report1;
Interface
Uses ......
Type ......
VaR
Qrform1: tqrform1;
Oldstr: string;
Linesperpage: integer; // number of records printed on each page
Lineperitem: integer; // The total number of records in each horizontal Group
Linestotal: integer; // total number of printed records
Linedisplayitem: integer; // This row displays the public information of the horizontal group.
Adodataset1count: integer; // number of records for each horizontal Group
ID: integer; // the serial number of the horizontal group.
Implementation
{$ R *. DFM}
// Custom function: determines whether to print to the footer of the page
Function tqrform1.ispagefoot (): Boolean;
Begin
Result: = false;
Linesperpage: = linesperpage + 1;
{Manually determine the number of records per page based on the Report preview results,
33 records on page 1 and 36 records on other pages of this program}
If quickrep1.pagenumber = 1 then
Begin
If linesperpage = 33 then result: = true;
End
Else
Begin
If linesperpage = 36 then result: = true;
End;
End;

// Custom function: determines whether all records are printed
Function tqrform1.islastrecord (): Boolean;
Begin
Adodatasetrptshqk. Next;
If adodatasetrptshqk. EOF then
Result: = true
Else
Begin
Adodatasetrptshqk. Prior;
Result: = false;
End;
End;

// Initialization variable:
Procedure tqrform1.quickrep1beforeprint (Sender: tcustomquickrep;
VaR printreport: Boolean );
Begin
Oldstr: = '';
Linesperpage: = 0;
Lineperitem: = 0;
Linestotal: = 0;
Linedisplayitem: = 0;
ID: = 0;
Adodataset1count: = 0;
End;

/// Report writing details-horizontal grouping Control Based on conditions:
Procedure tqrform1.detailband1beforeprint (Sender: tqrcustomband;
VaR printband: Boolean );
VaR
Newstr: string;
Begin
Linestotal: = linestotal + 1;
Newstr: = trim (adodatasetrptshqk. fieldbyname ('dlmc '). asstring );
If newstr = oldstr then
Qrshape100.width: = 0
Else
Begin
Adodataset1.commandtext: = 'select count (dlmc) as count1 from rpt_shqk where dlmc = '+ ''' + newstr + '''';
Adodataset1.active: = true;
Adodataset1count: = adodataset1.fieldbyname ('count1'). asinteger;
Adodataset1.active: = false;
Lineperitem: = lineperitem + adodataset1count;
Linedisplayitem: = lineperitem-adodataset1count Div 2;
Qrshape100.width := 285;
Oldstr: = trim (newstr );
ID: = ID + 1;
End;
If linestotal = linedisplayitem then
Begin
Qrlabelxh. Caption: = inttostr (ID );
Qrlabeldlmc. Caption: = trim (adodatasetrptshqk. fieldbyname ('dlmc '). asstring );
End
Else
Begin
Qrlabelxh. Caption: = '';
Qrlabeldlmc. Caption: = '';
End;
// Draw the bottom line based on the conditions:
Qrshape300.width: = 0;
// 1. If one page is output, draw the bottom line:
If ispagefoot () then
Begin
Qrshape300.width: = 697;
Linesperpage: = 0;
End;
// 2. If the record output is complete, draw the bottom line:
If islastrecord () Then qrshape300.width := 697;
End;

Procedure tqrform1.formclosequery (Sender: tobject; var canclose: Boolean );
Begin
Quickrep1.free;
End;

Procedure tqrform1.quickrep1startpage (Sender: tcustomquickrep );
Begin
Adodatasetrptshqk. Active: = true;
End;
End.
Zhang Qing Email: zhangking@263.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.