Jun 29, 2013

JasperReports 5.0 tutorial with iReport 5.0

Step 1: Define a POJO class as defined below. This is the Java bean data source that is going to provide the data to the report.


public class Person
    private String firstName;
    private String surname;
    private Integer age;
    public String getFirstName()
        return firstName;
    public void setFirstName(String firstName)
        this.firstName = firstName;
    public String getSurname()
        return surname;
    public void setSurname(String surname)
        this.surname = surname;
    public Integer getAge()
        return age;
    public void setAge(Integer age)
        this.age = age;
    public String toString()
        return "Person [firstName=" + firstName + ", surname=" + surname + ", age=" + age + "]";

Step 2: Download Jaspersoft iReport 5.0.0, and execute the  ireport.exe from the iReport folder (e.g. C:\ireport\5.0.0\iReport-5.0.0\bin). You need to design the template now using iReport 5.0.0 and generate the person-template.jrxml, which will be used in Java as the template to generate the report.

Step 3: Tell iReport where to find the classes by defining the class path via Tools --> Option, and then select the "Classpath" tab.

Step 4: Create a new report via File --> New.

provide the name and path where you want to generate the template jrxml file.

Click on "Next" and then "Finish" to get the designer screen where you can add labels and text fields for the report.

Step 5: Add the column header for the report by dragging and dropping the "Static Text" for the column headers.

Step 6: Before you can map the Text Field with bean data source values, you need to bring in the Person java class we created in Step 1.Click on the little data source icon as shown below to get the pop up that allows you to define the bean and select the firstName, surname, and age and select on "Add selected fields" and then click on "OK".

Step 7: Now you can map these fields to the new "Text Field" that you drag and drop as shown below. This will be done in the "detail1" section.

Step 8: Now, you need to map each Text Field to the corresponding fields of You do this by Right-clicking and selecting "Edit expression" on the field.

Step 9: In the "expression editor", you can map the field. Remove the default $F{Field} and "double-click" on "firstName" to get $F{firstName} and click on "Apply" as shown below. 

Step 10: Map all the fields as shown below, and click on the icon that compiles the design to generate the person-template.jrxml (text) and person-template.jasper (binary) files. If there are any compile errors, you need to fix it and re-compile. You only need the jrxml file to generate report in Java.

Step 11: Before you write the class, you need to have the relevant Jasper jar files. Here is the pom.xml file with the relevant dependency jar files. 

<!-- Jasper reports -->

Step 12: Finally the that generates the report by combining the layout template person-template.jrxml and the data by executing the Main class.

package; import; import; import java.util.Collection; import java.util.LinkedList; import java.util.List; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import; import; import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

public class Main
    public static JasperDesign jasperDesign;
    public static JasperPrint jasperPrint;
    public static JasperReport jasperReport;
    public static String reportTemplateUrl = "person-template.jrxml";
    public static void main(String[] args) throws IOException
            InputStream resourceAsStream = Thread.currentThread().getContextClassLoader()
            //get report file and then load into jasperDesign
            jasperDesign = JRXmlLoader.load(resourceAsStream);
            //compile the jasperDesign
            jasperReport = JasperCompileManager.compileReport(jasperDesign);
            //fill the ready report with data and parameter
            jasperPrint = JasperFillManager.fillReport(jasperReport, null,
                    new JRBeanCollectionDataSource(
            //view the report using JasperViewer
        catch (JRException e)
    private static Collection findReportData()
        //declare a list of object
        List<Person> data = new LinkedList<Person>();
        Person p1 = new Person();
        return data;

Finally, the report produced looks like

Note: iText jar is required to generate PDF reports. If you want to generate excel report, you need additional dependency jar like POI.



Anonymous Anonymous said...

Thank you this is help me to learn jasper

5:59 PM, March 03, 2014  
Anonymous Anonymous said...

thanks a lot, keep posting :)

10:37 PM, August 07, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home