Part I: Detailed procedures for making a report using ireport (in Windows environment)

Source: Internet
Author: User

Hint: In some sections, the text in the picture can not see, suggest to my blog browse article: http://blog.csdn.net/jemlee2002/
The article will cover 3 elements:

Part I: Detailed procedures for making a report using ireport (in Windows environment)

Part II: Developing a rich client report application using Jasperreport as a report control

Part III: Developing Web Report applications using Jasperreport as a report control

1. Preface

There are many articles on the web that can be searched for various reporting tasks using ireport and jasperreport, but I think there is very little (almost no) to do a more detailed description of how to use ireport to make a report, the basic idea of the article I have read is broad coverage , a lot of content is mentioned that is, not developers have time for each practice time to spend their own implementation, if you can have more detailed information, it is not a pleasure. Because of this idea prompted me to write this article, I hope to be able to use ireport and jasperreport friends to help, especially need to personally to achieve the report of friends, hope to bring you a little help. This article is not an introduction to every detail of ireport, and every detail about ireport can be consulted on ireport's website, but may cost a little. It is not necessary for the average user to pay for these costs because we are concerned with making the reports we need, rather than expanding the ireport, such as making ireport plugins.

2, prepare 2.1, download the JDK

Address: http://www.sun.com , select 1.3 above version (recommended 1.4.2 above version), install JDK , install by default, if your system has already installed jdk or jre to omit this step and verify jdk or jre can run by default on the command line (cmd ) into x:>java  if it appears: usage : java .................... The beginning of a heap of information is both verified. Otherwise you must configure the configuration information as follows, in windows environment variable settings:

path: Add "; Java installation directory" on the last side

java_home : "Installation directory forJAVA "

CLASSPATH: "java installation directory /bin"
Re-verify that the JDK or JRE can run anywhere in cmd

2.2. Download ireport

Address:http://ireport.sourceforge.net/, select 0.3.2 version (recommended version before press release) decompression ireport in any directory, The extracted file has a ireport.bat, by double-clicking, after about a few seconds if you can pop up iReport 's main form that your system can already run IReport , If the main form cannot be ejected, it is usually a first-step error or is not completed.

2.3. Prepare the Database

ireport supports most databases as long as the database can provide JDBC Drive. This article provides mysql database as an example, but at the end of the article it will refer to how to configure oracle . Please refer to the other materials for the installation and establishment of the database which are not covered by this article. This article assumes that the mysql and the mysql already has some tables and determines that data already exist in the table.

*"Special tips" version ofMySQL requires compatibility with the Lib directory under the iReport folder with MySQL driver, I recommend to http:/ /dev.mysql.com/downloads/ Download the latest version of the MySQL drive, so you should not be wary of database support issues for JDBC Drive issues.

2.4. Start the MySQL service

Determining the character set used by Mysql is an important issue, especially for friends who need a Chinese report, and should pay particular attention to this issue.

2.5. Determine business logic

What kind of report tasks do you want to accomplish? A detailed description of the report that needs to be implemented, which is the business condition for implementing the report, or most of the tasks are meaningless. This article uses a bug record table as an example of this article to make a bug volume statistic that is grouped according to the modules in the project and project.

3, start to configure the basic information 3.1, the configuration interface using the language and report output path

The first time to enter the system is the English environment, you can use "Tools"-"options" To open the configuration ireport System Basic Information dialog box. In the "Language" option, select the interface language you want, such as "Chinese-Chinese". Click on the "Apply" button and the system combines the entire interface with the culture.

in the Configure ireport System Basic Information dialog box select "Compile"Label, then decide your report output path, you can "compile in the Report Data folder" Select Tick, so the report's jrxml file and The Jasper file is placed in the same folder. (You will be asked to save the jrxml file to the specified folder when you create a new report.)

Someof the parameter modifications in the Options option do not work directly with the "Apply" button, such as "Look&feel" and must be restarted IReport To work. Do not know is not the ireport Bug?! The last "archive".

3.2. Configure MySQL database connection

This is the interface between the report and the database. You can open the Configuration List dialog box from the data source-connection / data source, andIReport will record all previously used connections, unless you manually delete the connections, or all connections will be connected / The Data Source Configuration List dialog box, whether or not it is indeed available.

Click "New" to enter the configuration

Fill in the information required for the JDBC connection, ireport supports multiple data source connections,

This article simply introduces the database JDBC connection connection method, which is also the most common way, especially in the embedded report application. After all the information is filled in and tested, the last thing to do is to save the information. Go back to the Configuration List dialog box and close the dialog to complete the MySQL database JDBC connection configuration.
Tip: If you need a report to provide Chinese content display can be used at the JDBC URL, such as input:
jdbc:mysql://localhost/subrdb?user=****&password=****&useunicode=true&characterencoding=gb2312
The * * * number is replaced with the database user password.

3.3, the basic configuration of a new empty report

Click the First tool of the toolbar new report ', create a new one, enter a report name and define some parameters for the report, For example name input bugsrpt (example is to do a project bug Volume Statistics report) click more. "Select the label, fill out or select xml encoding, which is related to your xml supported character set selection, please select as needed, such as need your xml file supports Chinese, then you can enter the Span style= "' font-family:" "Times New Roman"; >gb2312 or GBK , then click "ok button to enter the design interface of the report.

*"Special tips" Please save the report before starting any work, IReport prompt to save the location of the report, select the appropriate location and enter Bugsrpt as the name.

3.4. Define the font types and their properties that the report may require

A report has a variety of content, such as header, column name, data, other variable information, and so on, if the information is consistent font and attributes (such as color), then the entire report is lifeless, it is very rough. We can define the attributes of an element when we add each element to the report, but that's a time-consuming task, and if you can pre-define a combination of attributes, then just select one of those combinations when creating each element, and it's easy and fast.

Click Preview-Report font to open the Custom Combo dialog box. ClickNewto enter the definition Details dialog box, fill in the information and steps in the order shown in the diagram, and then define the 6 font combinations in header, group, column, column contents, statistical calculation, other, and so on. Note the choice of the PDF inline font, if you want the report to be provided in PDF file format, then you need to make a choice.

4, understand a few important concepts 4.1, ireport output format

The preview output format of

ireport can support the following:

PDF,HTML,CSV,java2d,Excel, plain text,jrviewer, most commonly PDF ,jrviewer. This article takes Jrviewer as an example. jrviewer is the output format of the report directly in the form of C/S , under the JFrame framework. jasperreport provides the default jrviewer output class.

4.2. Dynamic object variables, parameters, fields of the report

In the process of using IReport , there are many things related to variables (Variables), parameters (Parameters), fields, We will describe the use and significance of these objects:

• Fields:database content that is extracted from the database and that you want to appear in the report. For example, all values of an ID . $F {Filedsname}

• Parameters (Parameters): This is the entry that your application needs to provide to the report, such as when you want to provide the conditional value of the where statement when the report is interpreted, then you can use the parameter (Parameters). $P {parametername}

• Variable (Variables): This is the performance of some logical operations in the report, such as statistical values. $V {Variablesname}

Each object is defined in the form of a description of each object, such as defining a variable (Variables), and the expression is written $V {variablesname}, and the name of the variable appears in the report. The use method is described in detail later in this article.

4.3, compile, static operation, dynamic operation

jasperreport Runtime needs is a jasper suffix file, the compilation process is actually the jrxml suffix file generation jasper suffix file. (You can refer to the operating principle of Jasperreport )

Static and dynamic runs are relative, and the latter runs with the data source, such as with a database run. The former is static text run, which is independent of the data source, and is displayed as null if the object associated with the data source appears in the report.

4.4. Report Structure

The structure of a report is roughly a few parts:title,pageheader,columnheader,detial, Columnfooter,pagefooter,summary,groupheader,groupfooter.

· Title: Each report will generally have a name, such as XXX Sales report,title is the best place to shelve the name, of course, you can also be placed in the appropriate place.

PageHeader: Some of the common elements of the report, such as page numbers, creation time, creator, etc., are placed here as a better choice.

ColumnHeader: Beyond reproach, here is the name of the placed column, remembering that it is not column data.

· Detial: Place data that needs to be cycled, such as sales record data.

columnfooter: A statistical calculated value or a description of the column at which the column level is placed.

pagefooter: Place a page-level statistic or a description of the page.

· Summary: You may need statistical values for several pages (your report may have several pages). For example, with a total of 3 pages in a sales record, the best place to put statistics on these statistics is summary.

groupheader: The contents of each table may need to be divided by a property to display content and calculations, for example, if you want to display sales records separately for each group in the month, then you can define a group (the definition of the group is referenced later),GroupHeader is to place the group description or the best place for the group logo.

· GroupFooter: Statistics or description of placement groups

5. Add Object 5.1 to table, add static object

You can add static objects, such as text, click "T" from the tool bar, then click in the blank of the report, then add the static object to the report, then drag the object's border so that it is the right size, double-click the Object Popup object's Properties Configuration dialog box, and switch to " font "Label, in the Combox of the reportfont, select the" header "font (the header font is the custom attribute combination provided earlier), and then switch to the"Static Text"Label, modify the name of the header, such as"Bug Statistics Report "or" Sales Record table "and so on business-related content. To add a picture, click on "image Tool", followed by a similar operation as Text . Other static object operation steps are similar.

5.2. Use the connection

Remember the previous article (3.2 section) Configuring a MySql database connection? Here we will use the connection configured in the previous article. Select the menu "make"-"Use dynamic connection" to turn on optional dynamic connection, select any connection you need to the last "OK", save the report so that your report uses this connection.

*"Special note" This connection must be consistent with the connection used by the application in the future.

5.3. Creating SQL query statements

       sql statement is for any Span style= "' font-family:" "Times New Roman"; >rdbms the language in which external users need to manage the data in the database using these languages, the same ireport Also need to do this, we need to provide query database language-sql statement, so ireport this sql statement gets the data and then organizes it into the report and displays it to meet the needs of the user.

Open the SQL Input dialog box from the menu "data source"-"Report query", and in the "SQLquery" Label , enter the " C5>automatically RetrieveFields "checkbox and"readfields"button is to confirm the availability of automatic or manual fetch of database tables fields. ClickOKto save the report.

5.4. CreateFieldDynamic objects

The dynamic object of the report has variables, parameters, fields, and the previous article mentions their concepts, which will explain how to use them.

field is the field in the database, through the menu "preview"-"Report fields" open the list of fields (the tool bar can find the appropriate tool), you can drag and drop any field to the report anywhere, such as drag a part of the Bug 's Content field to detial Paragraph (content does not matter, just know the principle).

5.5. Create a group

A group is a very important concept, a report can be more than one group, each group is tagged with a keyword, such as the hope that the Bug statistics are based on the project (or product) statistics. Then you can set up a project-tagged group.

The parameter setting of the group can be understood by the interface, the most important of which is "group expression", which is the name of the field that must be typed correctly and exists, "proname" in this article is an element in "field". And so on, create additional group objects. Each set up a group, the report will appear on the interface of the group corresponding to the paragraph, as to their meaning and content to refer to the "4.4 report structure", they are the corresponding appearance. (Header and footer)

5.6. Adding parameters and Using Parameters

We reiterate that parametric action is typically an expression that requires an external argument to the report's entry, such as the Where condition of the SQL statement. Use preview-Report parameters to open the report Parameter List dialog box (tools can be found on the toolbar). Enter a name and other parameters. "OK" to save the report.

So how do you use it? Open the SQL Statement dialog box and refer to "5.3 Create SQL query Statement", this time the SQL statement should be:
SELECT *

From bugs where proname= $P {ProjectName} ORDER by Proname,modulename
Note that the red part of the word is to apply the variable you just defined to the SQL statements. This allows the report interpreter engine to replace these variables and then execute the SQL statement when the application provides parameters, providing the parameter-setting code when programming is mentioned in the second part of the specification.

5.7. Adding variables and using variables

A variable is defined like a parameter, which opens the Report Variable List dialog box (the tool can be found on the toolbar) through preview-report variables, and the variables defined in the figure are: a counter that defines a Bug , the data type is Java.lang.Integer , use the count function to testerthe Statistics field, which is the module group, which is the number of bugs in the statistical module. The tester can be changed to other non-group objects, such as Proname is a group object, do not use as the statistical parameters here. The above provides a custom variable, in fact ireport system also provides some embedded (buildin) variables, such as page number, row record number, etc., as needed.

6, the final report 6.1, the completion of the report 6.2, preview the reportClick on the report to run dynamically, and the content appears:7. Summary

The first part just introduces how to make a Dynamic Data report, in fact, IReport also provides a lot of features for developers to use, such as column chart, pie chart, and a variety of shapes and so on, to meet the needs of most applications of enterprises. I hope you can continue to study and make full use of it.     

about using the Oracle database as a data source: Provide the JDBC driver for the Oracle version you are using and place the drive in the iReport lib directory , there is basically no difference in the other operations when configuring the database's JDBC connection.

8. Supplementary Content 8.1, implementation form

You can add the necessary line implementation table in Detial, with Columnfooter, ColumnHeader, detial 3 locations, you can try to draw the position of the line!

Related Article

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.