Universal Crystal Report Template Create an instance

xiaoxiao2021-03-06  101

Universal Crystal Report Template Create an instance

Background

When there is a large number of reports that you need to achieve its printing and export through crystal reports, you have to create a corresponding template (RPT file) for these reports (RPT files), in fact, for each programmer I have an idea in my heart: Can I achieve the functions needed by these reports through a general report template? Let's start from this idea to solve this annoying problem.

2. Methodical ideas

Take a closer study of the file structure of the crystal report template RPT, mainly the following four aspects:

First, data source settings

Second, the data field binding

Third, special field application

Fourth, text object settings

The setting of the data source needs to be bound by binding a DataSet file. For different reports, there are different report data structures, the key to the problem is to unify a DataSet file with the same structure, which uses the unique data source of the crystal report.

After having a unified data source structure, there are two main points we need to do:

1. When controlling report data Introduction, we can convert the table structure of the incoming DataSet dataset into the same structure as the DataSet file (such as the conversion of the table name, field name, and number of fields);

Second, the report structure is constructed in the crystal report template to use unbinding fields and text.

3. Create steps

Through the above ideas, we roughly know that the implementation of the unified report template needs to do, the following specific operations:

First, ready to work

We all know that everything must be lost, mainly to be more and lose, and reach our ideal goal, when we unify the report template, we must lose the flexible display characteristics of each report. So starting, we have to set a maximum number of fields and actual display for this unified report template, which must be fixed, here we can set the maximum field maxColumnnum = 15, the actual display is Showcolumnnnu = 11.

Note: The maximum number of fields is greater than the actual display is prepared for future report expansion upgrades.

Second, create a data set file (rptdataSet.xsd)

Create a table name with a table1 table, there are 15 fields (corresponding to the maximum field above), the field name is rptfield1, rptfield2, rptfield3 ... rptfield15, all fields are set to String.

Third, create a crystal report template file (rPTMODEL.RPT)

Set the data source in the RPTDataSet in the Crystal Report Template to create the table1 in RPTDataSet.

(1) Insert a large text object in the header, name RPTTITLE, is to display the report title, then insert 11 text objects below, name RTPTITLESTR1, RTPTITLESTR2, RTPTITLESTR3 ... RTPTITLESTR11, the role is to show reports Column header.

(2) Insert 11 unbinded fields in detail, named unboundstring1, unboundstring1, unboundstring3 ... unboundstring11, respectively, and the role is to display report data.

Right-click on UNBOUNDSTRING1 and then edit the formula as follows:

WhileReadingRecords;

Stringvar x1: = {Table1.rptfield1};

Other unbinding fields can be bound to this format to bind other table fields.

Fourth, create a report display file (RPTView.aspx)

Add the CrystalReportViewer1 in the ASPX file.

Double-click the page to open the corresponding RPTView.aspx.cs program file, then add the code:

// Instantiate the BillSreportTest object RPTDOCSDCenter.report.billSreportTest RPTDOC = New SDCenter.Report.billSreportTest ();

/ / Change the field name of the DSTMP (report data set) table structure, named RPTFIELD 1 (2, 3 ....)

For (int i = 0; i

{

DSTMP.TABLES [0] .Columns [i] .Columnname = "rptfield" (i 1);

}

/ / Add a missing field to DSTMP in the maximum field number 15

INT columnsadd = 15-convert.toinT32 (dstmp.tables [0] .columns.count);

For (int K = 0; K

{

DSTMP.TABLES [0] .COLUMns.Add ("Rptfield" (15-K));

}

// rPTDoc Binding data source

RPTDoc.SetDataSource (DSTMP);

// Create the text object RPTTITLE in the report template and assign it

TextObject rpttitletext = rptdoc.reportdefinition.reportObjects ["rpttitle"] as textObject;

RPTTITLETEXT.TEXT = "Report Title";

/ / Initialization definition of the report template format

String rpttitlestr = session ["rpttitlestr"]. TOSTRING ();

String [] rpttitleArray = rpttitlestr.split (',');

String rptshowtitletmp = session ["rptshowtitle"]. TOSTRING ();

INT j = 0;

For (int i = 0; i

{

IF (Convert.Toint32 (rptshowtitletmp.substring (i, 1))> 0)

J = 1;

}

IF (j == 0)

J = 1;

INT rptwidth = 16410, rPTLEFTBEGIN = 120;

INT columnSshow = j;

INT columnsnoshow = 11-j;

INT columnswidth = convert.toint32 (rptwidth / j);

For (int h = 0; h <11; h )

{

/ / In the case where the maximum column is not 1111 in the report template, there is no associated column and its corresponding column title.

IF (Convert.Toint32 (rptshowtitletmp.substring (h, 1)) == 0)

{

TEXTOBJECT RTPTITLESTRTEXT = RPTDOC.ReportDefinition.reportObjects ["RTPTITLESTR" (H 1)] AS textObject;

RTPTITLESTRTEXT.ObjectFormat.enablesuppress = true;

FieldObject rptdataastrtext = rptdoc.reportdefinition.reportObjects ["unboundstring" (h 1)] as fieldObject; rptdataastrtext.ObjectFormat.enablesuppress = true;

}

Else

{

// Average report template display column width

Try

{

TEXTOBJECT RTPTITLESTRTEXT = RPTDOC.ReportDefinition.reportObjects ["RTPTITLESTR" (H 1)] AS textObject;

RTPTITLESTRTEXT.WIDTH = ColumnWidth;

RTPTITLESTRTEXT.LEFT = RPTLEFTBEGIN;

RtptitleStext.text = rpttitaArray [h];

FieldObject rptdatathastrtext = rptdoc.reportdefinition.reportObjects ["unboundstring" (h 1)] as FieldObject;

RPTDataStext.width = columnswidth;

RPTDataStrtext.LPTD = RPTLEFTBEGIN;

RPTLEFTBEGIN = ColumnSwidth;

}

Catch (System.exception E1)

{

}

}

}

// CrystalReportViewer1 Binding data source

CrystalReportViewer1.Reportsource = RPTDOC;

CrystalReportViewer1.DATABIND ();

4. Insufficient and optimization

First, there is a limit to the maximum number of fields and field display

Second, the display width of the report column can be controlled by transmitting the width size parameters.

(Need to be added and improved) ......

转载请注明原文地址:https://www.9cbs.com/read-126858.html

New Post(0)