【问题标题】:JasperReport concatenation only prints the first reportJasperReport 连接只打印第一个报告
【发布时间】:2019-09-12 11:01:59
【问题描述】:

我正在尝试将两个报告与 JasperReports 连接,我有一个列表,其中包含来自报告“A”和报告“B”的两个 jasperPrints。问题是它只打印第一个报告,第二页是空的。我虽然问题出在报告“B”中,但如果我先打印报告“B”,我也会遇到同样的问题:报告“B”被打印,而应该是报告“A”的第二页是空的。

这是我的代码:

public static InputStream generatePdfByteArrayFromJasper(List<InputStream> reportStreams, Collection dataSource, HashMap<String, Object> parameters)
throws JRException {
    List<JasperPrint> jasperPrints = new ArrayList<>();
    JRDataSource datasource = new JRBeanCollectionDataSource(dataSource, true);
    for (InputStream is : reportStreams) {
        JasperPrint jasperPrint = JasperFillManager.fillReport(is, parameters, datasource);
        jasperPrints.add(jasperPrint);
    }
    JRPdfExporter exporter = new JRPdfExporter();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SimpleOutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(out);
    SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
    configuration.setCreatingBatchModeBookmarks(true);
    exporter.setConfiguration(configuration);
    exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrints));
    exporter.setExporterOutput(exporterOutput);
    exporter.exportReport();
    return new ByteArrayInputStream(out.toByteArray());
}

有人可以帮助我吗? 谢谢!

【问题讨论】:

    标签: java jasper-reports


    【解决方案1】:

    不要对两个报告使用相同的数据源对象。数据源被第一个报表消耗,第二个报表将没有记录。

    您应该为每个报表创建一个数据源实例。对参数映射做同样的事情是个好主意,因为填充过程会将内置参数填充到映射中,并且存在由第一个报告设置的参数最终被用于第二个报告的风险。

    所以代码看起来像这样:

    public static InputStream generatePdfByteArrayFromJasper(List<InputStream> reportStreams, Collection dataSource, HashMap<String, Object> parameters)
    throws JRException {
        List<JasperPrint> jasperPrints = new ArrayList<>();
        for (InputStream is : reportStreams) {
            JRDataSource datasource = new JRBeanCollectionDataSource(dataSource, true);
            HashMap<String, Object> reportParameters = new HashMap<>(parameters);
            JasperPrint jasperPrint = JasperFillManager.fillReport(is, reportParameters, datasource);
            jasperPrints.add(jasperPrint);
        }
    

    【讨论】:

      猜你喜欢
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      相关资源
      最近更新 更多