Using JasperReports With An ArrayList of Objects Part 2: Using the Report In A Java Application

Introduction

In part 1, I covered how to create a JasperReport report using iReport. That report's data source is an ArrayList of Person objects. Each Person object also has an ArrayList of Phone objects. In part 2, I explain how to use the JasperReport report in a Java application. You can download all the source code (Eclipse Java project).

Creating A Filled Report

In part 1, I created a main and sub-report and then compiled those reports into two .jasper files stored in c:\jasperreports\. JasperReports uses the .jasper files to fill with data and create a .jrprint file.

To give your Java application access to the JasperReport classes, you'll need to include these jar files:

jasperreports-3.0.0.jar (located in \jasperreports-3.0.0\dist\ folder)

commons-beanutils-1.7.jar (located in \jasperreports-3.0.0\lib\ folder)

commons-collections-2.1.jar (located in \jasperreports-3.0.0\lib\ folder)

commons-logging-1.0.2.jar (located in \jasperreports-3.0.0\lib\ folder)

itext-1.3.1.jar (located in \jasperreports-3.0.0\lib\ folder)

poi-3.0.1-FINAL-20070705.jar (located in \jasperreports-3.0.0\lib\ folder)

So step 1 is to fill the .jasper file with data. These statements do that:



/*
* Setup the parameters and their values
* needed by the .jasper file
*/

Map parameters = new HashMap();
parameters.put("SUBREPORT_DIR", "c:/JasperReports/");
JasperFillManager.fillReportToFile("C:/JasperReports/contacts.jasper", parameters,
new JRBeanCollectionDataSource(TestPerson.getBeanCollection() ) );

The first two statements create a Map containing the parameter name (the key) and the parameter's value (the value). For this JasperReport we need to send it the value of the subreport_dir (the directory where the sub-report is stored).

The we can use the JasperFillManager class's fillReportToFile method to fill the .jasper file with data. This method takes three arguments, the .jasper file (we created this using iReport), the parameters to send to that .jasper file, and the data source.

Note that our data source is using the JRBeanCollectionDataSource class, which has a constructor that takes an ArrayList of bean objects. We create that ArrayList by calling the static getBeanCollection method of our class TestPerson. This was the same data source we used in part 1 to create our report in iReport.

Converting A Filled Report to A PDF

The following code uses the filled report, which is stored in contact.jrprint, to create a PDF.



JasperExportManager.exportReportToPdfFile("C:/JasperReports/contacts.jrprint");

In folder c:\jasperreports\ should now be a PDF named contacts.pdf containing all the data returned by the TestPerson.getBeanCollection method.

Converting A Filled Report to An Rich Text Format (RTF) File

To create an RTF file (which can be opened by Word or other word-processing software) use the following statements.


File sourceFile = new File("C:/JasperReports/contacts.jrprint");
            
JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);

File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".rtf");

JRRtfExporter exporter = new JRRtfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());

exporter.exportReport();

The above code creates a JasperPrint object using our .jrprint file (which is filled with data from our data source). Then using a JRRtfExporter object it exports the .jrprint file to a RTF file with the same name but extension of .rtf.

After running the above code you should have a contacts.rtf file in the c:\jasperreports\ folder.

Converting A Filled Report to An Excel File

To create an Excel file use the following statements.



destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls");
            
JRXlsExporter xlsExporter = new JRXlsExporter();

xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);

xlsExporter.exportReport();

The code above is similar to how we created the RTF file, except it uses class xlsExporter to export the .jrprint file to an Excel file type.

Summary

The hardest part of using JasperReports is creating the report. Once you have a report created, you can fill that report with data from a data source. In this example the data source is a collection of Person objects. There are other types of data sources including databases.

Once you have filled your .jasper report (which was created by compiling the .jrxml file in iReport) and created the .jrprint file, you can use JasperReport to convert that .jrprint file to various file types including PDF, RTF, and Excel.

To learn more about JasperReports and iReport, check out the references below.

References

  1. JasperReports
  2. JasperReports Community Edition
  3. JasperReports Documentation
  4. iReport
  5. iReport Documentation
  6. Example source code in Eclipse project
  7. iReport jrxml source files
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Great job, the information provided was a great help especially when there is a lack of online documentation concerning Jasper Report . You saved a lot of of hours of work. Thanks a lot.
# Posted By Ramin Dutt | 9/15/08 11:44 AM
Well put article and easy to follow even with no previous experience. Thanks for the help!
# Posted By Siya | 10/13/08 6:40 AM
Thanks a lot, very good example, full, clean and easy to test. Good job
# Posted By neo | 11/13/08 9:34 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner