使用 JasperReportBuilder 获得带有 DynamicReports 的空输出文件

Got empty output file with DynamicReports using JasperReportBuilder

我正在使用 DynamicReports 4.1.1。因为我依赖 Java 1.6。问题是,无论使用哪种导出格式,我的所有报告都是空的。 pdf大约有1600 Bytes,这样写是没有问题的。但是没有内容。我读过如果数据源为空就会发生这种情况,但这里不是这种情况。有人有想法吗?

 private void build() {
    try {
        JRDataSource c = new JRBeanCollectionDataSource(createDataSource());
        report()
                .setTemplate(Templates.reportTemplate)
                .columns(
                col.column("Item", "item", type.stringType()),
                col.column("Quantity", "quantity", type.integerType()),
                col.column("Unit price", "unitPrice", type.bigDecimalType()))
                .title(Templates.createTitleComponent("CollectionDatasource"))
                .detailFooter(cmp.line())
                .pageFooter(Templates.footerComponent)
                .noData(Templates.createTitleComponent("NoData"), cmp.text("There is no data"))
                .setDataSource(c);
        report().toPdf(new FileOutputStream("report4.pdf"));
    } catch (DRException e) {
        e.printStackTrace();
    }
}

private List<Data> createDataSource() {
    List<Data> data = new ArrayList<Data>();
    data.add(new Data("DVD", 5, new BigDecimal(30)));
    data.add(new Data("Book", 8, new BigDecimal(11)));
    data.add(new Data("PDA", 2, new BigDecimal(15)));
    return data;
}

private class Data {

    private String item;
    private Integer quantity;
    private BigDecimal unitPrice;

    public Data(String item, Integer quantity, BigDecimal unitPrice) {
        this.item = item;
        this.quantity = quantity;
        this.unitPrice = unitPrice;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public BigDecimal getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(BigDecimal unitPrice) {
        this.unitPrice = unitPrice;
    }

    @Override
    public String toString() {
        return "Data{" + "item=" + item + ", quantity=" + quantity + ", unitPrice=" + unitPrice + '}';
    }
  }

怎么了?

您犯了一个小错误(看起来像 复制粘贴问题)- 您正在从未正确初始化的 JasperReportBuilder 对象生成代码。

您创建了 JasperReportBuilder 的两个实例 class:

report()
        .setTemplate(Templates.reportTemplate)
        .columns(
           col.column("Item", "item", type.stringType()),
           col.column("Quantity", "quantity", type.integerType()),
           col.column("Unit price", "unitPrice", type.bigDecimalType()))
        .title(Templates.createTitleComponent("CollectionDatasource"))
        .detailFooter(cmp.line())
        .pageFooter(Templates.footerComponent)
        .noData(Templates.createTitleComponent("NoData"), cmp.text("There is no data"))
        .setDataSource(c); // here is a first instance. It is initialized via builde. You should use instance for any actions
report().toPdf(new FileOutputStream("report4.pdf")); // the second instance. Just "blank" object, you are missed initializing via builder

您的情况下的有效代码为:

report()
        .setTemplate(Templates.reportTemplate)
        .columns(
           col.column("Item", "item", type.stringType()),
           col.column("Quantity", "quantity", type.integerType()),
           col.column("Unit price", "unitPrice", type.bigDecimalType()))
        .title(Templates.createTitleComponent("CollectionDatasource"))
        .detailFooter(cmp.line())
        .pageFooter(Templates.footerComponent)
        .noData(Templates.createTitleComponent("NoData"), cmp.text("There is no data"))
        .setDataSource(c)
        .toPdf(new FileOutputStream("report4.pdf")); // we are not breaking the chain

或:

JasperReportBuilder report = report()
    .setTemplate(Templates.reportTemplate)
    .columns(
       col.column("Item", "item", type.stringType()),
       col.column("Quantity", "quantity", type.integerType()),
       col.column("Unit price", "unitPrice", type.bigDecimalType()))
    .title(Templates.createTitleComponent("CollectionDatasource"))
    .detailFooter(cmp.line())
    .pageFooter(Templates.footerComponent)
    .noData(Templates.createTitleComponent("NoData"), cmp.text("There is no data"))
    .setDataSource(c); // we are created design, set styles and datasource and so on for our builder

report.toPdf(new FileOutputStream("report4.pdf"));  // we are using the valid builder prepared at previous step

详情

这只是一个构建器模式,这里没有魔法。

我们来看看DynamicReports.report()方法的源码

public class DynamicReports {
    // some members

    public DynamicReports() {
    }

    public static JasperReportBuilder report() {
        return new JasperReportBuilder();
    }

如你所见,每次我们调用 DynamicReports.report() 方法时都会创建新对象。这里没有单例或静态成员。