2014-06-09 19 views
34

Ho questo codice per esportare un JasperReprot a XLS:JasperReports 5.6: JRXlsExporter.setParameter è deprecato

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

Aggiornamento a JasperReports 5,6 tutti setParameter sono contrassegnati come "deprecato" e non riesco a trovare la documentazione per adattare questo codice.

Come esportare un report in xls con JasperReports 5.6?

+2

Si consiglia di leggere [avviso sviluppatori] (http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRAbstractExporter.html#setParameter%28net.sf.jasperreports.engine.JRExporterParameter,% 20java.lang.Object% 29): 'Sostituito da setE xporterInput (ExporterInput), setConfiguration (ExporterConfiguration), setConfiguration (ReportExportConfiguration) e setExporterOutput (ExporterOutput) '. –

+0

Puoi anche trovare molto campione nel pacchetto * JR * –

risposta

62

JRExporter è diventato obsoleto in 5.6. Hanno introdotto la nuova interfaccia Esportatore e hanno adattato a posteriori tutti gli esportatori per avere ExporterInput, ReportExportConfiguration, ExporterConfiguration, ExporterOutput. Vedi sotto collegamento

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

Ciò significa che invece di setParameter, è necessario creare la configurazione utilizzando le classi di cui sopra o loro classi figlie PDF esempio di esportazione. Excel export dovrebbe seguire la stessa metodologia

JRPdfExporter exporter = new JRPdfExporter(); 

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

exporter.exportReport(); 

Excel controparte

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 avrà eccellere configurazione relative esportazioni. Impostare i valori secondo il vostro requisito

+0

Per il flusso di output, se stai usando una classe come 'ByteArrayOutputStream' dovresti fare questo:' exporter.setExporterOutput (new SimpleOutputStreamExporterOutput (os)); ' – CorayThan

+0

una riga "exporter.exportReport();" manca per la controparte excel – karansky

2

Grazie al codice di cui sopra, qui è il mio codice: Avviso: Export Excel con 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

Ecco il mio codice:

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