This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Trying the render a report to excel from a servlet - the servlet code runs fine but when excel opens I get a 'Unable to open file'. Any suggestions?? Here is the servelet code ----- 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(); - Thanks in advance
mohankrishna madeti
Greenhorn
Joined: Sep 23, 2003
Posts: 1
posted
0
<% try { //Class.forName(oracle.jdbc.driver.OracleDriver); DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = ........ File reportFile = new File("C:\\jsp\\sample.jasper"); String path = reportFile.getAbsolutePath(); //tried getAbsolutePath() //System.out.println("Absolute Path = " + reportFile.getAbsolutePath()); Map parameters = new HashMap(); parameters.put("myparam",new Integer(2)); JasperPrint jasperPrint = JasperManager.fillReport ( path,parameters, conn); byte bytes[] = new byte[10]; String result = JasperRunManager.runReportToHtmlFile("C:\\jsp\\sample.jasper" , parameters, conn); JRXlsExporter exporter = new JRXlsExporter(); ByteArrayOutputStream xlsReport = new ByteArrayOutputStream(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport); exporter.setParameter(JRExporterParameter.OUTPUT_FILE, "C:\\JSP\\"); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "sample.xls"); exporter.exportReport(); System.out.println("Sixe of byte array:"+xlsReport.size()); bytes = xlsReport.toByteArray(); response.setContentType("application/vnd.ms-excel"); System.out.println("After JasperPrint = "+result); response.setContentLength(bytes.length); xlsReport.close(); OutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); } catch(Exception e) {e.printStackTrace();} %> even i m getting exception but i m able to see my report data on excel file/page and i m able to save it.but there is an exception saying getOutputStream() has already been opened for the response..... if any thing i forgot or done wrong plese do let me know thank you. [ September 23, 2003: Message edited by: mohankrishna madeti ]
Shan Sun
Greenhorn
Joined: Oct 20, 2003
Posts: 1
posted
0
The above code is fine. I need one help.. i want to create a xlx file using the jasper report. i am able to create the report but in the excel sheet i am getting the extra empty line can any one help me to solve this proble. my id s.sundaram@ebusinessware.com thanks shan