Jasper报告Excel导出错误

时间:2018-01-28 12:00:09

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

我正在尝试使用以下代码块

从jasper报告中导出excel报告
JasperPrint jasperPrint = JasperFillManager.fillReport((JasperReport) request.getSession().getAttribute("report"),
            (Map) request.getSession().getAttribute("parameters"), getConnection());
    ServletOutputStream out = response.getOutputStream();
    JRXlsExporter exporter = new JRXlsExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
    exporter.exportReport();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment; filename=" + request.getSession().getAttribute("name") + ".xls");
    out.flush();

当执行上面的代码时,不是文件保存对话框,而是在浏览器中显示以下内容,

enter image description here

但是当我尝试以PDF格式导出报告时,它会完美执行。我试图找出服务器和应用程序日志,以获得提示excel导出实际发生的错误,但无法得到任何提示。我使用的是像jasper report 6.4.0,poi 3.14和tomcat 8.5.15这样的库。

所以我的问题是,在这种情况下,究竟可能出现的问题是excel导出失败了吗?任何有关解决问题的想法或如何追踪问题的提示都将不胜感激。

1 个答案:

答案 0 :(得分:2)

最后,我通过上面的两行来解决了这个问题,

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename=" + request.getSession().getAttribute("name") + ".xls");

前,

ServletOutputStream out = response.getOutputStream();
JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
exporter.exportReport();
out.flush();

因此,重新排列代码块使一切正常。重新排列后的代码块,

JasperPrint jasperPrint = JasperFillManager.fillReport((JasperReport) request.getSession().getAttribute("report"),
            (Map) request.getSession().getAttribute("parameters"), getConnection());
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment; filename=" + request.getSession().getAttribute("name") + ".xls");
    ServletOutputStream out = response.getOutputStream();
    JRXlsExporter exporter = new JRXlsExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
    exporter.exportReport();
    out.flush();
相关问题