开发者

How to export to excel in JasperReports?

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?


Try setting the following headers:

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


Here is my code but take care with the version implemented. Notice : Export excel with ireport, for ireport 6.0 , java 7

 Map<String, Object> parametro = new HashMap<String, Object>();
                parametro.put("USUARIO", UConstante.NAME_MINISTERIO_USER);
                parametro.put("RUTA_LOGO", PuenteFile.getRutaFiles(FacesContext.getCurrentInstance(), PuenteFile.RUTA_IMG_LOGO));
                parametro.put("PATH_SYSTEM", rutaFileSystemHD);
                parametro.put("WHERE_DATA", WHERE_REGISTRO);
                parametro.put("WHERE_PROYECTO_USUARIO", WHERE_PROYECTO_USUARIO);
                parametro.put("WHERE_ZONA", WHERE_ZONA);
                parametro.put("NAME_APP", RutaFile.NAME_APP);
                parametro.put("ID_USUARIO", getUsuario().getId());
                parametro.put("ID_PROYECTO", beanProyecto.getId());
                parametro.put("SUBREPORT_DIR", SUBREPORT_DIR);

                System.out.println(">>>>>> PARAMETROS :" + parametro.toString());

              try {
                    JasperPrint jasperPrint = JasperFillManager.fillReport(path, parametro, PgConnector.getConexion());
                    JRXlsExporter xlsExporter = new JRXlsExporter();
                    xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
                    xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(PATH_REPORT_FILE + nameExcel);
                    SimpleXlsReportConfiguration xlsReportConfiguration = new SimpleXlsReportConfiguration();
                    SimpleXlsExporterConfiguration xlsExporterConfiguration = new SimpleXlsExporterConfiguration();
                    xlsReportConfiguration.setOnePagePerSheet(true);
                    xlsReportConfiguration.setRemoveEmptySpaceBetweenRows(false);
                    xlsReportConfiguration.setDetectCellType(true);
                    xlsReportConfiguration.setWhitePageBackground(false);
                    xlsExporter.setConfiguration(xlsReportConfiguration);
                    xlsExporter.exportReport();

                } catch (Exception ex) {
                    ex.printStackTrace();
                }

and for lower versions of ireport 5.6

          try {
                   JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(path), parametro, PgConnector.getConexion());
                    JRXlsExporter exporterXLS = new JRXlsExporter();
                    exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
                    exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
                    exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
                    exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
                    exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
                    exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME,PATH_REPORT_FILE + nameExcel);
                    exporterXLS.exportReport();
                } catch (Exception ex) {
                    ex.printStackTrace();

                }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜