Learning JasperReports
At work, I've been tasked to do some reporting for a Java application that we have and I've been trying to learn the JasperReports library. Unfortunately, the existing documentation/tutorials that I've found have been somewhat limited. For example, I spent a few days pulling my hair out trying to understand how to get the subreports functionality working. I think I've figured out the basics now, so here is a simple tutorial.
Setup
- Download JasperReports 1.2.7
- Download IReport Designer 1.2.7
- I'm using Eclipse as my Java development environment, but you can use whatever you want.
Creating our project
In Eclipse, create a new Java project. To get everything compiled correctly, you need to have the following jars (from the JasperReports distribution) in your project classpath.
- jasperreports-1.2.7.jar
- commons-digester-1.7.jar
- commons-collections-2.1.jar
- commons-logging-1.0.2.jar
- commons-beanutils-1.5.jar
- itext-1.3.1.jar
Also, create a reports subdirectory for my reports to go into. For this simple project, I want to display in a subreport a list of PersonBean objects.
iReport is a visual reporting tool for creating JasperReports which frees you from having to muck with the JasperReports jrxml files. It's free and so far seems pretty decent.
Open it up, and go to the File menu > New Document. I created a document called 'master' which will generate a master.jrxml file for you. Use the 'Static Tool' button in the toolbar to add some text to the detail band.
To embed a subreport, Go to Edit menu > Insert Element > Subreport and then clicked on a spot in the detail band. This will bring up the Subreport wizard. Choose to create new subreport and accept all the defaults.
Back in the IDE, you need to write some code to compile the jrxml files.
JasperReport jasperReport = JasperCompileManager.compileReport(
"reports/master.jrxml");
// compile subreport
JasperReport subReport = JasperCompileManager.compileReport(
"reports/subreport.jrxml");
There are two parameters to pass in, the subreport and a datasource. In this example we are using a net.sf.jasperreports.engine.data.JRBeanArrayDataSource object which accepts an array of JavaBean objects.
parameters.put("subreport", subReport);
PersonBean[] people = new PersonBean[] {
new PersonBean("Bob", 21, "New York"),
new PersonBean("Chris", 27, "California"),
new PersonBean("Sally", 30, "Texas")
};
JRBeanArrayDataSource ds = new JRBeanArrayDataSource(people);
parameters.put("subreportData", ds);
After that we fill the report and output the file to pdf.
parameters, new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint,
"reports/myreport.pdf");
Now we go back to iReport and finish up our reports. In our subreport, add a columnHeader band, and then add the three column titles (name, age, location) to it. In the detail band, add the corresponding three fields ($F{name}, $F{age} and $F{location}. Make sure you set the age field expression class to java.lang.Integer and declare fields (View > Fields). You should have something similar to this:

Switch to the master report, right click on the subreport image and select 'Properties'. In the Subreport tab, choose 'Use datasource expression' in the combo and input '$P{subreportData}' as your expression. Switch to the Subreport (other) tab, select 'net.sf.jasperreports.engine.JasperReport' and input '$P{subreport}' as your expression. These correspond to the two parameters we passed in above in the Java code. Finally declare the two parameters (View > Parameters).
To actually generate the report, go back to your IDE and run the java code. A sparkly new pdf awaits.
Leave a Comment
If you would like to make a comment, please fill out the form below.
Author Name
A couple of lines for short desc
ription of the author of this
blog A couple of lines for short
description of the author of
this blog description of the
author of this blog
description of the author

very useful. Thanks
Hi there…I Googled for chris perez band, but found your page about …and have to say thanks. nice read. Melina Perez
AWESOME! Very useful.
Yes Ah Lee, I couldnt have said it better. This help is indeed useful.
Thanks a lot!
i have googled for 4 days without finding a solution to my problem (subreports issue with jdeveloper) but reading your post……you are a saviour.thanx….