Jump to content

How to create Excel File direct to disk


burferd

Recommended Posts

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 create

the file and write it directly to a directory on disk

without 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

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...