我有一个 Jasper 报告,它生成一个 Excel 文件并输出为一个字节 [],我希望该文件在浏览器中显示为下载,但目前它只是将原始代码打印到浏览器。
代码的相关部分如下,这是从 AJAX 请求发起的(页面已经加载):
JasperReport jasperReport;
JasperPrint jasperPrint;
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
byte[] excel = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
try {
ArrayList<SwimDataReport> dataList = getData();
if(dataList!=null){
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(dataList, false);
jasperReport = JasperCompileManager.compileReport(reportFile.getPath());
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
JRXlsExporter exporter = new JRXlsExporter();
ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
exporter.exportReport();
excel = xlsReport.toByteArray();
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename=\"Export.xls\"");
xlsReport.close();
OutputStream ouputStream = response.getOutputStream();
ouputStream.write(excel);
ouputStream.close();
facesContext.responseComplete();
this.cleanUp();
}else{
return;
}
} catch (JRException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
有什么想法我哪里出错了吗?