How to quickly and automatically generate a custom report author: ----------, as reproduced please ensure the integrity of this document, and to indicate the source. Welcome to C Builder Research, http://www.ccrun.com/doc/go.asp? Id = 29 In various management information system applications, a large number of reports are required, and the usual practice is manually by the programmer one by one. Production, low work efficiency; in addition, the user wants to generate a report in the application in the application to print. In order to solve the above two problems, this paper uses dynamic generation technology to realize fast automatically generating reports, allowing users to modify the reports manually, and package the implementation process into one class. 1. Design ideas use C Builder or Delphi programmer knows that there is a TDBGRID control, which can display and operate data records in the form of a table; to make a report for printing, you need to use the TQUickRep control, It adds to TQRLABEL, TQRDBTEXT, TQRSHAPE and other controls, set their corresponding data sets, data fields, etc., then arrange their position, displayed for preview and printing in a table or other format, which is a cumbersome process. Sometimes, the user hopes that the query is printed in the data of the TDBGRID control. According to the previous practice, it is necessary to manually design the report by the programmer according to TDBGRID. Here, this paper uses dynamic generation techniques to read the display information about TDBGRID, dynamically generate corresponding TQRLABEL, TQRDBTEXT, TQRSHAPE, etc., set the title and data of each field, and the table split strip in the TQUickRep control. This is completely feasible, because all controls in C Builder can be dynamically generated by the program, not only in the design phase. In addition, if the user is not very satisfied with the generated report table layout, this paper provides an interface user to handle the report, adjust the height, width of the table, and the customization of the user's to a certain degree of customization. Using C encapsulation features, implementation of automatically generating and customizing reports into a new class TGRIDPRINT, providing a public interface for programmers carefully, blocking internal information and concrete implementation, reflecting object-oriented design ideas, for programmer Bring convenience. Programmaker can also further expand its function on it. This new category of this design reduces the workload of programmers and provides users with the interface of custom reports, and improve the quality of the report and the enthusiasm of users. 2. Implementation processes automatically generate and customize reports include automatic generation and customization. The definition and implementation of the new class is in the GridPrint.h gridprint.cpp file, including 3 file reprst.h, reprst.cpp, reprst.dfm, which is a window frmreprst, which has added a TQUickRep in it Controls, its property BANDS's values of each property of True. 2.1 Automatic Generation Report First Defines a structure that represents a list of information in the table, in which a fixed title and displayed data text are available, in order to draw a table, each column fixed title bar and the right side of the data bar add a separate separation. Strip.
In the constructor of class TGRIDPRINT, first according to the incoming tquickrep * psrcquickrep (report pointer), TDBGRID * PSRCDBGRID (data table pointer), tqrband * SrctitleBand1 (the total title bar pointer in the report), TQRBAND * SRCCOLUMNHEADERBAND1 (report in the report Field Title Bar Pointer), TQRBAND * SRCDETAILBAND1 (data bar pointer in the report) parameter sets the private variable of the class. Rehably generate and set the total title text, field title bar rectangle, and the properties of the data bar rectangle box. Then via a loop, read the title and data information of each field in the TDBGRID, dynamically generate the title tag control, data text control, and the corresponding table split vertical stripe control in the report in the report. In the destructor of the class, remove all objects that are dynamically generated by constructor. The print preview function of the class implements the print preview feature of the report. Other functions are omitted. Automatically generate a definition of report classes (gridprint.h) # include // The relevant header file included
#include
#include
#include
#include
TypeDef struct tagfieldtype {// indicates the structure of a list of tables
Ansistring Stitle; // Field Title Name
INT IWIDTH; / / Table of Table Unit
Tqrlabel * plabel; // field title control
TQRDBText * pdbtext; // Displayed data control
TQRSHAPE * PSHAPETITETITLE, * PSHAPEDATA; / / Form Subtile of Title and Data
NEWFIELDTYPE;
Class Tgridprint {
PUBLIC:
TGridPrint (Tquickrep * psrcquickrep, tdbgrid * psrcdbgrid, tqrband * srctitleband1,
TQRBAND * SRCCOLUMNHEADERBAND1, TQRBAND * SRCDETAILBAND1); // Constructor
~ TGRIDPRINT (); // Destructor
Void dopreview (); // Print preview for report
Void setPRNTTILE (ANSISSTRING Stute); / / Handmade Set Total Total Total
Void setColumnswidth (int * columnswidth); / / Handmade sets of width
Void setHeadRecTheight (int imeht); / / Handmade the height of the topic
Void setDetailRectheight (int imeht); / / manually set the height of the data line
Private:
TDBGRID * pdbgrid; // will display DBGRID
TDataSet * pDataSet; // DBGRID corresponds to data set
TQUickRep * pquickrep; // Report control
TQRBAND * titleBAND1; / / Total title bar
TQRBAND * columnheaderband1; // Report field title bar
TQRBAND * DETAILBAND1; // Report data bar
Tqrlabel * ptitlelabel; // total title control
TQRSHAPE * PHEADRECT, * PDETAILRECT; / / The entire field title bar, table rectangular control for the data bar
INT iHEADRECTHEIGHT, IDETAILRECTHEIGHT; / / The height of the table rectangle frame, they have the same width
INT _ITOTALWIDTH; / / total width of the entire table
INT _IINTCLEARANCE; / / Table internal data column to the distance of the left table
INT _IFIELDCOUNT; / / The number of fields will be printed
NewfieldType_ArrayfieldType [40]; // Support to 40 field prints
void autoadjustColumnswidth (); // Program automatically adjusts the width of each column
}
GridPrint.cpp
TGRIDPRINT :: TGRIDPRINT (TQUickRep * psrcquickrep, tqrband * psrcdbgrid, tqrband * srctitleband1, tqrband * srccolumnheaderband1, tqrband * srcdetailband1) // Constructor
{INT I, PRELEFT;
PquickRep = psrcquickrep; // Set private variables according to the incoming parameter
PDBGRID = PSRCDBGRID;
PDataSet = PDBGRID-> Datasource-> DataSet;
Pquickrep-> dataset = pdatanet;
TitleBand1 = SRCTITLEBAND1;
ColumnHeaderBand1 = srccolumnheaderband1;
Detailband1 = srcdetailband1;
Ptitlelabel = New Tqrlabel (PquickRep); // Generate and set attributes for the total title tag
Ptitlelabel-> Parent = TitleBand1;
Ptitlelabel-> CAPTION = "Report Title";
Ptitlelabel-> Left = (TitleBand1-> Width - Ptitlelabel-> Width) / 2;
MEMSET (_ArrayfieldType, 0, sizeof (newfieldtype) * 40);
_ITOTALWIDTH = 0; // calculate the total width of the unit width of the table and the total width of the entire table
For (i = 0; i
{_ArrayfieldType [i] .iWidth = pdbgrid-> columns-> items [i] -> width;
_ITotalWidth = _ArrayfieldType [i] .iwidth;
IF (_TotalWidth> TitleBand1-> width)
{// If the original DBGIRD is width and greater than the total title bar width, adjust each column width
_ITOTALWIDTH = TitleBand1-> width;
AutoAdjustColumnwidth ();
PRELEFT = (TitleBand1-> Width - __itotalwidth) / 2; // Make the entire table
PHEADRECT = New TQRSHAPE (PSRCQUickRep); / / Generate and set the rectangular box of field title bar
PHEADRECT-> PARENT = ColumnHeaderBand1;
PHEADRECT-> LEFT = prelight; pheadRect-> TOP = 0;
PHEADRECT-> width = _TotalWidth; PHEADRECT-> Height = PHEADRECT-> Parent-> Height;
PdetailRect = new tqrshape (psrcquickrep); // Generate a rectangular box for setting up data rows
PDETAILRECT-> PARENT = Detailband1;
PDETAILRECT-> LEFT = prelight; pdetailRect-> TOP = -1;
PdetailRect-> width = _TotalWidth; pdetailRect-> height = pdetailRect-> parent-> height 1; _iintclearance = 1;
_ifiELDCOUNT = pdbgrid-> fieldcount; // Set the total number of tables in the table
For (i = 0; i <_ifiELDCOUNT; I ) // dynamically generate various fields
{// Secure title bar for this field
_ArrayfieldType [i] .plabel = new tqrlabel (pquickrep); // Title control
_ArrayfieldType [i] .plabel-> parent = columnheaderband1;
// field title name
_ArrayfieldType [i] .plabel-> caption = pdbgrid-> columns-> items [i] -> title-> caption;
// Font Title of Title
_ArrayfieldType [i] .plabel-> font = pdbgrid-> columns-> items [i] -> title-> font;
_ArrayfieldType [i] .plabel-> alignment =
PDBGRID-> Column-> items [i] -> title-> alignment; // alignment
_ArrayfieldType [i] .plabel-> left = prelight _iintclearance;
_ArrayfieldType [i] .plabel-> width = _ArrayfieldType [i] .iwidth-2 * _iintclearance;
_ArrayfieldType [i] .plabel-> height = _ArrayfieldType [i] .plabel-> font-> height;
_ArrayfieldType [i] .plabel-> TOP =
PHEADRECT-> TOP (PhetRect-> Height _ArrayfieldType [i] .PLABEL-> HEIGHT / 2;
_ArrayfieldType [i] .pshapeTitle = new tqrshape (pquickrep); // Separate vertical strip on the right side of this field
_ArrayfieldType [i] .pshapeTitle-> parent = columnheaderband1;
_ArrayfieldType [i] .pshapeTitle-> Left = prelight _ArrayFieldType [i] .iwidth;
_ArrayfieldType [i] .pshapeTitle-> TOP = 0;
_ArrayfieldType [i] .pshapeTitle-> width = 1;
IF (i == pdbgrid-> fieldcount-1) // The separation vertical strip width of the last column is 0
_ArrayfieldType [i] .pshapeTitle-> width = 0;
_ArrayfieldType [i] .pshapeTitle-> Height = _ArrayfieldType [i] .pshapetitle-> parent-> height;
// Displayed data column
_ArrayfieldType [i] .pdbtext = new tqrdbtext (pquickrep); // The text control corresponding to this field
_ArrayfieldType [i] .pdbtext-> parent = detailband1; _ArrayfieldType [i] .pdbtext-> dataset = pdataset; // data set
_ArrayfieldType [i] .pdbtext-> datafield = pdbgrid-> columns-> items [i] -> fieldname; // field name
_ArrayfieldType [i] .pdbtext-> font = pdbgrid-> columns-> items [i] -> font; // font
_ArrayfieldType [i] .pdbtext-> alignment = pdbgrid-> columns-> items [i] -> alignment; // alignment
_ArrayfieldType [i] .pdbtext-> left = prelight _iintclearance;
_ArrayfieldType [i] .pdbtext-> width = _ArrayfieldType [i] .iwidth-2 * _iintclearance;
_ArrayfieldType [i] .pdbtext-> height = _ArrayfieldType [i] .pdbtext-> font-> height;
_ArrayfieldType [i] .pdbtext-> TOP =
PdetailRect-> top (pdetailRect-> Height _ArrayfieldType [i] .pdbtext-> height) / 2;
_ArrayfieldType [i] .pshapedata = new tqrshape (pquickrep); // Separation vertical strip on the right side of the data
_ArrayfieldType [i] .pshapedata-> parent = detailband1;
_ArrayfieldType [i] .pshapedata-> left = prelight _ArrayfieldType [i] .iwidth;
_ArrayfieldType [i] .pshapedata-> top = 0;
_ArrayfieldType [i] .pshapedata-> width = 1;
IF (i == pdbgrid-> fieldcount-1) // The separation vertical strip width of the last column is 0
_ArrayfieldType [i] .pshapedata-> width = 0;
_ArrayfieldType [i] .pshapedata-> height = _ArrayfieldType [i] .pshapedata-> parent-> height;
PRELEFT = _ArrayFieldType [i] .pshapeTitle-> LEFT; // The left position of the following fields
}
}
TGRIDPRINT :: ~ TGRIDPRINT () // Destructor
{delete ptitlelabel; delete pheadreat; delete pdetailRect;
For (int i = 0; i
{Delete_ArrayfieldType [i] .pdbtext; delete_ArrayfieldType [i] .pshapedata;
Delete_ArrayfieldType [i] .plabel; delete_ArrayfieldType [i] .pshapetitle;
}
Void TgridPrint :: DOPREVIEW () // Print preview function
{PquickRep-> preview ();} 2.2 custom report
The common interface of the class provides 4 functions setPRNTTITLE, SETCOLUMNSWIDTH, SETHEADRECTHEIGHT, SETDETAILRECTHEIGHT, respectively, the total title of the manual setup table, the height of each column width, field header row, and the height of the data line. Programmaker can design the relevant interface to make further amendments to the layout of the report to meet the requirements of the user, and improve the quality of the report and the enthusiasm of users. In fact, do this to transfer programmers to users in the report design phase to users, avoid problems that cannot be changed.
2.3 call method
After completing the design of TGRIDPRINT, programmers can easily call it in a needed place. Pay attention to the two header files before calling. When calling, you need to change the DBGRID1 in the following constructor to replace the name of the TDBGRID control on the program interface, and other non-change.
#include "gridprint.h"
#include "reprst.h"
TFRMREPRST * PFRMREPRST = New TFRMREPRST (NULL);
TGRIDPRINT * PGRIDPRINT = New TGRIDPRINT (Pfrmreprst-> QuickRep1, DBGRID1, PFRMREPREP1, DBGRID1, PFRMREPRST-> TitleBand1, PfRmreprst-> ColumnHeaderBand1, PfRmrePRST-> DetailBand1);
PGRIDPRINT-> DOPREVIEW ();
Delete pgridprint; delete pfrmreprst;
3. Conclude
On the basis of the former design, based on the needs of the task, the object-oriented design method can be designed, and new classes that achieve the target can be designed and can be inherited and expanded. This article only designs for data sheet TDBGRID controls, and the role of throwing bricks can perform the design of the report on other formats on the basis of the above ideas.