Tip: In some sectors, the pictures in the article can not see, it is recommended to browse my blog: http://blog.9cbs.net/jemlee2002/ The article will involve 3 aspects:
Part 1: Use the IREPORT to make a detailed process (in the Windows environment)
Part II: Develop fat client report applications using JasperReport as a report control
Part III: Develop Web Report Applications using JasperReport as a report control
1 Introduction
You can search for many articles that use Ireport and JasperReport to achieve various report tasks, but I think there is very little (almost no) to make a more detailed introduction how to use the whole process of generating reports using Ireport, I have seen The basic idea of the article is a wide coverage. Many of the content is mentioned. It is not the development personnel to have time to realize each practice. If there is more detailed information, it is not a pleasure. For this thought, I hope that I can help those friends who use Ireport and JasperReport friends, especially those who need to realize reports, hoping to bring you a little help. This article is not introduced for every detail of Ireport, and each detail of Ireport can refer to the information of the IREPORT website, but it may cost some fees. General users don't have to pay these fees, because we care about how we need reports, rather than expanding Ireport, such as making Ireport plugins.
2, prepare
2.1, download JDK
Address: http://www.sun.com, select 1.3 or higher version (recommended 1.4.2 or higher version), install JDK, the default installation can be installed; if your system has installed JDK or JRE 省略 省One step, verify that the JDK or whether JRE can run by default, in the command line (CMD), 打:: 打 如果 如果: USAGE: Java ............................................................................................................................. . Otherwise, configuration must be configured, the configuration information is as follows, in Windows environment variable settings:
Path: Add "; Java installation directory" at the end
Java_Home: "Java installation directory"
ClassPath: "Java installation directory / bin" Re-authentication JDK or if the JRE can run in any location of CMD
2.2, download IREPORT
Address: http://ireport.sourceforge.net/, select 0.3.2 version (Recommended version before publishing) Unzip IREPORT in any directory, unzipped files with an Ireport.bat, by double-click, over 30 Seconds If you can pop up an IREPORT's main form indicates that your system can run IREPORT, if you cannot pop up the main form, it is generally the first step error, or not completed.
2.3, prepare the database
Ireport supports most of the database as long as the database can provide a JDBC drive. This article provides a MySQL database as an example, but the article will finally refer to how to configure Oracle support. For the installation and establishment of the database, please refer to other information. This article assumes that mysql has been installed and there are some tables already in MySQL, and the data is already available in the table.
* [Special Tips] MySQL version requires the use of the MySQL driver with the lib directory under the Ireport folder, the author recommends to http://dev.mysql.com/downloads/ download the latest version of MySQL drive, so it will not It should be a JDBC drive to support the database support. 2.4, start mysql service
Determine the character set used by mysql is an important issue, especially for friends who need Chinese reports, should pay special attention to this problem.
2.5, determine business logic
That is, what kind of report tasks are hoped to do. A detailed description of the report that needs to be implemented, this is a business condition that implements the report, otherwise the most of the tasks are meaningless. This article uses a bug record table as an example, and the example of this article is a BUG quantity statist for making a module packet in the project and project.
3, start configuring basic information
3.1 Configuring the language and report output path used by the interface
The first entry system is an English environment, and the basic information dialog box configures the IREPORT system can be enabled by [Tools]-[Options]. Select the interface language you need in the "Language" option, such as "Chinese-China". Click the [Apply] button to create a whole interface throughout the interface.
In the basic information dialog box of the IREPORT system, select [Compile] Label, then determine your report output path, you can select "Compile in Report Data Folders" to select the tick, so that the report's JRXML file and Jasper file are placed in the same file. folder. (You will ask you to save the JRXML file to the specified folder when new reports)
Part of the parameters in the [options] option cannot work directly by the [Apply] button, such as "Look & Feel", must restart IREPORT to work. I don't know if Ireport's bug? ! Finally [archive].
3.2, configure the database connection of mysql
This is the interface between the report and the database. You can turn on the Configuration List dialog by [Data Source] - [Connection / Data Source], Ireport will record all connections previously used, unless you manually remove these connections, all connections will exist in the Connection / Data Source Configuration List dialog box. No matter whether it is indeed available.
Click [New] to enter the configuration new connection interface, as shown:
Fill in the information required for JDBC connections, Ireport supports multiple data source connections, as shown:
This article is only introduced
Database JDBC Connection
Connecting methods, this is also the most common way, especially in embedded report applications. All information is filled in and tested, and finally, it is to save information. Go back to the configuration list dialog, close the dialog, complete
Mysql
database
JDBC
Connection configuration. Tip: If you need a report to provide Chinese content display can be worn in the JDBC URL, such as input: jdbc: mysql: // localhost / subrdb? User = **** & password = **** & useunicode = true & characterencoding = GB2312 where ** ** replace the user-friendly password for the database.
3.3, the basic configuration of the newly built a daily report
Click the first tool "New Report" in the toolbar, create a new report, enter some parameters of the report name, and definition report, such as the name input bugsrpt (example is a BUG quantity statistics for a project), click [more ... .】 Select the label, fill in or select XML encoding, which is related to the selection of the character set of your XML support, please select as needed, such as your XML file support Chinese, then enter GB2312 or GBK, then click [OK] button, enter the design interface of the report. * [Special Tips] Please save the report before starting any work. At this time, the IREPORT prompts the location of the report, and then enter BugSrpt as the name after selecting the appropriate location.
3.4, define the font type and its properties that may be required
The content of a report is five flowers, there are heads, field names, data, other variable information, etc., if this information is consistent font and attribute (such as color), then the entire report is dead, it is very rough. We can define the properties of the elements when adding each element to the report, but that is a time-time job, if you can predefine some of the combinations of properties, you only need to select one of these combinations when you create each element. , Save time and fast.
Click [Preview] - [Report Font] to turn on the Custom Combination dialog. Click [New] to enter the definition dialog box, as shown: Fill in the information and steps in the order in the figure, define "Head", "Group", "Column", "Column", "Statistical Calculation", " Other "The font combination in the like. Pay attention to the selection of the embedded fonts of the PDF, if you need a report, you will make a choice for this.
4, understand a few important concepts
4.1, Ireport's Output Format
The preview output format of IREPORT can support the following:
PDF, HTML, CSV, Java2D, Excel, Pure Text, JRVIEWER, the most commonly used PDF, JRVIEWER. This article is an example of JRVIEWER. JRVIEWER is the output format that is directly used as a report in a C / S mode, and outputs it under the JFrame frame. JasperReport provides a default JRViewer output class.
4.2 Dynamic object variables, parameters, fields of reports
In the process of using IREPOR, you will encounter these content related to variables, parameters, fields, and we want to introduce the use and significance of these objects:
Field (Fields): It is the database content that is available in the database and wants to appear in the report. For example, all values of an ID. $ F {filedsname}
• Parameters: This is your application needs to provide the entrance to the report, such as the condition value of the WHERE statement when you want to interpret it, then parameters can be used. $ P {parametersAme}
• Variables: This is the performance of some logical operations in the report, such as statistics. $ V {variablesname}
The definition format of each object is as follows, for example, a variable is defined, then the expression is written to $ V {variablesname}, the name of this variable is the name of this variable. The use method will be described in detail later.
4.3, compile, static operation, dynamic operation
JasperReport is required to run a Jasper suffix file, the compilation process is actually a file that generates the JRXML suffix to the Jasper suffix. (Can refer to the operation principle of JasperReport) Static operation and dynamic running is relative, the latter runs with data source, such as the database running. The former is a static text run, and the data source is independent. If there is an object related to the data source in the report, it is displayed in NULL.
4.4, report structure
The structure of a report is roughly several parts: Title, Pageheader, Columnhead, Detial, ColumnFooter, Pagefooter, Summary, Groupheader, Groupfooter.
· Title: Each statement generally has a name, such as the ××× sales report, Title is the best place to put this name, of course, you can still put it on the right place as needed.
· PageHeader: Some public elements of the report, such as page numbers, creating time, creative, and other information are placed here is a better choice.
· ColumnHeader: Unconscious here is the name of the list, remember not column data.
· DETIAL: Place data that needs to be loop, such as sales record data.
· ColumnFooter: Place the statistical calculation value of the column level or the description of the column.
• PageFooter: Place the statistical value of the page level or a page of the page.
Summary: There may be a statistical value that may need to be a few pages (your report may have a few pages). For example, 50 sales records occupy 3 pages, then the statistical value of these statistics records is Summary.
· GROUPHEADER: The content of each table may need to divide content and calculation content according to a certain property. For example, you want to display sales records separately in units in units, then you can define a group (definition reference for group), Groupheader is the best place to place a group or group logo.
· GroupFooter: Statistics or description of the group
5, add objects to the table
5.1, add static objects
You can add static objects through the toolbar, such as text, click [T], then click on the blank of the report, so you can add a static object to the report, then drag the border of the object, make it the size, Double-click the property configuration dialog box of the object pop-up, switch to [font] label, in the "Report Font" Combox, "Header" font, (the header font is the custom property combination provided by the previous article), then switch to [Static TEXT] Label, modify the name of the header, such as "BUG Statistics" or "Sales Record Statistics Table", etc. related to business. Add a picture, please click [Image Tool], followed by TEXT. Other static object operations are similar.
5.2, use connection
Remember the "Section 3.2 of the previous article" Configuring the MYSQL database connection? Here we will use the previous configuration. Select menu [create] - [Use Dynamic Connection] Turn on an optional dynamic connection, select any one of your needs, the last [OK], save the report, so your report uses this connection.
* [Special Tips] This connection must be consistent with the connection used in the future application.
5.3, create a SQL query statement
The SQL statement is a language that works with any RDBMS. External users need to use these language management to maintain data in the database, the same, Ireport also needs to do this, we need to provide the language-SQL statement of the query database, this, Ireport This SQL statement gains data, then organizes to the report and appears to meet the needs of users. Enter the SQL Enter dialog box through the menu [Data Source] - [Report Query] and enter the SQL statement in [Report SQL Query] Label. Still manually obtain the available fields for the database table. Click [OK] to save the report.
5.4, create a field dynamic object
The dynamic objects of the report have variables, parameters, fields, foregoing mentioned their concept, here will explain how to use it.
Fields are also the fields in the database, enable the list of fields through the menu [Preview] - [Report Field] (the corresponding tool can be found on the toolbar), you can drag and drop any field to any location of the report, such as drag some bug Content fields to the DETIAL segment (notified, just know the principle).
5.5, create a group
The group is a very important concept, a report can be multiple groups, each group is marked with a keyword, such as wishing the BUG statistics, based on the project (or product). Then you can set a group of item marks. As shown in the figure: The parameter setting of the group can be understood by the interface, the most important thing is "group expression", which is the first field name that must be entered and exists, "Proname" in this article is [Field] An element. Establish other group objects in this class. Each of the groups will appear on the interface of the report, as shown: As for their meaning and accommodation content reference "4.4 Report Structure", they are the first end. (Header and Footer)
5.6, add parameters and use parameters
We reiterate the role of parameters, generally requiring the outside world to provide parameters to the entrance to the report, such as the expression of the WHERE condition of the SQL statement. Enable the Report Parameter List dialog box (Report Parameters) to turn on the Report Parameter List dialog (the corresponding tool can be found on the toolbar). As shown: Enter the name and other parameters. [Ok], save the report.
So how do you use it? Open the SQL statement dialog, refer to "5.3 Creating SQL Query Statements", this time the SQL statement should be: select *
From bugs where proname = $ p {projectname} ORDER BY PRONAME, ModulenAme Note that the red body part is used to apply the variable just defined to the SQL statement. This way when the application provides parameters, as long as the report interpretation engine can replace these variables and then execute the SQL statement, the parameter setting code is provided when the second part is mentioned.
5.7, add variables and use variables
Definition of variables Similar parameters, open the report variable list dialog box by [Preview] - [Report Variable] (Toolbar), as shown, the variable defined in the figure is: Define a bug counter, The data type is java.lang.integer, using the count function for statistics Tester, the scope is the module group, that is, the BUG of the statistical module. The Tester can change to other non-group objects, such as Proname is a group object, do not use the statistical parameters here. The above is provided with a custom variable. In fact, the IREPORT system also provides variables that provide some embedding, such as page numbers, line records, and the like, which are used depending on the needs. 6, the final report
6.1, after completion
6.2, preview report
Click on the dynamic running report, as shown in the figure: 7, summary The first part only introduces how to make a dynamic data report, in fact IReport provides a lot of features for developers, such as histogram, pie chart, and various shapes Graphics, etc. to meet the needs of most applications of enterprises. I hope you can continue to study and make full use. About using the Oracle database as a data source: Provides the JDBC driver corresponding to the Oracle version used, place the drive in the lib directory of the IREPORT, configuring the JDBC connection of the database: Other operations are basically no different. 8. Supplemental content 8.1, implement the form to add the necessary lines in DETIAL to implement the table, with ColumnFooter, ColumnHeader, Detial These 3 positions, you can try the location of the line!