0

In a JSF 2.x application, I'm using PrimeFaces V-3.5 and JasperReports V-5.2. I am trying to export some reports with JasperReports. I am able to export report in PDF and RTF format; however, Excel format does not work and I encounter error as the following:

javax.el.ELException: java.lang.NoClassDefFoundError: jxl/read/biff/BiffException

Here is my report generator method:

private void prepareReport(String reportPath, @SuppressWarnings("rawtypes") List beanList, String outputFormat, String reportName) {
    JasperReport jReport = null;
    JasperPrint jPrint = null;
    try {
        jReport = JasperCompileManager.compileReport(reportPath);
        jPrint = JasperFillManager.fillReport(jReport, jasperParameter, new JRBeanCollectionDataSource(beanList));
    } catch (JRException e) {
        e.printStackTrace();
    }

    FacesContext ctx = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
    OutputStream outputStream = null;

    try {
        outputStream = response.getOutputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        if (outputFormat.equals(OUTPUT_FORMAT_PDF)) {
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "attachment; filename=" + reportName + ".pdf");
            JasperExportManager.exportReportToPdfStream(jPrint, outputStream);

        } else if (outputFormat.equals(OUTPUT_FORMAT_EXCEL)) {
            response.setContentType("application/xls");
            response.setHeader("Content-disposition", "attachment; filename=" + reportName + ".xls");
            JExcelApiExporter exporter = new JExcelApiExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
            exporter.exportReport();

        } else if (outputFormat.equals(OUTPUT_FORMAT_RTF)) {
            response.setContentType("application/rtf");
            response.setHeader("Content-disposition", "attachment; filename=" + reportName + ".rtf");
            JRRtfExporter exporter = new JRRtfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
            exporter.exportReport();
        }
    } catch (JRException e) {
        e.printStackTrace();
    }

    try {
        outputStream.flush();
        outputStream.close();
        FacesContext.getCurrentInstance().responseComplete();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

How can I export report in Excel format?

Alex K
  • 22,315
  • 19
  • 108
  • 236
nudastack
  • 155
  • 1
  • 5
  • 18
  • You should post the full stack trace – Alex K Oct 25 '13 at 13:42
  • Did you add *jxl-2.6.10.jar* to classpath? You need this artifact: *net.sourceforge.jexcelapi.jxl:2.6.10* for *JasperReports 5.2* – Alex K Oct 25 '13 at 14:11
  • I just added jxl-2.6.10.jar file and it is working. Thank you in advance. Make it as an answer so that I can vote for you. – nudastack Oct 25 '13 at 14:49

1 Answers1

0

You are using the JExcelApiExporter. It is using the JExcelApi library.

You can check the dependency in the JasperReports's pom.xml file. The JasperReports 5.2 is using this artifact:

<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.10</version>
</dependency>

It means that you should add jxl-2.6.10.jar to your application classpath (in case using JasperReports 5.2).

Alex K
  • 22,315
  • 19
  • 108
  • 236