How to create Jasper Report Table component with G

2019-04-17 17:13发布

问题:

The standard column layout can use to achieve this. How can I do it for the table component layout?

回答1:

It is quite easy. You should add new datasource for Table component and then use it (datasource) in this component.

The sample 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>

The design in iReport looks like this:

  • The main report view:

  • The Table component view:

In this sample I've delete the main datasource and put the Table component to the Title band. I've set whenNoDataType="AllSectionsNoDetail" property for displaying report.

The data in report is grouped by city field.