How to show each subreport in a separate Excel sheet

mario picture mario · Sep 26, 2012 · Viewed 9.2k times · Source

I would like to create a report with iReport for MS Excel.

My report includes two subreports. I need each subreport in a separate Excel sheet, but the generated export file has all data in one sheet.

I don't know which settings for this option required is.

Answer

Alex K picture Alex K · Sep 26, 2012

You can solve this issue with help of net.sf.jasperreports.export.xls.break.after.row property.

You can set this net.sf.jasperreports.export.xls.break.after.row property for the "sheets break" element in report (for example, line or staticText element).

In my sample below I've put line element to the Group Footer band for generating new sheet in Excel for a new group.

Here is my sample, master report:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ..>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["path_to_subreport"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT id, addressId FROM document ORDER BY addressId]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="ADDRESSID" class="java.lang.Integer"/>
    <group name="addressGroup">
        <groupExpression><![CDATA[$F{ADDRESSID}]]></groupExpression>
        <groupHeader>
            <band height="53">
                <subreport>
                    <reportElement x="189" y="0" width="200" height="31">
                        <property name="net.sf.jasperreports.export.xls.break.after.row" value="&quot;true&quot;"/>
                    </reportElement>
                    <subreportParameter name="addressId">
                        <subreportParameterExpression><![CDATA[$F{ADDRESSID}]]></subreportParameterExpression>
                    </subreportParameter>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport_new_sheet_for_excel.jasper"]]></subreportExpression>
                </subreport>
                <staticText>
                    <reportElement x="0" y="33" width="100" height="20"/>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isItalic="true"/>
                    </textElement>
                    <text><![CDATA[ID]]></text>
                </staticText>
            </band>
        </groupHeader>
        <groupFooter>
            <band height="9">
                <line>
                    <reportElement x="1" y="5" width="100" height="1">
                        <property name="net.sf.jasperreports.export.xls.break.after.row" value="true"/>
                    </reportElement>
                    <graphicElement>
                        <pen lineWidth="0.0"/>
                    </graphicElement>
                </line>
            </band>
        </groupFooter>
    </group>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

My subreport:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <parameter name="addressId" class="java.lang.Integer" isForPrompting="false"/>
    <queryString>
        <![CDATA[SELECT city, street FROM address WHERE id = $P{addressId}]]>
    </queryString>
    <field name="CITY" class="java.lang.String"/>
    <field name="STREET" class="java.lang.String"/>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="258" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA["City: " + $F{CITY}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="258" y="0" width="297" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA["Street: " + $F{STREET}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The result will be:

The generated report in MS Excel, case of using net.sf.jasperreports.export.xls.break.after.row

The result without using of "break" element (with net.sf.jasperreports.export.xls.break.after.row) will be:

The generated report in MS Excel


You can find more about info in Advanced Excel Features post.