teddybugs Posted September 20, 2019 Share Posted September 20, 2019 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? Link to comment Share on other sites More sharing options...
teddybugs Posted September 20, 2019 Author Share Posted September 20, 2019 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 Link to comment Share on other sites More sharing options...
swood_1 Posted September 20, 2019 Share Posted September 20, 2019 You also did not set a ContentType header for xls, but did for PDF. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now