1

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.

Alex K
  • 22,315
  • 19
  • 108
  • 236
Junaid
  • 1,179
  • 2
  • 18
  • 35
  • 1
    To me this looks like the plain code of the Excel file. So to me it looks like Jaspersoft is doing fine. Try using any other browser. Otherwise try to save it as an Excel file. – Markus Deindl Jan 29 '18 at 07:58
  • @MarkusDeindl It seems code was fine just needed little rarrangement. Solution of the issue is given below as answer. – Junaid Jan 29 '18 at 11:56
  • See also: https://stackoverflow.com/a/48411646/59087 – Dave Jarvis Jan 29 '18 at 17:08
  • rearranging the code not working for me. I am facing same issue with jasperreports-6.0.0.jar , org.apache.poi:poi-ooxml-schemas:3.10.1 ,org.apache.poi:poi-ooxml:3.10.1 and poi-xml jar – NIRAJ KUMAR Jun 22 '20 at 12:54

1 Answers1

4

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();
Junaid
  • 1,179
  • 2
  • 18
  • 35