IReport http://www.jasperreport.net First, Introduction
ireport is an open source project capable of creating complex reports, 100% written in the Java language and one of the most popular open source report designers in the world today.
Thanks to its rich graphical interface, you can quickly create any kind of report you want.
Here are some of the more prominent features of ireport:
• Drag-and-drop, WYSIWYG report editor;
• A variety of wizards to simplify complex report design tasks;
• More than 30 typesetting and formatting tools;
• The report can be converted to PDF HTML EXCEL FLASH CSV RTF TXT OpenOffice java2d jrviewer and other formats;
• Support for all valid data sources. such as: Jdbc,cvs,hibernate,javabean, etc.;
• User-definable data sources;
• An unlimited number of undo/redo;
• Integrated scripting (scriptlet) support;
• Built-in chart support: More than 20 kinds of chart support. and can be extended;
• Internationalization: Integrated in more than 15 languages;
• Report template and report library style management;
• Backup of the source files;
• Document Structure Browser. Second, start
In this chapter we will describe the environment required by the ireport runtime, how to download the ireport, and how to install it in our system. 2.1. Operating Environment (Requirements)
The ireport runtime requires the Sun JAVA2 SDK version 1.5 and later, in order to compile our report files we need a complete installation of the JDK. 2.2. Download (Download)
ireport official website: http://ireport.sourceforge.net/cap3.html. 2.3. Running (run)
After downloading the file to local decompression, you can simply double-click IReport.exe or Ireport.bat to run the program.
Note: The version of ireport in this article is 3.0.0. III. Structure of the report
In this chapter we will analyze the structure of the report, see what each part of the report does, and what effect each part will have when generating the report. Bar (bands)
The report is divided vertically into several sections, each of which we call "band". Each band has its own characteristics, some will print once when the report is generated, and some will print multiple times. The following figure.
Next we will introduce each type of band separately.
title Band: Title field is displayed only on the top part of the first page of the entire report, except for the first page, no matter how many pages in the report will not appear in the title Band content. is the title of the report.
pageheader Band:The contents of the PageHeader section will appear on every page in the entire report, and the position displayed is at the top of the page. If it is the first page of the report, the content in PageHeader will appear under Title Band, except for all pages other than the first page, and the content in PageHeader will appear at the top of the page, which is the header.
pagefooter Band: appears at the bottom of the page, which is the footer.
Detail Band: The Report Content section, in this Band design report needs to repeat the content, the content of the Detail section appears every page.
columnheader Band: for detail Band, the header of the report is typically drawn in this section.
columnfooter Band: the footer segment for detail Band.
Summary Band: A summary of the table, which appears after detail Band on the last page of the entire report, is typically used to count the totals of one or more fields in the report.
lastpagefooter: The content will appear in the last part of the last page of the report.
To actually demonstrate the effects of each band, we can add different objects to each band in the report to test the role of each band.
Steps:
First step: Create a new document, name the report named: Myfirstreport, and then click OK.
Step two: Set the JDBC Connection parameters
Click "Dateà Connection/Data source" and the following interface appears:
Then click "New" and the following interface appears:
Select the "Database JDBC Connection" entry, click "Next",
Set the parameters, click "Test", if the test is successful, click Save, if the test is unsuccessful, please check the above steps.
Step Three: Click Dataà Report Query. In Query Editor, enter the following statement:
SELECT * from Tb_employee, then click "OK"
Fourth step: Click the "Text not changing" button to add individual band content.
The individual band in the report are placed as follows:
Fifth Step: Drag the field you want to display to the "detail" column
Sixth step: Click "Execute Report" to see the results:
To start the report, the resulting effect is as follows:
Last page: four, report elements
In this chapter we will describe the objects that can be used in the report and their related properties.
The elements we are talking about are mainly graphic objects, such as Text,rectangle. There is no concept of paragraph, table, or paging in ireport, there are seven basic objects in the report:
-line (line segment)
-rectangle (Rectangle)
-ellipse (Oval)
-static text (text not to be changed)
-text field (text field)
-image (image)
-subreport (sub-report)
-crosstab ()
-chart
Through the combination of these components we can make a variety of complex report styles. ireport offers two special graphic objects: chart and barcode.
Each type of object has some common properties, such as height, width, position, band, and so on. In addition, there are special attributes for different elements.
Each element must be inside a band, and if an element spans more than one band, it will throw an exception where the element's position is wrong at compile time.
To add an element to a report, we can select the appropriate tool from the main toolbar and add it to the report area.
We can open the element's property settings window by double-clicking the object or the right-click menu.
The Properties window consists of several tabs, and the "Common" tab contains the common properties that each component has, and the other tabs are property settings for different elements. In the following sections, we will describe the use of each object in detail. v. Fonts and Styles
A style is a set of predefined properties that control the appearance of elements (such as background color, border lines, fonts, and so on). We can define a default style for our reports.
To apply a style to an element, we can select the element and choose the style you want from the style list in the Common tab of the element's Properties window.
Fonts is used to describe the appearance characteristics of text. 5.1. Fonts (font)
Usually defining a font, we need to do several things:
-font name (font name)
-font dimension (font size)
-attribute (attributes, such as bold-faced,italics,underlined,barred)
If we need to export a report in PDF format, we need to add the following information to the font:
PDF Font Name:pdf font name (pre-defined PDF font or a name for a TIF file in Classpath).
PDF Embedded: whether to include a logo in a PDF file when generating a report using a font file of an external TTF category.
PDF Encoding: Specifies a font encoding name.
Click Format à report glyph to create a custom font.
If we need to export the report to PDF format, the specified PDF font will be used, and the other attributes will be ignored. 5.2. Style (styles)
We can define a style by selecting "Format Styles" from the main menu. As shown in the following illustration:
Click Format Àstyles to create a custom style.
Click the "New" button to add a new style. In the popup window we can define some properties, if we need the value of a property, we can click on the button to implement.
In the "Common" tab of each element's Properties window there is a style property drop-down box, if we need to set a style for an element, we can select our defined style in this drop-down box so that a style can be applied to an element.
We can add a dynamic condition to a style, use a style when the condition is satisfied, or use the default style. For example, let's take the employee table in the database for example, if the employee's length of work is greater than 3 o'clock, we allow the length of work to be displayed in a red bold font.
Open the Query window and enter the following SQL:
Select * from Tb_employee
Create a new style, create a new conditions in the pop-up style conditions, and enter the following expression in the expression window:
($F {employee.workingyear}.intvalue () >2?newboolean (true): New Boolean (false))
The following figure:
The new style is shown below:
Next, put the employee name and seniority into the report, and apply our new style to the salary field, as shown below:
The start view runs as follows: vi. fields, parameters, variables
There are three types of ireport that can be used to save data: Fields/parameters/variables. These three objects can be used in expression in some places to dynamically change certain values when a report is generated by certain logic. Fields/parameters/variables have a type concept, and their type is a standard Java object. To use these three types of objects we have to create it first, by clicking on the "View" main menu and selecting the submenu fields,variables,parameters we can create different objects. With these three submenus we can view and manage Fields,variables,parameters objects.
The following interface will appear:
Through this window, we can create, modify, and delete these three types of objects. 6.1. Field (fields)
field in ireport is dedicated to organizing records. There are several ways to create field, and we can create the field we need based on the various types of data sources in ireport. 6.1.1, in the Fields tab, we can click the New button to create a new field.
A field has two required attributes: name, type, and an optional description. Starting with ireport1.0.0, fields can be any Java data type. This allows us to easily convert from java.lang.Object to field in the report when we use JavaBean as the data source. In the object's expression you can use the following syntax to refer to a field:
$F {field name}
For example, if you want to work with the username field in the Com.bstek.test.domain.Person bean, you can write:
((Com.bstek.test.domain.Person) ${username}) 6.1.2, use a SQL query to create field
In the design of ireport, the use of SQL query to create or record fields is the most widely used and the most straightforward way. To do this we need to open the Reportquerydialog window first (click on the main toolbar button), after the Reportquerydialog window appears, we can enter the appropriate SQL to retrieve and create fields.
Before opening the Reportquerydialog window, make sure that we already have a data source connection activated (see the relevant section for activation) and insert a query statement such as: SELECT * Form Employee
ireport will start the SQL query engine, displaying the results of the analysis in fields based on the SQL input. The fields are created by selecting the fields you want in the window below (you can select multiple options) and clicking the OK button.
In this example, all fields have multiple types, generally, and the field's data type is built on the original SQL type, such as String,integer ... 6.1.3, creating fields with JavaBean
One of the advanced features of ireport is that its data sources can be created using JavaBean (or Pojo) objects that contain getter and setter methods, in addition to building on SQL query. In this example, fields are the attributes of the Pojo class (or properties of the property, that is, the properties of the child object).
The first step: Define a simple bean:
Package Com.asinfo.ccm.model;
public class Productbean {
Private Stringproductname;
Public Stringgetproductname () {
Returnproductname;
}
Public Voidsetproductname (String productName) {
This.productname= ProductName;
}
}
Second step: To use JavaBean as the data source in ireport, first define the Classpath so that ireport can find the bean we defined, using the menu "Options"-"Classpath" to define Classpath, The path defined is the path where the compiled. class file is located, for example, I define the classpath as "C:\Documents and settings\wzz\workspace\ccmrightinfo\webroot\web-inf\ Classes ".
Step Three: In the menu "data"-"connections/datasources" menu, add data source, set the data source type to "JavaBeans Set data Source", set the data source properties as shown in the figure below:
Fourth step: Then go to the menu "Data"-"Report Query" to define the required bean properties, enter the name of the custom Bean in "Class name", in this case "Lld.test.ireport.ProductBean", and then click " Read Attributes "button to get the bean properties, click" Add Selected Field (s) "To add the properties you want to use, as shown in the following illustration:
To add a property to the field list, simply select the desired attribute and click on the "add field (s)" button. 6.2. Parameters (Parameters)
Parameters is usually used to transfer values from a program to a report at the time of printing. That is to say, parameters is usually the function of parameter passing. They can be used on specific occasions (such as the conditions of an SQL query in an application), such as any variable in the report that needs to be passed in externally (such as a string of char or report title included in an Image object).
As with fields, parameters also needs to define its data type at the time of creation. The data type of parameters is the standard Java object.
In ireport, the parameters mechanism is to allow the user to pass parameters to the report through the application. Expression in an object in Jasperreort can use the following syntax to access a parameter that exists in the current ireport:
$P {parameter name}.
If the application does not assign a value to the parameter defined in the report, then parameter will take the value of the default value that we set when defining it. Parameters is a Java object, so if its type is an object type, it is wrong to write the following expression in its default value:
0.123
You have to create an object, such as:
New Double (0.123)
This is the right way to do it. 6.2.1, using parameters in queries
Parameters can be used to do the pass of conditional parameters for SQL queries. If you want to get the details of the customer according to the department number (DEPT_ID) (We do not know the specific value of this department number when designing). We can now organize query statements like this:
SELECT * FROM employee where dept_id= $P {deptid}
The SQL query engine will then use PreparedStatement to process the incoming DeptID value as a query condition parameter.
If you want to pass the parameter value directly as part of the SQL statement, you can use the following special syntax: $P! {parameter name}. This notation allows you to replace parameter name with the value of parameter at query time. For example, if we have a parameter named Mywhere whose value is: where Dept_id=d11, the query is spelled as follows:
Select * FROM Employee $P! {Mywhere}
The actual string to be submitted at query time is:
Select * FROM employee where Dept_id=d11 6.2.2, used in program parameters
In the application, if you want to pass a value to our report parameters, we can put the relevant value into an extension Java.util.Map interface class, and then passed into the ireport. As in the following code:
... Map HM = new HashMap (); ... Jasperprint print = Jasperfillmanager.fillreport (filename,hm,new jremptydatasource ()); |
Fillreport is a key approach that allows you to pass a file name, a parameter set, to the report that we want to invoke. Example-we can have an incoming value in a department as the title of our report by the following processing method.
The following is an example:
a). Declare a parameter. This parameter is a java.lang.String type with the name: Name_report:
b). Drag the parameter to Titleband in the page. Modify the above program code as follows:
HashMap HM = new HashMap (); Hm.put ("Name_report", "report title"); ... Jasperprint print = Jasperfillmanager.fillreport (FILENAME,HM, New Jremptydatasource ()); |
The run-time effect is as follows:
In general, we do not need to pass a value for each parameters in the report, unless a parameter must be passed in from an external program when we need to set a value for a parameter. If a parameter program is not assigned a value then ireport will use default value expression to initialize a parameter if the default value Expression is not set so its value is null.
In fact, for a parameters we can not only pass a string,integer,boolean and other Java small objects, but also we can use parameters to pass some large enough complex objects, For example, an image (Java.awt.Image), or a parameters for a datasource connection to a subreport, and so on. When passing parameters with the map type, we should note that the arguments passed are consistent with the parameters type in the report, otherwise the classcastexception will be thrown.
ireport Embedded Parameters:
ireport provides a number of built-in parameters that are read-only for the user parameters. The list of embedded parameters is as follows:
Name of parameter |
Description |
Report_parameters_map |
It is an object of type Java.util.Map, which can be passed to populate the report to populate all the parameters in the report, in this parameter also contains user-defined parameters. |
Report_connection |
A JDBC connection parameter, the report can receive the corresponding data according to the incoming JDBC connection |
Report_datasource |
Provides a datasource for connection in a report. When the external incoming type is also datasource type |
Report_scriptlet |
The Scriptlet instance that is used when the report is generated, and if no scriptlet is specified, the parameters uses Net.sf.jasperreports.engine. Jrdefaultscriptlet. |
Is_ignore_pagination |
With this parameter you can control whether paging is turned on or off. By default, pagination is turned on. However, if you export to HTML or Excel, you are not paging. |
Report_locale |
It is used to set the region where the report is located. If no region is provided, then the system default region will be used. |
Report_time_zone |
Set the time zone of the report, if not provided, with the system default. |
Report_resource_boundle |
Specify the resource file to load for the report, and in the internationalization chapter, describe how to use a resource file to build the report. |
6.3. Variables (Variables)
Variables is used to store the results of calculations on certain fields, such as Sum.
Like Fields,parameters, variables also has data types. You must declare a Java type for a variable instance. The figure above shows how to create a new variable. Take a look at the meaning of each field:
Variable the name of the name:variable, we can refer to a Variable in the expression by the following method:
$V {variable name}
Variable Class type:variable can also be any Java type, in the drop-down box you can see most commonly used types, such as java.lang.String and so on.
Calculation type: A calculation type can be used to calculate the result. When a pre-defined value is "nothing", it means not doing any calculations. ireport calculates the result based on the specified calculation type and each record fetched from the data source, and then changes the value of the variables. The calculation of a variable means to execute an expression, and if the calculation type is nothing, then ireport will assign a value to variable according to variable expression. The calculation types are listed below:
Type |
Description |
Nothing |
Do not make any calculations |
Count |
The number of recordsets is recycled (counting how many records), which is not the same as sum, sum is calculated for numbers, and numbers are incremented. |
Distinct Count |
It is also the cumulative operation of how many records are counted, except that the records that have occurred are ignored during the accumulation process (for example, the value of a field is not recorded after it has already appeared) |
Sum |
Add each expression (the expression here can only be a numeric type) The result of the iteration into the variable |
Average |
It calculates the average of the cumulative result of an expression using arithmetic methods. |
Lowest |
Returns the minimum value of an expression |
Highest |
Returns the maximum value of an expression |
Standarddeviation |
Returns the standard variance based on all the values of an expression |
Variance |
Returns the change in the value received by the expression |
System |
No computation is done and the expression is worthless, in which case the report engine simply saves the last value of the variable in memory. |
Reset type: Resets types. Indicates when a variable is to be reset. The reset type is as follows:
Type name |
Description |
None |
Do not reset operations at any time |
Report |
The expression is not initialized by variable when the report is created |
Page |
Variable initialization on every page reset operation |
Column |
Variable initialization in each column does a reset operation (the column here refers to the page column in the report, and the column of each report can be modified by the Editàreport Properties window of the statement) |
Group |
Variable initialization in each group do a reset operation |
Reset Group: If group reset type is checked, the group name will be selected (for group reference, refer to later chapters).
Increment: It specifies that when a variable value is evaluated or when you want this group to be calculated using a similar subtotals or averages, the Increment type can refer to the following table.
Increment Group: If the group's Increment type is checked, it will specify that the set determines the Increment of variable.
Custom Incrementer Factory class: Specifies a Java class name that implements the Jrincrementerfactory interface and is typically used to define operations of non-numeric types such as Sum.
Variable expression: It is a Java expression used to identify the value of each iteration of the Variable.
Initial Value expression: It is an expression used when initializing for variable.
Built-in variables:
Like parameters, ireport provides some built-in variables (which are managed directly by the report engine) that are read-only and cannot be modified by the user, as shown in the following table:
Variable Name |
Description |
Page_number |
It contains the page of the current page. |
Column_number |
It contains the current number of columns. |
Report_count |
Current number of records |
Page_count |
The current page number |
Column_count |
The current column number |
<group Name>_count |
Total number of records in <group name> |
seven, columns and groups
In this chapter we will explain how to use ireports to manage bands and groups, and in the fourth chapter we describe the structure of the report, and in this chapter we will see how to use bands to divide the report. We'll also see how to use groups, how to create breaks in the report, how to manage subtotals, and so on. 7.1. Column (bands)
ireport divides a report into seven main bands and a band called Backgroup (altogether eight bands), and when we add a group there are two new band added to the report: group footer Bands and group header bands.
Click the button in the pop-up window we can modify the relevant properties of the bands that the current report has.
Using the window in the image above, we can modify any of the band properties in the List window on the left, such as its height (in pixels). Print when expression indicates that we can enter an expression here so that we can control whether the current band output is based on the value of the expression. It is important to note that print when expression must return an object of type Java.lang.Boolean, such as:
New Boolean (True)
The above expression indicates that the bands will always be printed (if we do not enter this is the default value), in the expression can use Fields,variables,parameters. Based on this feature, we can flexibly control whether the current band can be output by the relevant fields,variables,parameters, such as the following code:
($F {salary}.intvalue () >4000?new Boolean (True): Newboolean (False))
The above code shows that if the value of salary is greater than 4000, the current band is output or the current band is not output.
Move the mouse to the edge of the bands we can drag the mouse to change the size of band to adapt to the contents of the band. 7.2, Group (Groups)
Groups allows us to make a new cut grouping of the data in the report. Grouping is defined by an expression, Irepo