2006 IR Open Dicussion Posted August 27, 2006 Share Posted August 27, 2006 By: andre_a_s - andre_a_s Can't view the xls exported file 2006-05-22 12:54 Hi all :) I'm having problems when exporting to XLS. When I export to PDF, everything is fine, but when I try to export XLS, it exports and I cannot open it, "unable to open file". That's my code to generate the PDF: ================================================= String fileName = new SimpleDateFormat("ddMMyyyy-HHmmss").format(new Date()) + ".pdf"; exporter = new JRPdfExporter(); response.setHeader("Content-Disposition","attachment;filename="" + fileName + """); response.setContentType("application/octet-stream"); exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, print); exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, new BufferedOutputStream(response.getOutputStream())); ================================================= I just copied the code and changed a little for the XLS: ================================================= String fileName = new SimpleDateFormat("ddMMyyyy-HHmmss").format(new Date()) + ".xls"; exporter = new JRXlsExporter(); response.setHeader("Content-Disposition","attachment;filename="" + fileName + """); response.setContentType("application/vnd.ms-excel"); exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, print); exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, new BufferedOutputStream(response.getOutputStream())); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); ================================================= I can't generate a phisical file like "c:\file.xls", I need to do it like the PDF, the user need to download the file. Someone help me? =] By: Anurag - anurag1005 RE: Can't view the xls exported file 2006-05-23 00:04 Please try with this code This code will send the Response back to screen fromn where you ahev tried to generate the report and user can save the generated file to view. thanks Anurag ----- ServletContext context = this.getServletConfig().getServletContext(); File reportFile = new File(context.getRealPath("/reports/WebappReport.jasper")); JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath()); Map parameters = new HashMap(); parameters.put("ReportTitle", "Address Report"); parameters.put("BaseDir", reportFile.getParentFile()); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new WebappDataSource()); JRXlsExporter exporter = new JRXlsExporter(); ByteArrayOutputStream xlsReport = new ByteArrayOutputStream(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport); exporter.exportReport(); System.out.println("Sixe of byte array:"+xlsReport.size()); bytes = xlsReport.toByteArray(); response.setContentType("application/vnd.ms-excel"); response.setContentLength(bytes.length); ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); -------------------- By: andre_a_s - andre_a_s RE: Can't view the xls exported file 2006-05-23 09:10 Really thanx Anurag !!! It workd fine. But tell me... why the way've I tried did not work? 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