Crystal Reports is an efficient report technology with powerful content creation and integration functions. It is a report tool developed by a third party. Using Crystal Reports, we can better present data to users. In the past, data in the dataset can be processed programmatically and then output and displayed in Windows or web applications. However, if you want to do more work than basic formatting, such: the sum, average, multi-pole summary, and Icon creation are complex, and the crystal report just makes up for the shortcomings.
We can use Crystal Reports in both Windows applications and Web applications. However, Crystal Reports in Windows applications are more powerful than those in Web applications, you can easily print and export data. Here we will only discuss some simple use of the crystal report in Asp.net.
Using Crystal Report experts in Asp.net, you can quickly create reports. Although Crystal Reports are developed by a third party, in vs2003, Crystal Reports have become an integral part of the development environment. Let's take a very simple example. Create a web application, add the button control under the web form in the toolbox on the page, the crystalreportviewer control (this is the crystal report control) and the file field control under HTML. The file field control allows us to select a local file. to display a crystal report, we need a crystal report file with the suffix Rpt. You can add a new project to this file. Now, if you already have a crystal report file, you can view it in the following steps.
1. Double-click the page to enter the code environment. Add the following code in the page_load event:
If (session ["FILENAME"]! = NULL)
Crystalreportviewer1.reportsource = session ["FILENAME"]. tostring ();
This code mainly stores the file name in the session and judges it in the pageload event to avoid errors when refreshing the page.
2. Add the following code to the Click Event of the button:
String strname = file1.postedfile. filename;
If (strname. Trim ()! = "")
Crystalreportviewer1.reportsource = strname;
Session ["FILENAME"] = strname;
Press F5 and select an rpt file. click the button to view the report result displayed on the page.
The establishment of the crystal report file is important throughout the process. The creation of the crystal report includes two modes: pull and push. The PULL mode directly specifies the database driver and then assembles the data, in push mode, you need to write your own code to connect to the database and upload them to the report. The Push mode is more flexible than pull.
For space reasons, let's talk about the PULL mode:
Add an rpt file to the page. A dialog box is displayed. Select blank report and click OK.
In "field resource manager" on the right, right-click "database Field" and choose "add or delete database". The database expert dialog box is displayed, and select oledb (ADO) from the available data source) create a database connection according to the wizard.
After a database connection is established, you can add the tables in the database to the report, and then drag the summary of the fields to be displayed to the details in the rpt file, and the field name is automatically added to the header, this name can be changed according to your preferences.
This is a simple report. Of course, the report function is far more than that. You should write the code below.
1. First import the namespace
Using crystaldecisions. crystalreports. engine;
Using crystaldecisions. shared;
2. After the namespace is imported, it is necessary to declare an object of the reportdocument class.
3. Add the following code to the pageload event.
Reportdoc = new reportdocument ();
Reportdoc. Load (server. mappath ("craystalreport. rpt "));
Crystalreportviewer1.reportsource = reportdoc;
You can see the result by running F5. This is just a simple example. To learn more about the powerful functions of the crystal report, you need to keep learning. In vs2003, the RPT interface is very silly, you only need to right-click the page to insert a lot of things, such as slices, charts, and summaries. It's time to try again later.
I wrote it here. Continue learning .........