JasperReport setParameter() 已弃用?
JasperReport setParameter() Deprecated?
我最近为我的项目升级了 Jasper Reports 库,从 3.7.6 升级到 6.0.0。我终于可以构建 Maven 并且报告工作得很好。但是,setParameter() 函数似乎在不同版本之间已被弃用,我不确定如何重构我的代码以适应这一点。
弃用代码示例:
private static void exportMultipleToCSV(Collection<JasperPrint> jasperPrints, OutputStream baos) throws JRException {
JRCsvExporter csvExporter = new JRCsvExporter();
csvExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints);
csvExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
csvExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, Integer.valueOf(1500000));
csvExporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, Integer.valueOf(40000000));
csvExporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, Integer.valueOf(4));
csvExporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, Integer.valueOf(15));
csvExporter.exportReport();
}
我浏览了 SourceForge 页面,可以看到它已被 ExporterInput、ExporterConfiguration 和ExporterOutput 但我不确定如何一起利用它们来实现所需的输出。
等效代码如下所示:
JRCsvExporter csvExporter = new JRCsvExporter();
//jasperPrints is Collection, but we need a List
csvExporter.setExporterInput(SimpleExporterInput.getInstance(new ArrayList(jasperPrints)));
csvExporter.setExporterOutput(new SimpleWriterExporterOutput(baos));
SimpleCsvExporterConfiguration exporterConfiguration = new SimpleCsvExporterConfiguration();
//nothing to set here, but you could do things like exporterConfiguration.setFieldDelimiter
csvExporter.setConfiguration(exporterConfiguration);
csvExporter.exportReport();
请注意,旧版 API 允许您执行 csvExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT)
之类的操作。问题是 CSV 导出器实际上并没有使用该参数,只有文本导出器正在查看 JRTextExporterParameter.PAGE_HEIGHT
。使用新的 API,您可以在每个导出器上做什么 settings/configuration 一目了然。
我最近为我的项目升级了 Jasper Reports 库,从 3.7.6 升级到 6.0.0。我终于可以构建 Maven 并且报告工作得很好。但是,setParameter() 函数似乎在不同版本之间已被弃用,我不确定如何重构我的代码以适应这一点。
弃用代码示例:
private static void exportMultipleToCSV(Collection<JasperPrint> jasperPrints, OutputStream baos) throws JRException {
JRCsvExporter csvExporter = new JRCsvExporter();
csvExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints);
csvExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
csvExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, Integer.valueOf(1500000));
csvExporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, Integer.valueOf(40000000));
csvExporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, Integer.valueOf(4));
csvExporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, Integer.valueOf(15));
csvExporter.exportReport();
}
我浏览了 SourceForge 页面,可以看到它已被 ExporterInput、ExporterConfiguration 和ExporterOutput 但我不确定如何一起利用它们来实现所需的输出。
等效代码如下所示:
JRCsvExporter csvExporter = new JRCsvExporter();
//jasperPrints is Collection, but we need a List
csvExporter.setExporterInput(SimpleExporterInput.getInstance(new ArrayList(jasperPrints)));
csvExporter.setExporterOutput(new SimpleWriterExporterOutput(baos));
SimpleCsvExporterConfiguration exporterConfiguration = new SimpleCsvExporterConfiguration();
//nothing to set here, but you could do things like exporterConfiguration.setFieldDelimiter
csvExporter.setConfiguration(exporterConfiguration);
csvExporter.exportReport();
请注意,旧版 API 允许您执行 csvExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT)
之类的操作。问题是 CSV 导出器实际上并没有使用该参数,只有文本导出器正在查看 JRTextExporterParameter.PAGE_HEIGHT
。使用新的 API,您可以在每个导出器上做什么 settings/configuration 一目了然。