We can generate reports using two ways,
1) From "JRXML" (Source) file
2) From "Jasper" (Compiled) file
Tools Needed For This Tutorial
You need to download
Note
If you are created a JRXML/Jasper file using a particular version of Ireport (For example consider Ireport-4.7.0)
In order to run the report, you must use same set of jar's available on installed Ireport-4.7.0 directory (C:\Program Files\Jaspersoft\iReport-4.7.0\ireport\modules\ext)
If you are creating a report using Ireport-4.7.0 and compiling / running using Ireport-4.2.0 jar files, you will get java.lang.NullPointerException
Creating the XML File for
JasperReports using iReport
Execution of report from JRXML file will be very slow, as it need to compile before the execution
1) From "JRXML" (Source) file
2) From "Jasper" (Compiled) file
Tools Needed For This Tutorial
You need to download
- JDK 6
- iReport-4.1.1 for designing the report
- commons-beanutils-1.8.2.jar
- commons-collections-3.2.1.jar
- commons-digester-1.7.jar
- commons-logging-1.1.jar
- groovy-all-1.7.5.jar
- iText-2.1.7.jar
- jasperreports-4.1.1.jar
Note
If you are created a JRXML/Jasper file using a particular version of Ireport (For example consider Ireport-4.7.0)
In order to run the report, you must use same set of jar's available on installed Ireport-4.7.0 directory (C:\Program Files\Jaspersoft\iReport-4.7.0\ireport\modules\ext)
If you are creating a report using Ireport-4.7.0 and compiling / running using Ireport-4.2.0 jar files, you will get java.lang.NullPointerException
Creating a Sample Report
A sample report which shows work hours of employees will be
created. Report will be created based on the following database entries shown
in Figure 2. Any database
can be used as long as you can obtain a reference to a java.sql.Connection.
Figure 2: Sample
Table – “tutorial_table” in “tutorial_db”
We want to show the same data in the table,
however in a different format, for example, all entries for each employee
grouped together. We also want to display the total hours worked for each employee.
And finally, to give a comparison among the employees, we want to display these
information in a chart. Figure 3 shows what we want to obtain.
Figure 3: Desired
Final Report
Start
by creating a new document in iReport. (File
>New Document) In the report properties screen, specify the report name
as “EmployeeReport” and confirm other default values.
Before
proceeding further, bear the following in mind:-
Use Edit
> Insert Element to insert different types of elements into the report.
After selecting the element, you will see that the cursor turns into a “+” sign. Pressing
the left button of the mouse and dragging it, specify the bounds of the
inserted element. Double-clicking on the element opens the property dialog, use
this dialog to edit element properties. Property names are mostly
self-explanatory.
Dynamic content of the report is provided
by three things.
1.
Parameters
2. Fields
3. Variables.
All these three are added to the report as “Text Field”s through
Edit-Insert element-Text field.
To specify a text field’s content to come
from a Parameter, specify the Text
field expression in the property dialog of the element like the following: $P{parameterName}
Similarly, to specify a text field’s
content to come from a Field,
specify the Text field expression in the property dialog of the element like
the following: $F{fieldName}
In
our case, fieldName can be any of “EmployeeName”, “HoursWorked”, “Date”.
Similarly, to specify a text field’s
content to come from a variable, specify the Text field expression in the
property dialog of the element like the following: $V{variableName}
After specifying a text field’s content to
come from either a Parameter, a Field or a Variable, you should define the
corresponding content name in “Report Fields”, “Report Parameters” or “Report Varibles” through “View-Report Fields”, “View-Report
Parameters” or “View-Report Variables”.
By defining a “Report parameter”, we ensure that we
will provide the JasperReport compiler with a Hashtable having an entry with
key=”parameterName” and value=”parameterValue” prior to the compilation of the
report.
Similarly, by defining a “Report field”, we ensure
that resultset obtained by “Report Query” will contain a column named “fieldName”.
Variables are of two types:
builtin variables or non-builtin variables. You will see examples of both in a
few moments.
First, insert a static text as the title of report.
Specify title to be “Employee Work Hours Report”. Using geometric
shapes, the appearance of the report can be enhanced. However, for the time
being, we won’t bother with the aesthetics.
To add a Group to the
report, press the “Groups” icon. On the opened dialog, press New. Specify group
name as “employee” and group expression as “$F{EmployeeName}”, press OK with
other fields in default values. You will see that two new bands are added as “employeeHeader”
and “employeeFooter”.
According to the instructions given above, obtain the design in Figure 4. Specify the Report Query through
Edit-Report query as
“SELECT
* FROM tutorial_table ORDER BY EmployeeName”.
“Order by” is necessary because we created “employee” group with
group expression “$F{EmployeeName}”
Figure 4: Final iReport Design
Creating the Final Report using JasperReports
After obtaining the jrxml file, it is only a few steps to
obtaining the final report. The following code segment taken from sample.java
given in Appendix B is all to get the final report viewed in a PDF viewer.
// First, load JasperDesign
from XML and compile it into JasperReport
JasperDesign jasperDesign = JasperManager.loadXmlDesign("path-to-your-jrxml-file\\sam
JasperReport jasperReport =
JasperManager.compileReport(jasperDesign);
// Second, create a map of
parameters to pass to the report.
Map parameters = new HashMap();
parameters.put("employeeChart",
createEmployeeChartImage());
// Third, get a database
connection
Connection conn = Database.getConnection();
// Fourth, create JasperPrint
using fillReport() method
JasperPrint jasperPrint = JasperManager.fillReport(jasperReport,
parameters, conn);
// You can use JasperPrint to
create PDF
JasperManager.printReportToPdfFile(jasperPrint,
"desired-path\\SampleReport.pdf");
// Or to view report in the
JasperViewer
JasperViewer.viewReport(jasperPrint);
// Or create HTML Report
JasperExportManager.exportReportToHtmlFile(jasperPrint,
"desired-path\\SampleReport.h
Figure
5: From XML to the final report by
JasperReports
PDF Report From JRXML File
JRXML file is a JasperReports Document. JRXML is the XML file format of JasperReport, which can be coded manually, generated, or created using a tools like IReport, JasperAssistant etcExecution of report from JRXML file will be very slow, as it need to compile before the execution
PDF Report From Jasper File
Jasper file is a compiled
format of JasperReports Document.
Execution of report from Jasper
file will be very fast, as it is pre-compiled
It is recommended for the production environment
This comment has been removed by the author.
ReplyDeleteHello Sajadh,
ReplyDeleteDeveloping new gen developer friendly BI framework with some extremely unique features. Would like to give you early access & love to hear your opinion. Please do let me know of how to reach out to you. Would be launching product in 4 weeks from now Also can you share your email-id so that we can more details about the product or you can send us the test mail.