我正在使用DynamicReports 4.1.1。因为我依赖Java 1.6。问题是,无论使用哪种导出格式,我的所有报告都是空的。 pdf大约有1600字节,因此编写此类文件没有问题。但没有内容。我已经读到,如果数据源为空,则可能会发生这种情况,但是这里不是这种情况。有人有主意吗?

 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类的两个实例:

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()方法都会创建一个新对象。这里没有单例或静态成员。

09-25 20:38