JasperReports 5.6:不推荐使用JRXlsExporter.setParameter

Tob*_*bia 41 java export jasper-reports deprecated

我有这个代码将JasperReprot导出到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();
Run Code Online (Sandbox Code Playgroud)

升级到JasperReports 5.6所有setParameter都被标记为"已弃用",我找不到适应此代码的文档.

如何使用JasperReports 5.6将报告导出到xls ?

San*_*hav 69

JRExporter在5.6中被弃用.他们引入了新的界面Exporter并对所有出口商进行了改造,使其具有ExporterInput,ReportExportConfiguration,ExporterConfiguration,ExporterOutput.见下面的链接

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

这意味着您需要使用上面提到的类或其子类PDF导出示例来代替setParameter.Excel导出应遵循相同的方法

JRPdfExporter exporter = new JRPdfExporter();

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

exporter.exportReport();
Run Code Online (Sandbox Code Playgroud)

Excel对应物

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();
Run Code Online (Sandbox Code Playgroud)

SimpleXlsReportConfiguration将具有excel导出相关配置.根据您的要求设置值

  • 对于输出流,如果你使用像 `ByteArrayOutputStream` 这样的类,你应该这样做: `exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(os));` (2认同)