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

标签 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 贷方和借方值未打印,为什么会发生这种情况?

最佳答案

要输出报告行,您需要将字段放入报告的详细信息区域中。您已经使用了列标题区域,它自然只打印一次。

关于java - JRBeanCollectionDatasource 仅打印第一个 bean 的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31566652/

相关文章:

java - 使用 checkmarx 时 Servletoutputstream.write 存在 XSS 漏洞

sql-server - 如何通过 Windows 身份验证为 SQL Server 数据库正确设置 ColdFusion 数据源?

arrays - JDK 中是否有数组的 .java 源文件?

java - 如何在 Spring Data JPA 中编写具有多个可为空参数的查询?

java - 如何显示当前登录的用户 Firebase

java - JasperReports PDF 字体名称(jasperreports.properties 中的默认值)

java - 报告设计无效。未找到字段 Jasper Reports

spring jndi数据源设置

sql-server - WAS 8.5 在故障转移处理具有 JNDI 名称 XXX 的资源时无法找到主池管理器

java - 如何在多语言Android应用程序中获取选定的Radiobutton?