Pages

Jasper Report with POJO Datasource

Jasper Report with POJO Datasource

Following codes should be use as client code. can be placed on your controller class.
ReportUtil class is the library developed by me. You can use it as "ReportUtil.createreport".
First Set classpath in Jasper report tools -->option -->add JAR
Second get fields from class.Click datasource icon,click javabean datasource tab,enter package name included class name.


 private final static String PATH_DOC="../server/default/deploy/complain-documents.war/report/";
 private final static String PATH_DOC_REPORT_SOURCE="../server/default/deploy/complain-documents.war/report-source/complainreport.jasper";
 private final static String REPORT_NAME="complain_log_";

Following code is the client code.code pass the values to method. Values includes parameters dataset and jasper file name.

 
   try {
    HashMap parameters = new HashMap();
    parameters.put("fromDate", viewdatestart);
    parameters.put("toDate", viewdateend);
    parameters.put("username", req.getUserPrincipal().getName());
    String pathReportJasperFile=PATH_DOC_REPORT_SOURCE;
    String part2="-"+new Date().getTime();
    String pathReportPDFFile=REPORT_NAME+part2+".pdf";
    ReportUtil.createreport(listViewLog, pathReportJasperFile, PATH_DOC+pathReportPDFFile,parameters);
    model.addAttribute("fileTemp", pathReportPDFFile);
   } catch (Exception e) {
    e.printStackTrace();
   }
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ito2.telecommunication.domain.ViewLog;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 * ReportUtil
 *
 */
public class ReportUtil 
{

 
 /***
  * 
  * 
  * @param list data set to be set
  * @param pathReportJasperFile E:/ito2/report/viewlog360report.jasper
  * @param pathReportPDFFile D:/test_jasper.pdf
  * @throws Exception
  * @author Sanka Senavirathna
  * Mar 30, 2016 12:54:58 AM
  */
   public static void createreport(List list,String pathReportJasperFile,String pathReportPDFFile,HashMap parameters) throws Exception {
    

     JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(list);
     JasperPrint jasperPrint = JasperFillManager.fillReport(pathReportJasperFile, parameters,beanColDataSource);
            JasperExportManager.exportReportToPdfFile(jasperPrint, pathReportPDFFile);
    
   }

   /***
    * 
    * sample main method    * @param args
    * @throws Exception
    * @author Sanka Senavirathna
    * Mar 30, 2016 1:00:05 AM
    */
  public static void main(String args[]) throws  Exception
  {
   String pathReportJasperFile="E:/ito2/report/viewlog360report.jasper";
   String pathReportPDFFile="D:/test_jasper.pdf";
   List employeeList = addEmployeeInfo();//data to be set
   createreport(employeeList,pathReportJasperFile,pathReportPDFFile,null) ;
  }
  
 
    
}

No comments:

Post a Comment