JRBeanCollectionDatasource仅打印第一个bean的属性值

时间:2015-07-22 14:50:26

标签: java jasper-reports datasource

我正在处理一个简单的JRBeanCollection示例,我只是想打印 javabean集合的所有属性值到pdf报告。

问题是我的代码只打印了我创建的列表的第一个bean。

这是我写的所有代码,

public static void main(String[] args) {
    String fileName = "src/test/report2.jasper";
    String outFileName = "test.pdf";
    HashMap hm = new HashMap();
    JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(FundBeanFactory.createBeanCollection());
    try {
        JasperPrint print = JasperFillManager.fillReport(
                fileName,
                hm,
                beanCollectionDataSource);

        JRPdfExporter exporter = new JRPdfExporter();

        exporter.setExporterInput(new SimpleExporterInput(print));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outFileName));
        SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
        configuration.setCreatingBatchModeBookmarks(true);
        exporter.setConfiguration(configuration);
        exporter.exportReport();
    } catch (JRException e) {
        e.printStackTrace();
    }
}

bean类,

public class FundBean {

  private Double debit;
  private Double credit;

  public Double getCredit() {
      return credit;
  }

  public void setCredit(Double credit) {
      this.credit = credit;
  }

  public Double getDebit() {
      return debit;
  }

  public void setDebit(Double debit) {
      this.debit = debit;
  }
}

创建列表的beanFactory类

public class FundBeanFactory {

  public static List<FundBean> createBeanCollection(){
    List<FundBean> fundBeans   = new ArrayList<FundBean>();

    FundBean bean1 = new FundBean();
    bean1.setCredit(89201.12);
    bean1.setDebit(122392.23);

    FundBean bean2 = new FundBean();
    bean2.setCredit(95650.16);
    bean2.setDebit(787878.80);
    fundBeans.add(bean1);
    fundBeans.add(bean2);

    return fundBeans;
  }
 }

jrxml文件:

<parameter name="Credit" class="java.lang.Double"/>
<field name="debit" class="java.lang.Double"/>
<field name="credit" class="java.lang.Double"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="79" splitType="Stretch">
        <staticText>
            <reportElement mode="Opaque" x="61" y="17" width="420" height="43" backcolor="#999999" uuid="6878b8e7-ffdc-4465-842f-c1b6de0b5d87"/>
            <textElement>
                <font size="24"/>
            </textElement>
            <text><![CDATA[              Available Funds Test]]></text>
        </staticText>
    </band>
</title>
<pageHeader>
    <band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="151" splitType="Stretch">
        <staticText>
            <reportElement mode="Opaque" x="0" y="0" width="100" height="41" backcolor="#0033CC" uuid="2fca55e7-bfc3-4795-9735-5f4ca5b621e6"/>
            <textElement>
                <font size="24"/>
            </textElement>
            <text><![CDATA[Debits]]></text>
        </staticText>
        <staticText>
            <reportElement mode="Opaque" x="100" y="0" width="100" height="41" backcolor="#0066CC" uuid="25536a17-ca40-4256-bc82-fca3b79be2ab"/>
            <textElement>
                <font size="24"/>
            </textElement>
            <text><![CDATA[Credits]]></text>
        </staticText>
        <textField>
            <reportElement x="0" y="41" width="100" height="67" uuid="56004fd8-48e8-4cfe-9ecc-53324b94f8a2"/>
            <textFieldExpression><![CDATA[$F{debit}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="100" y="41" width="100" height="67" uuid="ba4e4ab6-4a0f-4486-b2e6-15ffc0d04808"/>
            <textFieldExpression><![CDATA[$F{credit}]]></textFieldExpression>
        </textField>
    </band>
</columnHeader>

未打印bean2信用卡和借记卡值,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

要输出报告行,您需要将字段放在报告的detail区域中。您使用的column header频段自然只打印一次。