Monday, May 26, 2014

Jasper Report with Eclipse

Here we learn Step by Step to create jasper Report with eclipse, iReport with the help of java bean

Step 1: First Download  iReport-5.5.0-windows-installer and jasperreports-5.5.0 remember both version must be same like in this case both are 5.5.0

Step 2: Extract the jar file after extracting you find 










step 3: Open eclipse create java project then create folder with name library and copy all the files 
from lib and dist folder and add to the build path.

step 4: Create java bean with setter and getters if there is a field which is a object then create seperate class for the field . If a field is occur more than once then declare that field as a collection
like List.

                                            Address.java

package bean;

public class Address {
private String city;
private String country;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}

}

                                                 Employee.java

package bean;

import java.util.List;

public class Employee {
private int id;
private String name;
private List<Address> address;

public List<Address> getAddress() {
return address;
}
public void setAddress(List<Address> address) {
this.address = address;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}



}

step : 5 Create a factory class which contain a static method whose return type is Collection that return objects of your bean class.

                                                         EmployeeImpl.java


import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import java.util.List;





import bean.Address;

import bean.Employee;



public class EmployeeImpl {


public static Collection<Employee> getEmployee()
{
List<Employee>employees = new ArrayList<Employee>();
Employee e= new Employee();
e.setId(1);
e.setName("Aditya");
List<Address> addresses =new ArrayList<Address>();
Address a =new Address();
a.setCity("Ashok Nagar");
a.setCountry("India");
Address a1 =new Address();
a1.setCity("Ashok Nagar");
a1.setCountry("USA");
addresses.add(a);
addresses.add(a1);
e.setAddress(addresses);
employees.add(e);
return employees;
}
}


step : 5 Export this project as .jar

step :6 Now open ireport click on tools-->options---> classpath-->Add jar  Attach your jar file tick and press ok.

step :7 Click on Report Datasources-->New-->java bean set Datasource-->Click next
then give complete name of your factory class and your static method




then click on test a dialog box is open with a successfully message click save.

Step: 8 Click file-->New-->Blank A4-->Open this tempelate-->Give Report Name to Employee




Click next--> Finish.

Step: 9 Click  (near Preview)



Click java bean data source give name of your bean class click read attribute then select all except class click add selected field click preview data here you see all your value 

Click ok

Step: 10 Then all the fields are come in field add id,name but not Address because it is a object for this we must create a subreport which is to be added in Employee.




Step 11: From Pallette add drag subreport in the details band a dialog box is open check create a new report-->next-->blank A4-->next---> 



Data source is same as Employee because it contain the factory method.
Click next-->next-->Give sub report a name ex Employee_Address click finish

Step:12 Click on your Employee report then click on sub report so that on the right side properties of subreport is open change connection type to use a data source expression



 Click on data source expression a dialog box is open that contain all the properties id,name,address use
 new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{address})



Click Ok.

Step :13 Then repeat step 9 but this time give name of Address Bean and not preview data because if you preview data it gives a error 





Step :14 Then attach your Country, City  to your subreport compile it but preview your Employee report if you preview your sub report it gives an error




Thats it you can see your report but if you want to call it from your java code then some additional steps need to be done.

Step 15: Change Language of both your report from groovy to java Click on Employee and Employee_Address in Report Inspector then on right hand side their is a language drop down change groovy to java




Step 16: Add both Employee.jrxml and Employee_Address.jrxml to your eclipse project inside jrxml project .And create a Class with main() 

public static void main(String[] args) {

HashMap<String, Object> hm = new HashMap<String, Object>();
JRDataSource  ds = new JRBeanCollectionDataSource(EmployeeImpl.getEmployee());
        JasperReport jasperReport;
        JasperPrint jasperPrint;
        try {
            jasperReport = JasperCompileManager.compileReport("jrxml/Employee.jrxml");
            jasperPrint = JasperFillManager.fillReport(jasperReport, hm, ds);
            JasperExportManager.exportReportToPdfFile(jasperPrint, "simple_report.pdf");
        } catch (JRException e) {
            e.printStackTrace();
        }

}

Run the class refresh your project and yipeeeee their is a pdf with name simple_report.pdf

Project Structure









Hope this help...


No comments:

Post a Comment

Total Pageviews

Like Us on Facebook

Number Of Unique Visitor

Free counters!