Anda di halaman 1dari 20

Jasper Reports

By
Elsayed Hassan

Teaching Assistant
Faculty of computers and information
Cairo University

Software Engineer
ITWorx Egypt
Outlines
• Introduction
• Getting Started
• Report Templates
• Reporting Filling Process
• Handling generated reports
• Reporting Data
• Report Sections
• Sub Reports
• Datasets
• Charts
• Crosstabs
Introduction

• JasperReports is a powerful java open source reporting


tool

• Has the ability to deliver rich content onto the screen,


to the printer or into PDF, HTML, RTF, XLS, CSV and
XML files.

• Uses report templates (JRXML OR programmatically


created template )
Getting Started
1- How To install jasper reports
All its reporting functionality is available in single JAR file
http://sourceforge.net/project/showfiles.php?group_id=36382&package_id=28579

2- Requirements
• Java Virtual Machine (JRE 1.3 or higher)
• XML (JAXP 1.1 XML Parser )
• Jakarta Commons Digester Component (version 1.7 or later)
http://jakarta.apache.org/commons/beanutils/
• Jakarta Commons Collections Component (version 2.0 or later)
http://jakarta.apache.org/commons/collections/
• Jakarta Commons Logging Component (version 1.0 or later)
http://jakarta.apache.org/commons/logging/
• JDBC (JDBC 2.0 Driver)
• PDF
iText - Free Java-PDF library by Bruno Lowagie and Paulo Soares (version 1.01 or later)
http://www.lowagie.com/iText/
• XLS : Jakarta POI (version 2.0 or later) http://jakarta.apache.org/poi/
• Charts : JFreeChart (1.0.0 RC1 or later)
http://www.jfree.org/jfreechart/
Getting Started (Cont.)

2-Report Life Cycle

JasperPrintManager

Screen
JasperFillManager Print
Printer
parse Compile
JRXML JasperDesign JasperReport JasperPrint PDF
Fill Export
HTML
JRXmlLoader JRCompiler
XML
JasperExportManager
JasperCompileManager
Report Templates
1- Creating report templates
Two ways
- creating net.sf.jasperreports.engine.design.JasperDesign objects directly
using the API

- editing JRXML files using either a simple text editor, an XML editor or a
specialized GUI tool
2- Compiling report templates
Converting from JRXML file or JasperDesign object to JasperReport object

parse Compile
JRXML JasperDesign JasperReport

JRXmlLoader JRCompiler

JasperCompileManager
Report Templates (cont.)
3- Report Compilers
Those Java report compilers come in different flavors depending on the actual Java compiler
used to compiled the on-the-fly generated class

• net.sf.jasperreports.engine.design.JRJdtCompiler
• net.sf.jasperreports.engine.design.JRJdk13Compiler
• net.sf.jasperreports.engine.design.JRJdk12Compiler
• net.sf.jasperreports.engine.design.JRJavacCompiler
• net.sf.jasperreports.engine.design.JRJikesCompiler

To simplify the report compilation process, the


jasperReports API offers a façade class for compiling
reports. This is the
net.sf.jasperreports.engine.JasperCompileManager
Report Templates (cont.)

4- Report Elements

1- Text elements
1.1 Fonts and Unicode support
1.2 Static texts
1.3 Text fields
2 Graphic elements
2.1 Lines
2.2 Rectangles
2.3 Ellipses
2.4 Images
2.5 Charts and graphics
3 Box elements
4 Hyperlinks and bookmarks
5 Elementgroups
6 Frames
Report Filling Process

• The main objective for this piece of software


• There are tree things that should be supplied to
the report filling process as input:
– report template (in the compiled form);
– parameters;
– data source.
JasperFillManager

JasperReport JasperPrint
Fill

• The output of the report filling process is always a


pixel-perfect document, ready for viewing,
printing or exporting to other formats
Report Filling (cont.)

• Asynchronous report filling


– net.sf.jasperreports.engine.fill.AsynchonousFillHandle
• cerateHandle(jasperReport,paramMap,dataSource)
• startFill()
• cancelFill()
• addListener()
– net.sf.jasperreports.engine.fill.AsynchronousFilllListener
• reportFinished()
• reportFillError()
• reportCancelled()
Reporting Data
• The library does not make any assumptions about
where the data it uses for filling the reports comes
from.
• Relies on two things
– Report Parameters
named values sent to the report template as java.util.Map
– Report Data Source (Two Different Scenarios)
1- An implementation of JRDataSource Interface
- Has just two methods next() , getFieldValue()
- Some implementations provided
» collections or arrays of JavaBeans
» XML Data
» Swing Table Model
» JDBC Result Set
Report Data Source (cont.)

2- Using java.sql.Connection as a parameter


for the filling method
– most of the time reports are generated using
data that comes from tables found in relational
databases

– Users have the possibility to put the SQL query


needed to retrieve the report data from the
database, in the report template itself.
Handling Generated Reports

1. Loading and saving reports


The JasperPrint object can be serialized and deserialized using two utility
classes
• net.sf.jasperreports.engine.util.JRSaver
• net.sf.jasperreports.engine.util.JRLoader

• Viewing Reports
• Jasper Reports has a Swing-based built in viewer that can be
integrated in any java application net.sf.jasperreports.view.JRViewer
• This built in viewer can be customized by extending it and adding any
required customization

3. Printing Reports
net.sf.jasperreports.engine.JasperPrintManager
I. JasperPrintManager.printReport(myReport, false);
III. JasperPrintManager.printPages(myReport, 4, 10, true);
Handling Generated Reports (cont.)

1. Exporting Reports
Transform from JasperReport object format to more popular formats like
PDF,HTML,RTF,XLS
 The main class for exporting is
net.sf.jasperreports.engine.JasperExportManager
has methods for exporting to PDF,HTML,XML
 Also there exist specialized exporters like
 JRXlsExporter
 JRHtmlExporter
 JRRtfExporter
 JRXmlExporter
 JRTextExporter
 JRCsvExporter
All this classes are implementations for net.sf.jasperreports.engine.JRExporter
Interface
Reporting Data
1. Expressions
2. Parameters
3. Data Sources
• JDBC data source
• JavaBeans data sources
• Map-based data sources
• TableModel data source
• Empty data sources
• Rewindable data sources
4. Report Query
5. Fields
6. Variables
1. Variable Incrementer
 net.sf.jasperreports.engine.fill.JRIncrementerFactory
 net.sf.jasperreports.engine.fill.JRIncrementer
Report Sections
1. Main Sections
 Title
 Page header
 Column header
 Detail
 Column footer
 Page footer
 Summary
 Last page footer
 Background

3. Attributes
 Band height
 Preventing band split
 Skipping bands

5. Data Grouping
SubReports

 Subreport parameters

 Subreport data source

 Returning values from subreports


Datasets

A dataset is a concept that lies somewhere


between a data source and a subreport

 Main dataset

 Subdatasets

 Dataset runs
Charts
 Jasper reports uses JFreeChart library for
rendering chart elements
 Chart Properties
 Display Chart Legend
 Evaluating Charts
 Chart title and subtitle
 Chart Customizer
 Chart datasets
 Dataset runs
 Chart Plot
 Chat types
Crosstabs

 A crosstab is a special type of report element that summarizes


data into a two dimensional grid.
 Crosstab parameters
 Crosstab dataset
 Crosstab Measures
 Data grouping (bucketing)
 Row Groups
 Column Groups
 Bucket Expression
 Bucket Comparator
 Crosstab cells
 Detail crosstab cell
 Total crosstab cell