2010-09-17 30 views
1

我需要在JasperReports中導出爲ex​​cel和csv格式。對於excel,我嘗試了使用JRXlsExporter類,但它不是導出。事情是與「保存和取消」彈出窗口與文件類型未知的未來..如何在JasperReports中導出爲ex​​cel?

file type like "getReportDetail.do" 

其中getReportDetail.do是「路徑」屬性中配置XML支柱的「動作」元素。我打電話給這個getReportDetail.do通過點擊html按鈕來調用「action class」來導出excel。

我設置參數如下圖所示

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); 

其中reportStreamByteArrayOutputStream()對象,

reportExporterJRXlsExporter對象

contenttyperesponse.setContentType("application/xls");

任何想法,爲什麼它是發生了什麼?

+2

試着將文件保存到文件系統首先看是否生成的報告很糟糕,或者之後發生的情況。 – 2010-09-17 16:52:25

回答

3

嘗試設置以下標題:

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

這裏是我的代碼,但照顧與實施的版本。 注意:導出Excel與iREPORT分享到人人,爲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(); 
       } 

和更低版本的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(); 

       }