Java Jasper报表Excel导出错误
我正在尝试使用以下代码块从jasper报表导出excel报表Java Jasper报表Excel导出错误,java,spring-mvc,jasper-reports,export-to-excel,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()); ServletOutputSt
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();
当执行上述代码时,浏览器中将显示以下内容,而不是“文件保存”对话框:
但当我尝试以PDF格式导出报告时,它执行得非常完美。我试图跟踪服务器和应用程序日志,以了解excel导出的实际错误,但无法获得任何提示。我正在使用像JasperReport6.4.0、POI3.14和Tomcat8.5.15这样的库
所以我的问题是,在这种情况下,excel导出失败的问题到底是什么?任何关于解决问题的想法或如何追踪问题的提示都将受到欢迎。最后,我通过上面的两行代码解决了这个问题
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();
最后我通过上面的两行解决了这个问题
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();
对我来说,这看起来像是Excel文件的普通代码。所以在我看来,Jaspersoft做得很好。尝试使用其他浏览器。否则,请尝试将其保存为Excel文件。@MarkusDeindl代码似乎很好,只是需要一些修改。下面给出了问题的解决方案作为答案。另请参阅:重新排列代码对我不起作用。我在jasperreports-6.0.0.jar、org.apache.poi:poi ooxml模式:3.10.1、org.apache.poi:poi ooxml:3.10.1和poi xml jar中遇到了同样的问题。在我看来,这就像是Excel文件的普通代码。所以在我看来,Jaspersoft做得很好。尝试使用其他浏览器。否则,请尝试将其保存为Excel文件。@MarkusDeindl代码似乎很好,只是需要一些修改。下面给出了问题的解决方案作为答案。另请参阅:重新排列代码对我不起作用。我在jasperreports-6.0.0.jar、org.apache.poi:poi-ooxml模式:3.10.1、org.apache.poi:poi-ooxml:3.10.1和poi-xml-jar中面临同样的问题