Data reporting is the process of collecting and submitting data to authorities entrusted with compiling statistics. Accurate data reporting gives rise to accurate analysis of the facts on the ground; inaccurate data reporting can lead to wrong decision making. The common problems in data reporting can be due to change in business logic or if there is need for enhancement of the core logic of the report, it can be due to sub-reports or multiple charts. To overcome these overheads, many frameworks, tools, libraries and third party applications were introduced. JasperReports is one of them.
Why JasperReports?
JasperReports is an open source Java reporting engine and does not have its own syntax expression. Therefore it cannot run on its own, and needs to be embedded into another client or server-side Java application. It is meant for Java developers who need to add reporting capabilities to their applications. As it is Java based, it can be run on any platform that supports Java (JDK 1.3 and above). All the JasperReport’s functionalities are gathered in a single JAR file, jasperreports-x.x.x.jar. The main purpose of JasperReports is to create page oriented, ready to print documents in a simple and flexible manner.
Features of JasperReports
Some of the significant features of JasperReports are:
- Flexible report layout.
- Can present data either textually or graphically.
- Developers can supply data in multiple ways.
- Can accept data from the multiple data sources.
- Can generate watermarks
- Can generate sub reports.
- Capable of exporting reports in a variety of formats.
Life cycle of JasperReports
- Designing the report− In this step, we create the JRXML file, which is an XML document that contains the definition of the report layout. We can use any text editor or iReportDesigner or JasperSoft studio to manually create it. If we use iReportDesigner or JasperSoft studio we can visualize the layout of the report and thus the real structure of the JRXML can be ignored. Here we have used JasperSoft studio to design the report.
Report design template
- Title – Title contains the title of the report and appears only once at the beginning of the report.
- PageHeader – PageHeader appears at the top of the page. It may contain date, time, name of the organization etc.
- ColumnHeader – ColumnHeader contains the specific field names like Name, Place etc.
- Detail – Here the entries of specific fields are included.
- ColumnFooter – ColumnFooter may contain summary of the specific field.
- PageFooter – PageFooter appears at the bottom of the report. It may contain count of page.
- Summary – It contains information inferred from “detail” part, for example, after listing the number of hours, worked by each author, total hours worked by each author can be put in visual chart like pie chart, graph, etc. for better comparison.
Template designed for my sample report
- Compiling the report− In this step, JRXML is compiled in a binary object called a Jasper file (*.jasper). This compilation is done for performance reasons. Jasper files are what you need to ship with your application in order to run the reports.
- Executing the report (Filling data into the report)− In this step, data from the application is filled in the compiled report. The class net.sf.jasperreports.engine.JasperFillManager provides necessary functions to fill the data in the reports. A Jasper print file (*.jrprint) is created, which can be used either to print or export the report.
- Exporting the report to desired format− In this step, we can export the Jasper print file created in the previous step to any format. As Jasper provides various forms of exports, hence with the same input, we can create multiple representations of the data.
Code Snippet
Connection con = null;
try {
// connection to the database
String url = “jdbc:mysql://localhost:3306/registers”;
String username = “username”;
String password = “password”;
Class.forName(“com.mysql.jdbc.Driver”);
con = (Connection) DriverManager.getConnection(url, username,
password);
String queryString = “select * from users”;
java.sql.Statement stmt = con.createStatement();
ResultSet resultset = null;
resultset = stmt.executeQuery(queryString);
Map parameters = new HashMap();
parameters.put(“ReportTitle”, “User Details”);
JRResultSetDataSource jasperReports = new JRResultSetDataSource(
resultset);
// compiling the report
JasperReport sourceFile = JasperCompileManager .compileReport(“/home/usr/sample/userReport.jrxml”); // give the fully qualified path of the .jrxml file
//Executing the report
JasperPrint jasperPrint = JasperFillManager.fillReport(
sourceFile, parameters, jasperReports);
//Exporting the report to docx file
JRDocxExporter jasperDocx = new JRDocxExporter();
ExporterInput exportInput = (ExporterInput) new SimpleExporterInput(
jasperPrint);
jasperDocx.setExporterInput(exportInput);
OutputStreamExporterOutput jasperOutputDocx = new SimpleOutputStreamExporterOutput(
“/home/usr/sample/userReport.docx”); //specify the path where we want to save the report
jasperDocx.setExporterOutput(jasperOutputDocx);
SimpleDocxExporterConfiguration simple = new SimpleDocxExporterConfiguration();
jasperDocx.setConfiguration(simple);
jasperDocx.exportReport();
System.out.println(“The jrxml is converted into docx”);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Required Libraries are
- commons-beanutils-1.9.0
- commons-collections-3.2.1
- commons-digester-2.1
- dynamicreports-core-3.0.0
- jasperreports-6.1.0
- commons-logging-1.1.1
- mysql-connector-java-5.1.