JasperReports 5.6: JRXlsExporter.setParameter is deprecated

Tobia picture Tobia · Jun 9, 2014 · Viewed 35.1k times · Source

I have this code to export a JasperReprot to XLS:

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

Upgrading to JasperReports 5.6 all setParameter are flagged as "deprecated" and I can not find documentation to adapt this code.

How to export a report to xls with JasperReports 5.6?

Answer

Sangram Jadhav picture Sangram Jadhav · Jun 9, 2014

JRExporter became deprecated in 5.6. They introduced new interface Exporter and retrofitted all exporters to have ExporterInput, ReportExportConfiguration, ExporterConfiguration,ExporterOutput. See below link

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

This means that instead of setParameter, you need to create configuration using above mentioned classes or their child classes PDF export example. Excel export should follow same methodology

JRPdfExporter exporter = new JRPdfExporter();

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

exporter.exportReport();

Excel counterpart

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 will have excel export related configuration. Set values as per your requirement