-1

标准的列布局可以用来实现这一点。如何为表格组件布局做到这一点?

4

1 回答 1

6

这很容易。您应该为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中的设计如下所示:

  • 主要报告视图:

在此处输入图像描述

  • 表格组件视图:

在此处输入图像描述

在此示例中,我删除了主要数据源并将Table组件放入Title带。我设置了 whenNoDataType="AllSectionsNoDetail"属性来显示报告。

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

于 2012-09-14T14:06:18.047 回答