JasperReports

Introduction
JasperReports is a powerful open source reporting tool that has the ability to deliver rich content onto the screen, to the printer, or into PDF, HTML, XLS, RTF, ODT, CSV, TXT and XML files. It is entirely written in Java and can be used in a variety of Java-enabled applications to generate dynamic content. Its main purpose is to help create page-oriented, ready-to-print documents in a simple and flexible manner.

In addition to textual data, JasperReports is capable of generating professional reports including images, charts, and graphs. Some of the major JasperReports features include:
 * It has flexible report layout.
 * It is capable of presenting data textually or graphically.
 * It allows developers to supply data in multiple ways.
 * It can accept data from multiple datasources.
 * It can generate watermarks.
 * It can generate subreports.
 * It is capable of exporting reports to a variety of formats.

Links

 * JasperReports Project
 * iReport - Graphical report designer for JasperReports
 * JasperForge.Org - Jaspersoft community projects

Project Setup

 * Download the latest version of the JasperReports Project.
 * Add the files below to the libraries path of your application.
 * jasperreports-x.y.z.jar
 * commons-digester-x.y.jar
 * commons-beanutils-x.y.jar
 * jdt-compiler-x.y.z.jar (jrxml compiler)

If you want to export files to PDF, RTF or HTML you need the jar file of iText.

If you want to export files to Excel you need the jar file of poi-X.Y.Z-FINAL.jar.

Other libraries may be required in order to show charts, to use Hibernate, etc.

Graphical report designer for JasperReports
Use iReport to design JasperReports. If you have Netbeans, download the plugin version from the project website.

iReport is an easy-to-use, drag-and-drop WYSIWIG report designer with support for all data sources and custom sources and sub-reports, complex layouts, multiple data sources, and programmable dashboards.

API Overview
The figure below illustrates the JasperReports API overview:



The example below shows how to go through the API phases:

// First, load JasperDesign from XML and compile it into JasperReport JasperDesign jasperDesign = JasperManager.loadXmlDesign("BasicReport.xml"); JasperReport jasperReport = JasperManager.compileReport(jasperDesign); // Second, create a map of parameters to pass to the report. Map parameters = new HashMap; parameters.put("ReportTitle", "Basic JasperReport"); parameters.put("MaxSalary", new Double(25000.00)); // 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, "BasicReport.pdf"); // Or to view report in the JasperViewer JasperViewer.viewReport(jasperPrint);

Example 2: Compiling and displaying an empty JasperReport

try { String filename = Util.getResourcePath(this, "br/jaraujo/mibss/reports/rptPerformanceResults.jrxml"); JasperReport jasperReport = JasperCompileManager.compileReport(filename); JasperPrint jprint = JasperFillManager.fillReport(jasperReport, new HashMap, new JREmptyDataSource); JasperViewer.viewReport(jprint); } catch (Exception e) { System.out.println(e); }