如何使用Group By创建Jasper Report Table组件?

时间:2012-09-14 13:42:36

标签: jasper-reports

标准列布局可用于实现此目的。如何为表组件布局执行此操作?

1 个答案:

答案 0 :(得分:5)

这很容易。您应该为 Table 组件添加新数据源,然后在此组件中使用它(数据源)。

示例 jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ... whenNoDataType="AllSectionsNoDetail" ...>
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="tableDataset">
        <queryString language="SQL">
            <![CDATA[SELECT id, city, street FROM address ORDER BY city]]>
        </queryString>
        <field name="ID" class="java.lang.Integer"/>
        <field name="CITY" class="java.lang.String"/>
        <field name="STREET" class="java.lang.String"/>
        <group name="CITY">
            <groupExpression><![CDATA[$F{CITY}]]></groupExpression>
        </group>
    </subDataset>
    <title>
        <band height="58" splitType="Stretch">
            <componentElement>
                <reportElement key="table" style="table" x="0" y="0" width="299" height="46"/>
                <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="tableDataset">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="90">
                        <jr:groupHeader groupName="CITY">
                            <jr:cell height="30" rowSpan="1">
                                <textField>
                                    <reportElement x="0" y="0" width="90" height="30"/>
                                    <textElement/>
                                    <textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
                                </textField>
                            </jr:cell>
                        </jr:groupHeader>
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30"/>
                                <textElement/>
                                <text><![CDATA[Id]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="20" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90">
                        <jr:columnHeader style="table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30"/>
                                <textElement/>
                                <text><![CDATA[Street]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="20" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{STREET}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </title>
</jasperReport>

iReport 中的设计如下所示:

  • 主要报告视图:

enter image description here

  • 组件视图:

enter image description here

在此示例中,我删除了主数据源,并将 Table 组件放入 Title 频段。我已设置 whenNoDataType =“AllSectionsNoDetail” 属性以显示报告。

报告中的数据按 city 字段分组。