zeeshan nazakat zeeshan nazakat - 4 months ago 53
Java Question

Jasper Report in Excel format conversion

I am currently working in jasper report I have one method to generate report in PDF format. It is working fine but I want the same report in excel format.
Here is my method:

public void generateFleetReportPdf(List listOfDailyBean) throws JRException, IOException {

/*
Convert List to JRBeanCollectionDataSource
*/
JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(listOfDailyBean);

/*
Map to hold Jasper Report parameters
*/

Map<String,Object> parameters = new HashMap<String, Object>();
parameters.put("ItemDataSource", beanCollectionDataSource);

String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/reports/sadaqatReport.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, parameters, beanCollectionDataSource);
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
FacesContext.getCurrentInstance().responseComplete();

}

Answer

Please try the below code. It should export the Jasper report to .xlsx format. If you prefer .xls, then replace net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter with net.sf.jasperreports.engine.export.JRXlsExporter

public void generateFleetReportXlsx(List listOfDailyBean) throws JRException, IOException {
    /*
     Convert List to JRBeanCollectionDataSource 
     */
    JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(listOfDailyBean);

    /* 
        Map to hold Jasper Report parameters
    */

    Map<String,Object> parameters = new HashMap<String, Object>();
    parameters.put("ItemDataSource", beanCollectionDataSource);

    String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/reports/sadaqatReport.jasper");
    JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, parameters, beanCollectionDataSource);
    HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
    httpServletResponse.addHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.xlsx");
    ServletOutputStream outputStream = httpServletResponse.getOutputStream();
    net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter exporter = new net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,outputStream);
    exporter.exportReport();
    outputStream.flush();
    outputStream.close();
    FacesContext.getCurrentInstance().responseComplete();
}
Comments