Tuesday, 18 August 2015

Generate Jasper report into different formats(xls,pdf,csv,odt,rtf,html)


                                Generate Jasper reports into different formats 


You can generate jasper report into different formats like excel sheets(xls),portable document format (pdf) ,comma separated values(csv),rich text format(rtf),hyper text mark up language(html),extensible markup language(xml), open document text(odt) etc.


In order to generate jasper report into specific format,you need to have JasperPrint object.You can make use of jasper report classes for each formats. for ex: to generate csv file ,you can make use of JRCsvExporter class.

1) Generate report in CSV format:-

 private static byte[] generateCsv(JasperPrint jasperPrint) throws JRException {  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    JRCsvExporter rtfExporter = new JRCsvExporter();  
    rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
    rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);  
    rtfExporter.exportReport();  
    return baos.toByteArray();  
   }  

2) Generate report in PDF format:-

private static byte[] generatePDF(JasperPrint jasperPrint) throws JRException {  
    return JasperExportManager.exportReportToPdf(jasperPrint);  
   }  

3) Generate report in XLS format:-

private static byte[] generateXls(JasperPrint jasperPrint) throws JRException {  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    JRExporter xlsExporter = new JExcelApiExporter();  
    xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
    xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);  
    xlsExporter.exportReport();  
    return baos.toByteArray();  
   }  

4) Generate report in RTF format:-

private static byte[] generateRtf(JasperPrint jasperPrint) throws JRException {  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    JRRtfExporter rtfExporter = new JRRtfExporter();  
    rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
    rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);  
    rtfExporter.exportReport();  
    return baos.toByteArray();  
   }  

5) Generate report in HTML format:-

private static byte[] generateHtml(JasperPrint jasperPrint, String reportName) throws JRException {  
  String HTML_REPOT_LOC = "/var/data/sched-ops/kpireports/htmlreport/";  
    JasperExportManager.exportReportToHtmlFile(jasperPrint, HTML_REPOT_LOC + reportName + ".html");  
    return null;  
   }  

6) Generate report in XML format:-

 private static byte[] generateXml(JasperPrint jasperPrint) throws JRException {  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    JRExporter xmlExporter = new JRXmlExporter();  
    xmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
    xmlExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);  
    xmlExporter.setParameter(JRXmlExporterParameter.IS_EMBEDDING_IMAGES, Boolean.TRUE);  
    xmlExporter.exportReport();  
    return baos.toByteArray();  
   }  

7) Generate report in ODT format:-

private static byte[] generateOdt(JasperPrint jasperPrint) throws JRException {  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    JRExporter odtExporter = new JROdtExporter();  
    odtExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
    odtExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);  
    odtExporter.exportReport();  
    return baos.toByteArray();  
   }  



Make sure you have imported Jasper Report jar into your workspace.

Enjoy programming. :)

No comments:

Post a Comment