【问题标题】:How to add collection of java bean in table in jaspersoft report [duplicate]如何在 jaspersoft 报告中的表中添加 java bean 的集合[重复]
【发布时间】:2015-12-23 09:36:14
【问题描述】:

我正在使用 jaspersoft 进行报告。我需要在表中添加我的 java bean 字段集合,我使用 java bean 集合作为数据适配器。我能够显示作为 java bean 获得的数据,但我没有找到将这些字段添加到 jaspersoft 报告中的表的方法。请帮助我从过去的几个数据中一直在解决这个问题,但我仍然没有解决这个问题。以下是我正在尝试的源代码。

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2015-12-23T15:07:03 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="javabeantest1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="78330407-cdc3-4758-b2a3-2ddae526bed9">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="test factory3"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="javabean" uuid="adf401bd-6c3e-4316-8ff1-285d86d56c48">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="TestFactory1"/>
        <queryString language="SQL">
            <![CDATA[]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <field name="age" class="java.lang.Integer"/>
    </subDataset>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="age" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="87" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="33" splitType="Stretch">
            <staticText>
                <reportElement x="70" y="3" width="100" height="30" uuid="2c3c0b2c-ef69-423b-ba00-47bfb52dfade"/>
                <text><![CDATA[name]]></text>
            </staticText>
            <staticText>
                <reportElement x="170" y="1" width="100" height="30" uuid="8c4db8c2-212f-4427-89b7-5eaf3091a1aa"/>
                <text><![CDATA[age]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="31" splitType="Stretch">
            <textField>
                <reportElement x="70" y="0" width="100" height="30" uuid="39fd9f41-27dd-406f-aa32-d6e930dc346a"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="170" y="0" width="100" height="30" uuid="1c916b19-cb2f-481c-8d0d-349eced5a8a2"/>
                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="249" splitType="Stretch">
            <componentElement>
                <reportElement x="290" y="49" width="200" height="200" uuid="0d721a59-5052-42a4-bd71-b61e51308115">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="javabean" uuid="31830583-c665-4c8d-af3a-1a5a0aba5306">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource() ]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="89affd55-2820-4860-a01c-ce794c5941a9">
                        <jr:tableHeader style="Table 1_TH" height="30"/>
                        <jr:tableFooter style="Table 1_TH" height="30"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="33b03723-89a8-4eba-b08a-e9360ecf3e08"/>
                                <text><![CDATA[name]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table 1_CH" height="30"/>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="54e2604e-96f8-4e83-b506-ca2beeef5751"/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="41fcbd02-87dc-4a72-8464-d9609e5e592a">
                        <jr:tableHeader style="Table 1_TH" height="30"/>
                        <jr:tableFooter style="Table 1_TH" height="30"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="f4078108-0c94-406e-907f-d20a0932afeb"/>
                                <text><![CDATA[age]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table 1_CH" height="30"/>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="b1250d44-7bb0-445f-8c46-ab6c880872a8"/>
                                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </summary>
</jasperReport>

【问题讨论】:

  • 嗨,Alex K,很久以前就有人问过这个问题

标签: java jasper-reports


【解决方案1】:

您的 java bean 需要是这样的(对应于您的 subDataset 命名的 javabean)。

public class JavaBean {
  private String name;
  private int age;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}

那么你需要一个List&lt;JavaBean&gt;的JavaBean。

假设您在填充过程中将此List 作为参数传递(在带有键listOfJavaBeans 的参数映射中)。

jrxml定义参数

<parameter name="listOfJavaBeans" class="java.util.List"/> 

将其传递给您的表作为数据源使用

<datasetRun subDataset="javabean" uuid="31830583-c665-4c8d-af3a-1a5a0aba5306">
    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listOfJavaBeans})]]></dataSourceExpression>
</datasetRun>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2017-10-20
    相关资源
    最近更新 更多