How to export to excel in JasperReports

jasper-reports

I need to export to excel and csv format in JasperReports. For excel I tried by using JRXlsExporter class, but it is not exporting. The thing is with "save and cancel" popup window is coming with file type unknown..

 file type like "getReportDetail.do"

where getReportDetail.do is "path" attribute in "action" element of struts config xml. I am calling this getReportDetail.do by clicking html button to invoke "action class" to export excel.

I am setting parameter like below

reportExporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint); 
reportExporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, reportStream); 

reportExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); 
reportExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
reportExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
reportExporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);

where reportStream is object of ByteArrayOutputStream(),

reportExporter is JRXlsExporter object

and the contenttype is response.setContentType("application/xls");

Any idea why it is happening?

Best Answer

Try setting the following headers:

Content-Type: application/vnd.ms-excel
Content-Disposition: attachment; filename=report.xls
Related Topic