簡體   English   中英

Jasper Report Excel導出錯誤

[英]Jasper Report Excel Export Error

我正在嘗試使用以下代碼塊從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();

執行以上代碼后,瀏覽器中將顯示以下內容,而不是文件保存對話框:

在此處輸入圖片說明

但是,當我嘗試以PDF格式導出報告時,它執行得很好。 我試圖找出服務器和應用程序日志以獲取提示,但excel導出實際上發生了錯誤,但無法獲取任何提示。 我正在使用像jasper report 6.4.0,poi 3.14和tomcat 8.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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM