2004 IR Help Posted August 24, 2006 Share Posted August 24, 2006 By: Raj - mageshraj Exporting to XLS & Embeding report in jsp 2005-07-07 18:27 I am having problem of exporting the reports to Excel file. Java Code: ======= public byte[] xlsReportToArray(JasperPrint jasperPrint){ byte[] bytes = null; try{ JRXlsExporter jasperXlsExportMgr = new JRXlsExporter(); ByteArrayOutputStream xlsReport = new ByteArrayOutputStream(); jasperXlsExportMgr.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE, java.lang.Boolean.TRUE); jasperXlsExportMgr.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, java.lang.Boolean.FALSE); jasperXlsExportMgr.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport); jasperXlsExportMgr.exportReport(); bytes = xlsReport.toByteArray(); }catch (JRException jex){ // throw new Exception ("Unable to create the XLS file" + jex); jex.printStackTrace(); } return bytes; } public byte[] loadReportXLS() { //Get the connection, parameters... // byte[] bytes = xlsReportToArray(jasperPrint); return bytes; } JSP code ======= byte[] bytes = loadReportXLS(); response.setHeader("Content-Disposition", "attachment; filename="+filename+".XLS"); response.setContentType("application/vnd.ms-excel"); //response.setContentType("application/msexcel"); response.setContentLength(bytes.length); if (bytes.length > 0) { try{ ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); }catch (IOException ioex){ ioex.printStackTrace(); } } %> Problem: ====== When i open the jsp page, it opens a popup window , with options of "Open","Save" etc.. if i click on open, it opens the report in the excel, which works great. BUT I need to open the excel file without the popup screen.. I tried uncommenting the "response.setHeader" property which stops the popup window, but the data is not displayed properly(column is missing, data scattered around).. 2. How to open(embed) the report in JSP without saving/exporting it in a html file... Any help to solve this would be appreciated.. By: sanforum - sanforum not showing image when exporting to excel 2006-01-05 01:21 The following is the code to generate report in excel format. It works fine for reports having texts only.. But when it produce reports having image it is not showing the image... the image file path is written in the .JRXML file which is compiled to .JASPER file from which the report is generated in Excel in jsp The code for generating the reports in Jsp in Excel format...... <%@page contentType="application/vnd.ms-excel"%> <%@ page errorPage="error.jsp" %> <%@ page import="net.sf.jasperreports.engine.*" %> <%@ page import="net.sf.jasperreports.engine.util.*" %> <%@ page import="net.sf.jasperreports.engine.export.*" %> <%@ page import="java.util.*" %> <%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <% //response.setHeader("CACHE-CONTROL","NO-CACHE"); //response.setHeader("EXPIRES","0"); //response.setHeader("PRAGMA","NO-CACHE"); //CreateXml cml=(CreateXml) request.getAttribute("REPORT_BEAN"); //String reportName = cml.getReportName(); String reportName=request.getParameter("reportName"); try { Class.forName("org.gjt.mm.mysql.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://sfindiajv4:3306/track_master","root","admin"); // File reportFile = new File("./usertemplates/test.jasper"); File reportFile = new File("./usertemplates/"+reportName+".jasper"); String abspath = reportFile.getAbsolutePath(); //tried getAbsolutePath() String path = reportFile.getPath(); System.out.println("Absolute Path = " + reportFile.getAbsolutePath()); System.out.println(" Path = " + reportFile.getPath()); Map parameters = new HashMap(); parameters.put("BaseDir", reportFile.getParentFile()); //parameters.put("myparam",new Integer(2)); JasperPrint jasperPrint = JasperManager.fillReport ( path,parameters, new net.sf.jasperreports.engine.JREmptyDataSource()); byte bytes[] = new byte[10000]; //String result = JasperRunManager.runReportToHtmlFile("./usertemplates/test.jasper" , parameters, conn); 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("Size of byte array:"+xlsReport.size()); bytes = xlsReport.toByteArray(); response.setContentType("application/vnd.ms-excel"); System.out.println("After JasperPrint = 1"); response.setContentLength(bytes.length); System.out.println("After JasperPrint = 2"); xlsReport.close(); System.out.println("After JasperPrint = 3"); OutputStream ouputStream = response.getOutputStream(); System.out.println("After JasperPrint = 4"); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); } catch(Exception e) {e.printStackTrace();} %> 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