在 JSF 2.x 应用程序中,我使用 PrimeFaces V-3.5 和 JasperReports V-5.2 。我正在尝试使用 JasperReports 导出一些报告。我能够以 PDF 和 RTF 格式导出报告;但是, Excel 格式不起作用,我遇到如下错误:
javax.el.ELException: java.lang.NoClassDefFoundError: jxl/read/biff/BiffException
这是我的报告生成器方法:
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();
}
}
如何以 Excel 格式导出报告?
答案 0 :(得分:0)
您正在使用JExcelApiExporter。它正在使用JExcelApi库。
您可以检查 JasperReports 的 pom.xml 文件中的依赖项。 JasperReports 5.2 正在使用此工件:
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
这意味着您应该将 jxl-2.6.10.jar 添加到您的应用程序类路径中(如果使用 JasperReports 5.2 )。