使用JasperReportBuilder获取DynamicReports的空输出文件

时间:2018-03-28 09:28:12

标签: java jasper-reports dynamic-reports

我正在使用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 + '}';
    }
  }

1 个答案:

答案 0 :(得分:3)

有什么问题?

您犯了一个小错误(看起来像复制和粘贴问题) - 您正在从未正确初始化 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()方法时都会创建新对象。这里没有单身人士或静态成员。