Jasper报告以Excel格式转换

时间:2016-07-18 04:46:46

标签: java jasper-reports export-to-excel

我目前在JasperReports工作。我有一种方法来生成PDF格式的报告。它工作正常,但我想要excel格式的相同报告。

这是我的方法:

public void generateFleetReportPdf(List<DailyTrakingReportBean> 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();
}

2 个答案:

答案 0 :(得分:1)

请尝试以下代码。它应该将Jasper报告导出为 .xlsx 格式。如果您更喜欢 .xls ,请将net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter替换为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();
}

答案 1 :(得分:0)

 try {
        InputStream jasperStream = this.getClass().getResourceAsStream("yourReportName.jasper");
        Map<String, Object> params = new HashMap<>();
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);

        JRXlsxExporter xlsxExporter = new JRXlsxExporter();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        xlsxExporter.exportReport();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=anyName" + sdf.format(new Date()) + ".xlsx");
        response.getOutputStream().write(os.toByteArray());
        response.getOutputStream().flush();
        response.getOutputStream().close();
        response.flushBuffer();
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {

    }
    return "success";
}