2014-06-09 15 views
34

tôi có mã này để xuất JasperReprot để XLS:JasperReports 5.6: JRXlsExporter.setParameter bị phản

 JasperPrint jprint=JasperFillManager.fillReport(expRpg, null, new JRBeanCollectionDataSource(datalist)); 
     JRXlsExporter exporter = new JRXlsExporter(); 
     exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jprint); 
     exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outStream); 
     exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); 
     exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); 
     exporter.exportReport(); 

Nâng cấp lên JasperReports 5,6 tất cả setParameter được gắn cờ là "phản đối" và tôi không thể tìm thấy tài liệu để thích ứng với mã này.

Cách xuất báo cáo thành xls với JasperReports 5.6?

+2

Bạn nên đọc [thông báo dành cho nhà phát triển] (http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRAbstractExporter.html#setParameter%28net.sf.jasperreports.engine.JRExporterParameter,% 20java.lang.Object% 29): 'Thay thế bởi setE xporterInput (ExporterInput), setConfiguration (ExporterConfiguration), setConfiguration (ReportExportConfiguration) và setExporterOutput (ExporterOutput) '. –

+0

Bạn cũng có thể tìm thấy nhiều mẫu trong gói * JR * –

Trả lời

62

JRExporter không còn được dùng nữa trong phiên bản 5.6. Họ giới thiệu giao diện mới Xuất khẩu và trang bị thêm tất cả các nhà xuất khẩu để có ExporterInput, ReportExportConfiguration, ExporterConfiguration, ExporterOutput. Xem dưới đây liên kết

http://jasperreports.sourceforge.net/api/net/sf/jasperreports/export/Exporter.html

Điều này có nghĩa rằng thay vì setParameter, bạn cần phải tạo ra cấu hình sử dụng các lớp nêu trên hoặc các lớp con mình PDF xuất khẩu chẳng hạn. xuất khẩu Excel nên theo cùng một phương pháp

JRPdfExporter exporter = new JRPdfExporter(); 

exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); 
exporter.setExporterOutput(outputStream); 
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration(); 
exporter.setConfiguration(configuration); 

exporter.exportReport(); 

Excel đối

JRXlsExporter exporter = new JRXlsExporter(); 
exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); 
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile)); 
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration(); 
configuration.setOnePagePerSheet(true); 
configuration.setDetectCellType(true); 
configuration.setCollapseRowSpan(false); 
exporter.setConfiguration(configuration); 

exporter.exportReport(); 

SimpleXlsReportConfiguration sẽ có nổi trội cấu hình xuất khẩu liên quan. Set giá trị theo yêu cầu của bạn

+0

Đối với luồng đầu ra, nếu bạn đang sử dụng một lớp như 'ByteArrayOutputStream', bạn nên làm điều này:' exports.setExporterOutput (new SimpleOutputStreamExporterOutput (os)); ' – CorayThan

+0

một dòng "exports.exportReport();" bị thiếu cho đối tác excel – karansky

2

Thank vào mã ở trên, đây là mã của tôi: Chú ý: Xuất sắc với 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(PuenteFile.getRutaFiles(FacesContext.getCurrentInstance(), PuenteFile.RUTA_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(); 
       } 
0

Đây là mã của tôi:

String sourceFileName = "./jasper_report_template.jasper"; 
Map parameters = new HashMap(); 
String printFileName = null; 
try { 
     printFileName = JasperFillManager.fillReportToFile(sourceFileName, parameters, beanArrayDataSource); 
     if(printFileName != null){ 
      //JasperPrintManager.printReport(printFileName, true); 
      /** 1- export to PDF*/ 
      JasperExportManager.exportReportToPdfFile(printFileName, 
         "C://Users/zanderkong/Desktop/sample_report.pdf"); 
      /**3- export to Excel sheet*/ 
      RXlsExporter xlsExporter = new JRXlsExporter(); 
      xlsExporter.setExporterInput(new SimpleExporterInput(printFileName)); 
      xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput("C://Users/zanderkong/Desktop/sample_report.xls")); 
      SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration(); 
      configuration.setOnePagePerSheet(true); 
      configuration.setDetectCellType(true); 
      configuration.setCollapseRowSpan(false); 
      xlsExporter.setConfiguration(configuration); 
      xlsExporter.exportReport(); 
     } 
     } catch (JRException e) { 
      e.printStackTrace(); 
      }