Jasper Report For Beginners
Jasper Report For Beginners
Jasper Report For Beginners
beginners
Jasper Reports is an open source reporting library which is available for windows and linux platforms. The Jasper report
support many output formats like Excel, CVS, XML, PDF, HTML , RTF, Text etc..
It also supports many data sources like JDBC, Hibernate, EJB, Java Bean etc.,
The Jasper Report can be used on the web and also it can be used from simple Java application. It vastly supports the
presentation formats like Dashboards, tables, cross tabs, and charts. Basically open and standards based Java and Xml.
Supported by the most active community of report designers and developers.
The Jasper report simplifies the solution by providing support for multiple data sources, sub reports and cross tab reports.
The built in virtualization capability of Jasper enables efficiently rendering large reports with limited available disk storage
resources.
Templates
Jasper uses jrxml templates to build and render the report. The jrxml is a standards based xml which can be dynamically
generated using the open source tool iReport. iReport is the tool which can be used to generate the jrxml templates which
can be connected to various data sources like JDBC, XML, Hibernate etc., Jrxml file contain the report structure information.
Report Basics
There are some basic steps involved in creating a Jasper Report. We will discuss a report generation steps for Excel output.
We will discuss how each of the above operations can be done using a simple report. Our report uses Java Bean as the
data source and generates multiple sheet Excel Report.
Let us briefly discuss about the design of a simple template using iReport tool. iReport provides options to include Title,
Page Header, Column Header, Detail, Column Footer etc., as per our requirement. Also there are options like Page
Number, Total Pages, Page x of y, Sum, Current Date and percentage which can be inserted where ever needed. In our
report template we just have the Column Header and details.
If you scrutiny the sample jrxml file given below, you will find the <jasperReport> tag with attributes like name, column count,
page width, margins etc., which are specific to a report. Title and Page Header tags specify Title and Page Header
information.
Column Headers are defined using an element called ‘Static Text’. For each Static Text element the cell level properties are
defined using the tag ‘Report Element’. The cell type like Transparent or Opaque can be assigned using the attribute ‘mode’
of the ‘reportElement’ tag. Attributes x, y, width and height are so important for positioning and size of the cell. If you make a
small mistake in any of the value, the entire report structure will be misaligned. You can specify a background color using
‘backcolor’ attribute but if you want to specify a background color, you must have the mode as Opaque.
It is not necessary to know the tags and attributes of the jrxml file, but if you know them basics it will be easy to fine tune the
report and you will be able to identify the problem in case of any issues.
So far we have completed how the Header row is built in jrxml template. Now we will have a look at how the detail rows are
built.
We will be using the ‘detail’ tag to display the detail rows. We use ‘textField’ tag and ‘textFieldExpression’ tag is used in
which the field name is passed. For example ‘personName’ is the field we are using. Firstly you need to create a field by that
name specifying the type of the field.
Later in the ‘textFieldExpression’ tag you need to specify the field name as shown below.
<textFieldExpression class=”java.lang.String”><![CDATA[$F{personName}]]></textFieldExpression>
Go through the jrxml file details given below and try to understand the structure and usage of various tags and attributes. A
faster way to learn is by experimenting it using iReport. When ever you change some thing using iReport, the jrxml file is
getting modified.
import java.util.HashMap;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;public class CompileReport {
public static void main(String[] args) {
//JasperReport jasperReport;
JasperPrint jasperPrint;
try {
JasperCompileManager.compileReportToFile(“C:\\eclipse\\workspace\\NewJasper\\src\\FirstJasper.jrxml”);
JasperCompileManager.compileReportToFile(“C:\\eclipse\\workspace\\NewJasper\\src\\SecondJasper.jrxml”);}
catch (JRException e) {
e.printStackTrace();
}
}
}
In the above example we have two jrxml templates, one for each sheet in our excel report. We assume that we have
completed the design of both of our jrxml templates. The next step is to compile them.
JasperCompileManager.compileReportToFile(“C:\\eclipse\\workspace\\NewJasper\\src\\FirstJasper.jrxml”);
Just call the method compileReportToFile of JasperCompileManager by passing the jrxml file name (FirstJasper.jrxml). The
report gets compiled and a new FirstJasper.jasper file is generated.
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;public class ExportDemoExcel {
public static void main(String args[]){
try {
Object[] objArr = new Object[3];
objArr[0]= new DemoBean(“Ramesh”,”USA”,”CA”, “San Jose”, “Jasper Report”, 2, “San Jose”, “Expert”);
objArr[1]= new DemoBean(“Suresh”,”USA”,”CA”, “San Jose”, “Jasper Report”, 2, “San Jose”, “Expert”);
objArr[2]= new DemoBean(“Paul”,”USA”,”CA”, “San Jose”, “Jasper Report”, 2, “San Jose”, “Expert”);
JasperPrint jp1 = JasperFillManager.fillReport(“C:\\eclipse\\workspace\\NewJasper\\src\\FirstReport.jasper”, new
HashMap(), new JRBeanArrayDataSource(objArr));
JasperPrint jp2 = JasperFillManager.fillReport(“C:\\eclipse\\workspace\\NewJasper\\src\\SecondReport.jasper”, new
HashMap(), new JRBeanArrayDataSource(objArr));
List<JRPrintPage> pages = new ArrayList<JRPrintPage>(jp2.getPages());
int i=1;
for(int count=0;count<pages.size();count++){
jp1.addPage(i, (JRPrintPage)pages.get(count));
i++;
}JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.
IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.TRUE);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp1);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
“e:/demo1.xls”);
exporter.setParameter(JRXlsExporterParameter.SHEET_NAMES, new String[]{”Personal Information”, “Skills”});
exporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS,
Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.OFFSET_X, 0);
exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, true);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, false);
exporter.exportReport();
} catch (JRException e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace();
}
}
}
We are using the Java Bean as the data source. The next operation will be filling the report.
Just call the ‘fillReport’ method of the ‘JasperFillManager’ class by passing the jasper file name and the data source. In our
case the data source is an Object Array containing the Java Beans. Make a note that the field names which we use in the
template should match the Java Bean attribute, otherwise it will through an error. When the fill operation is successful, file is
generated with the jrpint extension. In our case it will be FirstReport.jrprint.
Export Excel
Before exporting a report we are passing some export parameters as shown above. This is because by default these
parameters have some initial values and that may affect the appearance of our report. The report file is exported and stored
in the specified path.