Learning JasperReports

POSTED BY Chris on Nov 8 under programming

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

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.

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.

// compile master report
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.

Map parameters = new HashMap();
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.

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
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:

iReports subreport

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.

Name (required)

Email (required)

Website

Comments

5 Comments so far
  1. Emanuel Perez February 24, 2007 8:55 am

    very useful. Thanks

  2. Melina Perez November 24, 2007 3:20 pm

    Hi there…I Googled for chris perez band, but found your page about …and have to say thanks. nice read. Melina Perez

  3. Lee Kwan Yew January 6, 2008 9:40 pm

    AWESOME! Very useful.

  4. Goh Chok Tong January 8, 2008 5:26 pm

    Yes Ah Lee, I couldnt have said it better. This help is indeed useful.

    Thanks a lot!

  5. joseph mbugua September 10, 2008 12:56 am

    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….

Copyright boggy time | Powered by WordPress | Using the GreenTech Theme