Jasper Reports:子报表内的页码不起作用

Jasper Reports: Page Number inside subreport does not work

我的应用程序中有几个类似的报表,因此我创建了一个基本结构,在标题中包含一个子报表,在页脚中包含另一个子报表。

问题是我有 70 个类似的报告,如果有一天我需要更改页脚结构,我不想更改 70 个报告,我宁愿只更改一个。

在页脚子报表中我有这个:

<?xml version="1.0" encoding="UTF-8"?>
<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="reportFooterV" pageWidth="550" pageHeight="650" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="550" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6a63a92f-0859-4b0b-84b8-6166c2fe0951">
    <property name="ireport.zoom" value="1.5"/>
    <property name="ireport.x" value="19"/>
    <property name="ireport.y" value="0"/>
    <parameter name="date" class="java.lang.String" isForPrompting="false"/>
    <title>
        <band height="19">
            <textField>
                <reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="0" y="0" width="191" height="19" forecolor="#999999"/>
                <textElement verticalAlignment="Bottom">
                    <font fontName="Roboto" size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Auto">
                <reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="352" y="0" width="99" height="19" forecolor="#999999"/>
                <textElement textAlignment="Right" verticalAlignment="Bottom">
                    <font fontName="Roboto" size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

但是变量 PAGE_NUMBER 不起作用,在页面中始终显示 1。

在主报告中,我在 pageFooter 区域有这个

<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="systemParametersSummary" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="properties.Messages" isIgnorePagination="true" uuid="6a63a92f-0859-4b0b-84b8-6166c2fe0951">

....
    <pageFooter>
        <band height="22">
            <subreport>
                <reportElement uuid="ac3cfc74-4e5a-45bc-945a-7ca6c82c4f6a" x="2" y="0" width="150" height="22"/>
                <subreportParameter name="date">
                    <subreportParameterExpression><![CDATA[$P{date}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportExpression><![CDATA[$P{footer}]]></subreportExpression>
            </subreport>
        </band>
    </pageFooter>
</jasperReport>

我不知道为什么我找不到解决方法,如果有人能帮助我...谢谢!

如果您仅将这些 sub-reports 用于 page-numbering,那么这就是您收到错误的原因。这些 sub-reports 中的每一个都只有 1 页长,因此它始终是第 1 页,共 1 页。

通常也不需要使用 sub-reports 以这种方式进行页码编号,因为调用 sub-reports 的开销通常比您从中获得的任何好处都值得。

我建议你摆脱你的 sub-reports,只让主报告做 heavy-lifting 例如通过将您的日期文本字段放在主报告页脚中并使用此代码进行页码,使用主报告引擎数据在完整报告中的页码:

<textField evaluationTime="Master">
    <reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="352" y="0" width="99" height="19" forecolor="#999999"/>
        <textElement textAlignment="Right" verticalAlignment="Bottom">
            <font fontName="Roboto" size="10" isBold="false"/>
    </textElement>
    <textFieldExpression><![CDATA[$V{MASTER_CURRENT_PAGE} + " of " + $V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
</textField>

因此,在您的主要报告中,您应该以:

<pageFooter>
        <band height="22">
            <textField>
            <reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="0" y="0" width="191" height="19" forecolor="#999999"/>
            <textElement verticalAlignment="Bottom">
                <font fontName="Roboto" size="10" isBold="false"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
        </textField>
        <textField evaluationTime="Master">
            <reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="352" y="0" width="99" height="19" forecolor="#999999"/>
            <textElement textAlignment="Right" verticalAlignment="Bottom">
                <font fontName="Roboto" size="10" isBold="false"/>
            </textElement>
            <textFieldExpression><![CDATA[$V{MASTER_CURRENT_PAGE} + " of " + $V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
        </textField>
    </band>
</pageFooter>

不要忘记,如果您在主报告标题栏中使用 sub-report 用于类似目的,那么您可能也应该 re-think 该方法。

如果有特定原因,您可以在 sub-report 中应用相同的逻辑,以根据原始问题使用。当您使用 Master page-numbering 时,这也会给出正确的结果,例如您的 sub-report 代码将如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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="reportFooterV" pageWidth="550" pageHeight="650" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="550" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6a63a92f-0859-4b0b-84b8-6166c2fe0951">
    <parameter name="date" class="java.lang.String" isForPrompting="false"/>
    <title>
        <band height="22">
            <textField>
                <reportElement x="0" y="0" width="191" height="19" forecolor="#999999" uuid="89a04d3d-73e0-4f28-9747-3206c4022769"/>
                <textElement verticalAlignment="Bottom">
                    <font fontName="Roboto" size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Master">
                <reportElement x="352" y="0" width="99" height="19" forecolor="#999999" uuid="89a04d3d-73e0-4f28-9747-3206c4022769"/>
                <textElement textAlignment="Right" verticalAlignment="Bottom">
                    <font fontName="Roboto" size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{MASTER_CURRENT_PAGE} + " of " + $V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

这是一个老问题,但现在有一个变量 MASTER_TOTAL_PAGES 在我的页脚子报表中起作用。

相应的 MASTER_CURRENT_PAGE 对我不起作用,但您可以轻松地从主报告中传入当前页码。