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

JRBeanCollectionDatasource prints only first bean's propery values

我正在处理一个简单的 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();
    }
}

豆子class,

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 class,

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 贷方和借方值未打印,这是为什么?

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