Jasperreports excel failed to export

0

I am trying to export jasperreport to xls format but failed.

following are my code:

<%@page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.export.JRXlsExporter"%>
<%@page import="net.sf.jasperreports.engine.export.JRPdfExporter"%>
<%@page import="net.sf.jasperreports.engine.export.JRPdfExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.JRExporter"%>
<%@page import="net.sf.jasperreports.engine.JRExporterParameter"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.JasperPrint"%>
 <%
    String RptName = (request.getParameter("RptName")) != null? request.getParameter("RptName"):"";    
    String iReportPath = utilHandler.GetResBundle("IREPORT_PATH");
    String jasperReportPath    = iReportPath + RptName + ".jasper"; 
    response.setContentType("application/"+strReportFormat);
    os = response.getOutputStream();
 
    if(strReportFormat.equals("pdf")) {
    exporter = new JRPdfExporter();    
    exporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "UTF-8");    
    print = JasperFillManager.fillReport(jasperReportPath,map, connection);
    exporter.setParameter(JRExporterParameter.JASPER_PRINT,print);     
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,os);    
    exporter.exportReport();    
 
    } else if(strReportFormat.equals("msexcel")) {
      strReportFileFormat = "xls";
      response.setHeader("Content-Disposition"," inline; filename="+RptName+"."+strReportFileFormat);      
      exporter = new JRXlsExporter();
      exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
      exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, os);
      exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
      exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
      exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS,Boolean.TRUE);
      exporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN,Boolean.TRUE);
      exporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS,Boolean.TRUE);
      exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
      System.out.println("generate Excel");
        print = JasperFillManager.fillReport(jasperReportPath,map, connection);
        exporter.setParameter(JRExporterParameter.JASPER_PRINT,print);     
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,os);    
        exporter.exportReport();      
    }
 %>

i get Failed - Network error on chrome browser,

Error on chrome

i use the .jasper file to produce pdf and excel, xls are failed but PDF work fine and display fine on chrome browser. i checked the tomcat 8 log, could not find any error log on catalina.out and also the tomcat console (windows bat). Any idea what could be the problem?

teddybugs's picture
Joined: Sep 17 2019 - 10:16pm
Last seen: 3 weeks 22 hours ago

the response Content-Disposition is wrong, i replaced to:

response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(RptName.getBytes("utf-8"),"ISO-8859-1") + ".pdf\"");
response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(RptName.getBytes("utf-8"),"ISO-8859-1") + ".xls\"");

now working fine

teddybugs - 4 weeks 1 day ago

1 Answer:

0

You also did not set a ContentType header for xls, but did for PDF.

 

swood_1's picture
604
Joined: Nov 15 2012 - 10:47am
Last seen: 9 hours 13 min ago
Feedback
randomness