Sample 1: Executing a template based report

I will not begin with theory, these can be found in the JasperReports portal (JasperForge).

This first sample, implements a window in order to show the result of executing a template based report (I known, this is a tutorial for the JR API), I think that is a good start point to review code about loading a template, compiling, filling and last exporting the report to some format. Notice that I will only put small fragments of code, the complete code can be reviewed downloading the sample at the page end.

In order to execute the sample, I write an “Ant” task, you only need to open the folder of the script (build.xml in the root of the project) and execute the command “ant”. If you want to use some IDE, the project can be loaded in it, the required libraries are in the “lib” folder and the main class is: “”.

Notice that the path to the report template is in the “” file, if required, change this path in order to run the sample.

Lets start, the class that makes the interesting work is “HelloWorldReportExecutor”, this class contains the code to execute several tasks, lets see the most important:

public String executeReport(){
   JasperDesign jasperDesign=loadTemplate();
   JasperReport jasperReport=compileReport(jasperDesign);
   JasperPrint jasperPrint=fillReport(jasperReport);
   String res=exportReport(jasperPrint);

In the snippet we can see the steps to execute the report.

First, we load the template, and create a “JasperDesign” object structure, this structure is our report design represented by the template. Lets see two lines of the “loadTemplate” method:

   File templateFile=new File(templatePath);
   JasperDesign jasperDesign= JRXmlLoader.load(templateFile);

We create a “File” object with the template path, and on order to load this xml file, the “JRXmlLoader” class is used. The “JasperDesign” object contains all the template defined configurations, is equal to have the template as an objects structure. In following samples, we will create the “JasperDesign” structure using the JR API.

The second step, is to compile the report (a temporal directory is defined before, this is required for the process). Lets see the following code:

   JasperReport jasperReport= JasperCompileManager.compileReport(jasperDesign);

The “JasperCompileManager” class help us to compile the report design, if some design error was made in the template, we will notice it here. The “JasperReport” object is a structure similar to “JasperDesign” but, in this case, it contains the compiled and verified report structure.

With the “jasperReport” object, we can fill the report with data, this is made by means of:

JasperPrint jasperPrint =JasperFillManager.fillReport(
                                new HashMap(),
                                new JREmptyDataSource());

The “JasperFillManager” class help us to fill the report with data, it need as parameters: the compiled report structure, a report parameters map, and a “DataSource” (as our sample don’t need any data, we use an instance of “JREmptyDataSource”). In the report parameters map, we need to put the values of the parameters used in the report ($P{}).

The “JasperPrint” class, is another structure, it contains the report and its data (the filled report), is ready to be showed, stored, or exported.

Following, the last step, how to export the report:

JRAbstractExporter exporter=new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
StringBuffer stringBuffer=new StringBuffer();
exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, stringBuffer);

First, we create the exporter, in this case a “JRRtfExporter” or an Rtf exporter. As you know (I hope) there is several exporters in JR, e.g. Pdf, Xls, Html, etc.

Is time to configure the exporter with some parameters: the structure to be exported (“jasperPrint” object), the file encoding, and the result container (in this case, a StringBuffer, in order to create a String with it).

Next, calling the “exportReport” method, we get an “StringBuffer” with the report exported in Rtf format, who will be showed in the text area in the sample application window.

When you execute the sample application, you will see the generated report in the text area, some strange characters are showed too, I think this should be because the Swing component I am using to render it, don’t support some Rtf contents.

And voilà, here ends the first sample, I hope this can help you, any question or comment, please write me.

Thanks for reading.

Here is the code for this first sample, is hosted in sourceforge: jRTutorialSample1


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s