burferd Posted May 22, 2007 Share Posted May 22, 2007 I have checked 'The JasperReports Ultimate Guide' and numerous posts on this forum, but have not been able to find an answer to my question. I have an application that generates an Excel report.When the report is generated, I get a prompt to save or open the file.That all works just fine, but what I need is to createthe file and write it directly to a directory on diskwithout being prompted. Here is the code I am using to generate the report: public String doExcelReport( 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; } If there is a way to dump the file directly, I suspect it will be a parameter set in the following type of calls: response.setContentType("application/vnd.ms-excel"); response.setContentLength(bytes.length); response.setHeader("Content-disposition","attachment; filename=""+filename+".xls""); Link to comment Share on other sites More sharing options...
burferd Posted May 25, 2007 Author Share Posted May 25, 2007 Doesn't anyone write files directly to disk?It seems like a lot of people have looked at this, but no one has any suggestions on how to do this or where to look for an answer..... Link to comment Share on other sites More sharing options...
arunvishvin Posted May 30, 2007 Share Posted May 30, 2007 Hi,This is easy...set a path for the file with a filename and export it to that file using the option OUTPUT_FILE_NAME in the exporter parameter.Your file will be exported to the location specified in the file.(and dont forget to remove response code) Link to comment Share on other sites More sharing options...
burferd Posted May 31, 2007 Author Share Posted May 31, 2007 I'm still having problems with this.I suspect it is that I am not sure exactly what I need in the exporter settings. Any chance of either including the required parameters or pointing me to where I can find an example?? I removed the response.setXXX() calls and changed the export code to: 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_FILE_NAME, "C:/MyReport.xls");exporter.exportReport(); Link to comment Share on other sites More sharing options...
kidice Posted May 31, 2007 Share Posted May 31, 2007 here is an example with a javabean collection Code:JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(exampleCollection);jasperReport = JasperCompileManager.compileReport("C:/Example.jrxml"«»);jasperPrint = JasperFillManager.fillReport(jasperReport, parameter, datasource);JasperExportManager.exportReportToPdfFile(jasperPrint,"C:/Example.pdf"«»);Post edited by: kidice, at: 2007/05/31 13:58 Link to comment Share on other sites More sharing options...
burferd Posted May 31, 2007 Author Share Posted May 31, 2007 Thanks for the feedback.I found the problem. The code in my last post worked.I had a problem before I got there that I found with the debugger. 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