JasperReports is an open-source reporting tool for Java that generates application reports. This reporting tool supports exporting rich report content to screens, printers, or various file formats such as HTML, PDF, XLS, RTF, CSV, XML, ODT, and TXT.
JasperReports can also be used for Java-enabled applications such as Java EE or web applications to generate dynamic content.
Install JasperReports
Install Jasper Reports Community Edition from here.
https://community.jaspersoft.com/community-download
Jasper report structure
The .jrxml report template consists of the following parts as shown in the diagram below.
1. The title contains the "headline" of the report. It only appears once at the beginning of the report. For example, the report is titled "Student Report."
2. The page title can include date and time information or the institution name. It is displayed at the top of each page. For example: the page title can be "Student Records."
3. Column headers list the names of specific fields to be displayed in the report, such as "Student ID," "Student Name," "Student Address," etc.
4. The detailed information section displays entries for specific fields. For example: Student ID=101, Student Name="Den Peek", Student Address="Pune".
5. Column footers can display the sum of any field. For example, "Total study time: 6 hours per day."
6. The footer may contain page number information. It is displayed at the bottom of each page, for example: "Page numbers 1, 2, 3, and all page numbers."
7. Abstract: The summary of the report, written at least once in the abstract.
JasperReports — Lifecycle
Design the report — In this step, we will create a JRXML file, which is an XML document containing the report layout definition. Compilation Report - In this step, JRXML is compiled into a binary object named Jasper (*.jasper). Filling data into reports—in this step, data from the application is populated into the compiled report. The net.sf.jasperreports.engine.JasperFillManager class provides the necessary functions needed to fill the report data. The system will create a Jasper print file (*.jrprint), which can be used to print or export reports. Export Report - In this step, we can use JasperExportManager to export the Jasper print file created in the previous step into any format.
API class
There are many classes available for compiling JRXML report designs, filling out reports, printing reports, exporting them as PDF, HTML, and XML files.
What content is contained in the (net.sf.jasperreports.engine) package?
· JasperCompileManager — Used to compile JRXML report templates. · JasperFillManager — Used to fill reports using data from data sources. · JasperPrintManager — Used to print documents generated by the JasperReports library. · JasperExportManager — Used to obtain PDF, HTML, or XML content of documents generated during the report filling process.
Data source
A data source is a structured data container. When generating reports, the JasperReports engine retrieves data from the data source. Data can be obtained from databases, XML files, object arrays, and object collections.
The table below summarizes data sources and their implementation classes—
Jasper Reports data elements
Parameters: $P $P{parameter_name)
· It is used for individual values in the report.
· Data that cannot be passed through the data source can be passed through parameters. For example, report titles and other data.
· JasperReports templates or JRXML templates can have zero or more parameter elements.
Field: $F $F{field_name}
· Report fields are elements that represent the data mapping between data sources and report templates.
It is used to change values in each iteration, or in other words, to represent a row of data in a table.
Variable: $V $V{variable_name}
· It is provided by Jasper reports.
It is also used to perform calculations on Jasper reports, such as arithmetic operations.
Steps to create any Jasper report
1. Design the template by copying the jrxml file to the Spring Resources folder
2. Create required parameters
3. Create data sources
4. Compile the .jrmxl template stored in the JasperReport object
5. Fill the report—by passing compiled .jrxml objects, parameters, and data sources.
6. Export Reports — Use JasperExportManager
Example of SpringBoot — JasperReports
Add Maven dependencies.
Controller
service
emp24.jrxml
After running, you will receive the generated report.http://localhost:8990/empapp/api/v1/reports/jasper/emp24?fileType=doc
JasperReports — Example of a subreport representation
For subreports, open the existing template emp.jrxml, and add another template user.jrxml as the subreport.
To add a child report to the main report, open the main report, click the child report from the panel, drag it to the summary area, and provide the following details.
Select "Create only child report elements," then click "Next." Do not use any database connections, then proceed to the next step and finish. Right-click > Align > Adapt to Width/Table Format
For User.jrxml subreports, we will pass subreport data from the main report as a parameter. To achieve this, we need ......
Create a new parameter like subReport and the data type JasperReport as in the main report Create new parameters for subDatasource and data type JRBeanCollectionDataSource Create new parameters subParameters and data type Map
Now open the subreport properties
Eliminate $P{REPORT_CONNECTION} Expression: $P{subReport} Parameter mapping expression: $P{subParameters} Data source expression: $P{subReportDataSource}
Now, any data we pass as a parameter will be populated into the subreport.
Sample code
empSubreport.jrxml
Test him:http://localhost:8990/empapp/api/v1/reports/jasper/subreport?fileType=pdf
Reference:The hyperlink login is visible. Original:The hyperlink login is visible. |