Search this Java developer blog:

JasperReports 5.0 tutorial with iReport 5.0

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

package com.mycompany.app.jasper;

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;
    }
    
    @Override
    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 com.mycompany.app.jasper.Person 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 Person.java. 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 Main.java class, you need to have the relevant Jasper jar files. Here is the pom.xml file with the relevant dependency jar files. 

<!-- Jasper reports -->
<dependency>
 <groupId>net.sf.jasperreports</groupId>
 <artifactId>jasperreports</artifactId>
 <version>5.0.1</version>
</dependency>
<dependency>
 <groupId>org.codehaus.groovy</groupId>
 <artifactId>groovy-all</artifactId>
 <version>2.1.1</version>
</dependency>
<dependency>
 <groupId>com.lowagie</groupId>
 <artifactId>itext</artifactId>
 <version>2.1.7</version>
</dependency>


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

package com.mycompany.app.jasper; import java.io.IOException; import java.io.InputStream; 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 net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.design.JasperDesign; 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
    {
        try
        {
            InputStream resourceAsStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(reportTemplateUrl);
            //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(
                            findReportData()));
            //view the report using JasperViewer
            JasperViewer.viewReport(jasperPrint);
        }
        catch (JRException e)
        {
            e.printStackTrace();
        }
    }
    
    private static Collection findReportData()
    {
        //declare a list of object
        List<Person> data = new LinkedList<Person>();
        Person p1 = new Person();
        p1.setFirstName("John");
        p1.setSurname("Smith");
        p1.setAge(Integer.valueOf(5));
        data.add(p1);
        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.

3 comments:

  1. Thank you this is help me to learn jasper

    ReplyDelete
  2. thanks a lot, keep posting :)

    ReplyDelete