burferd Posted August 3, 2007 Share Posted August 3, 2007 I am generating an excel spread sheet report and when I get the prompt to open or save, if I pick 'Open', the output is generated in my application browser window rather than in a separate browser or excel itself. How do I get the output generated either in excel or at least in a new browser window? Here is the code that I use to generate the excel output from a JasperPrint object. Code: public String doExcelReportView( JasperPrint reportout, String filename ) { result = "Ok"; FacesContext faces = javax.faces.context.FacesContext.getCurrentInstance(); HttpServletResponse response =(HttpServletResponse)faces.getExternalContext().getResponse(); JRXlsExporter exporter = new JRXlsExporter(); ByteArrayOutputStream xlsReport = new ByteArrayOutputStream(); try{ exporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.JASPER_PRINT, reportout); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, java.lang.Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, java.lang.Boolean.FALSE); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport); exporter.exportReport(); }catch (JRException jex){ result = "Couldn't complete xls export:"+jex.getMessage(); return result; } byte[] bytes = xlsReport.toByteArray(); response.setContentType("application/vnd.ms-excel"«»); response.setContentLength(bytes.length); response.setHeader("Content-disposition","attachment; filename=""+filename+".xls""«»); try{ ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); }catch (Exception ioex){ result = "Couldn't complete xls export:"+ioex.getMessage(); } faces.responseComplete(); return result; } Link to comment Share on other sites More sharing options...
jwsmith22 Posted August 3, 2007 Share Posted August 3, 2007 Here is what I do to output to a pdf viewer, I would assume that you can do the same thing with Excel... Code:public void printPreview() throws SettingsException, IOException, JRException {//Get the path to the viewing application String pdfViewer = Settings.getSetting("pdfViewer",""Foxit Reader""«»);//log... classLogger.debug(pdfViewer);//create a temporary file in the system's temp file space File tempFile = File.createTempFile("tmp",".pdf"«»);//Export to the temporary file... JasperExportManager.exportReportToPdfFile(jasperPrint,tempFile.getAbsolutePath()); classLogger.debug(tempFile.getAbsolutePath()); pdfViewer = """ + pdfViewer + """; String[] command = new String[2]; tempFile.deleteOnExit();//At some point make sure both Path's are surrounded with "" to account for spaces command[0] = pdfViewer ; command[1] = """ + tempFile.getAbsolutePath() + """;//execute your viewer Runtime.getRuntime().exec(command); } Link to comment Share on other sites More sharing options...
burferd Posted August 3, 2007 Author Share Posted August 3, 2007 It looks like you are executing a command to run your pdf viewer on the system that is running the application. This is a JSF application, and I need to run this on the client, not the server. Link to comment Share on other sites More sharing options...
jwsmith22 Posted August 3, 2007 Share Posted August 3, 2007 Have you tried setting the content type simply to "application/excel"? Of course, I am not familiar with what "vnd.ms-excel" is so I am not sure. Link to comment Share on other sites More sharing options...
burferd Posted August 3, 2007 Author Share Posted August 3, 2007 That appears to work in my development environment.Still need to check in deployed client-server environment, but i suspect that will work also. Thanks. The 'vnd-ms.excel' was from an example I managed to find. I have no clue as to what it signifies. Link to comment Share on other sites More sharing options...
szaharia Posted August 3, 2007 Share Posted August 3, 2007 Hi, This could be just because an environment setting. If you are on win xp, try this: open a windows explorer and click on the Folder Options menu. Click on the File Types tab and select the XLS type in the Registered file types area. Click on Advanced. In the new opened dialog, make sure the 'Browse in same window' checkbox is NOT checked. Click OK, then again OK, and your problem should be solved. Good luck,sanda Link to comment Share on other sites More sharing options...
burferd Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks, That explains why I got different behavior on two different machines. 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