Junaid
Junaid

Reputation: 1179

Jasper Report Excel Export Error

I am trying to export excel report from jasper report using following code block,

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();

When above code executes, instead of file save dialog, following shows in browser,

enter image description here

But when i try to export report in PDF format, it executes perfectly. I tried to trace out server and application logs to get hint what is actually happening wrong for excel export, but unable to get any hint. I am using libraries like jasper report 6.4.0, poi 3.14 and tomcat 8.5.15.

So my question is, in this circumstances, what exactly might be the problem here which is failing excel export? Any idea regarding solving the problem or tips on how can i trace the problem will be appreciated.

Upvotes: 1

Views: 2982

Answers (1)

Junaid
Junaid

Reputation: 1179

Finally i solved the issue by bringing following two lines above,

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

Before,

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

So, rearranging the code block made everything work fine. Code block after rearrangement,

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();

Upvotes: 4

Related Questions