简体   繁体   中英

Jasperreports excel failed to export

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, 铬错误

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?

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\"");

for both pdf and xls, now working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM