Jasper Report Example by Passing ArrayList collection Datasource by Using JRBeanCollectionDataSource with TIBCO Jasper Report 6.0
Here we have Second example of jasper Report by Arraylist collection datasource .
First of all create a jasper design for you report
here we using the list collection as data source so we have create the field in jasper report
in order to print the collection bean value
REMEMBER : - field name must be same as collection bean value .
we have a bean class name EmployeeBean like as
package confonet.efiling.service.common;
public class EmployeeBean {
private String empId;
private String name;
private String companyName;
private String desgination;
private String salary;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getDesgination() {
return desgination;
}
public void setDesgination(String desgination) {
this.desgination = desgination;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
}
and my jasper report design is following .
my main java class for create the data for report and generating report .
EmployeeJasperReport .java
package confonet.efiling.service.common;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
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;
public class EmployeeJasperReport {
public static void main(String[] args) {
//create a HashMap for Passing data to jasper report
HashMap employeeReportMap = new HashMap<String, Object>();
EmployeeBean employeeBean1 = new EmployeeBean();
employeeBean1.setEmpId("0000");
employeeBean1.setName("Ambrish");
employeeBean1.setDesgination("Java Tech Lead");
employeeBean1.setCompanyName("NIC");
employeeBean1.setSalary("600000 RS ");
EmployeeBean employeeBean2 = new EmployeeBean();
employeeBean2.setEmpId("1111");
employeeBean2.setName("Depp");
employeeBean2.setDesgination("Java Developer");
employeeBean2.setCompanyName("NIC");
employeeBean2.setSalary("300000 RS ");
EmployeeBean employeeBean3 = new EmployeeBean();
employeeBean3.setEmpId("2222");
employeeBean3.setName("Gaurav");
employeeBean3.setDesgination("Java Developer");
employeeBean3.setCompanyName("NIC");
employeeBean3.setSalary("30000 RS ");
EmployeeBean employeeBean4 = new EmployeeBean();
employeeBean4.setEmpId("3333");
employeeBean4.setName("J K Saini");
employeeBean4.setDesgination("Java Developer");
employeeBean4.setCompanyName("NIC");
employeeBean4.setSalary("20000 RS ");
List reportDataList = new ArrayList<EmployeeBean>();
reportDataList.add(employeeBean1);
reportDataList.add(employeeBean2);
reportDataList.add(employeeBean3);
reportDataList.add(employeeBean4);
// Creating the data Bean Collection Data Source for passing to jasper report design
JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(reportDataList);
try {
// Give the path of report jrxml file path for complie.
JasperReport jasperReport = JasperCompileManager.compileReport("C:\\Users\\preeti_mehta\\JaspersoftWorkspace\\MyReports\\EmployeeReportjrxml.jrxml");
//pass data HashMap with compiled jrxml file and a empty data source .
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, employeeReportMap,beanCollectionDataSource );
// export file to pdf to your local system
JasperExportManager.exportReportToPdfFile(jasperPrint, "D:/jasperoutput/empReport "+new Date().getTime()+".pdf");
System.out.println("Printed----------------------");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
No comments:
Post a Comment