如何计算详细信息带跨越的页面数?

时间:2019-04-04 10:34:21

标签: jasper-reports

是否有可能我没有使用变量来动态计算页面详细信息带跨度?

1 个答案:

答案 0 :(得分:1)

页数是内置变量$V{PAGE_NUMBER},带有evaluationTime="Report"的文本字段将显示报告中所有页的计数,包括标题栏页面和摘要栏页面。如果这些页面(标题/摘要)是固定数字,则可以从$V{PAGE_NUMBER}

中减去

例如,您有1个页面标题带,没有摘要带,详细信息将使用的页面数为:

<textField evaluationTime="Report">
    <reportElement x="0" y="0" width="50" height="20" uuid="973faf10-8714-48de-ac22-b746b5f56b1a"/>
    <textElement textAlignment="Left" verticalAlignment="Middle"/>
    <textFieldExpression><![CDATA[$V{PAGE_NUMBER}-1]]></textFieldExpression>
</textField>

但是,如果这些不是固定的,那么标题带或摘要带将动态跨越多个页面,一种解决方案是声明一个可变性。

<variable name="detailBand_cnt" class="java.lang.Integer" incrementType="Page" calculation="Count">
    <variableExpression><![CDATA[1]]></variableExpression>
    <initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>

从0开始,每页以1递增(计数),这将起作用,因为仅在填充明细区域时才评估变量,它不会在明细区域之前开始,也不会在明细区域之后继续。

完整示例

使用空数据源运行,例如10条记录

<?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="CountDetailsPage" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isTitleNewPage="true" isSummaryNewPage="true" isSummaryWithPageHeaderAndFooter="true" uuid="1070117d-1c41-40e8-85cd-d04958412665">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <variable name="detailBand_cnt" class="java.lang.Integer" incrementType="Page" calculation="Count">
        <variableExpression><![CDATA[1]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="d5206823-c0fe-416a-aa9f-fdc6dc584f2b"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Title page 1]]></text>
            </staticText>
            <break>
                <reportElement x="0" y="38" width="100" height="1" uuid="465c7a1c-d59f-486d-8730-2e1a853f0b12"/>
            </break>
            <staticText>
                <reportElement x="0" y="50" width="100" height="20" uuid="7c726918-e7d7-4a47-a5f4-a87561ef08fb"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Title page 2]]></text>
            </staticText>
        </band>
    </title>
    <detail>
        <band height="125" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="550" height="20" uuid="ae3566d9-c2a2-41e5-99fa-cd184f1b1eee"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[On detail]]></text>
            </staticText>
        </band>
    </detail>
    <pageFooter>
        <band height="54" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="70" height="20" uuid="38b0a124-e61b-46cb-8b4e-60e2f4ccd066"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Detail Page " + $V{detailBand_cnt}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="70" y="0" width="50" height="20" uuid="c5f945f0-895d-4481-8895-6f945a070450"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[" of " + $V{detailBand_cnt}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="407" y="0" width="95" height="20" uuid="98da264d-689f-4382-99b2-f304e0202555"/>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Totale Page " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="502" y="0" width="50" height="20" uuid="973faf10-8714-48de-ac22-b746b5f56b1a"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[" of " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
    <summary>
        <band height="100" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="ac518697-d616-47a9-a029-dfbb0d5f7ea3"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Summary Page1]]></text>
            </staticText>
            <break>
                <reportElement x="0" y="30" width="100" height="1" uuid="6f92ae5a-2786-407f-b2bf-62c32287ae88"/>
            </break>
            <staticText>
                <reportElement x="0" y="40" width="100" height="20" uuid="0e61769d-5bd0-4224-b905-425e90e0d87b"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Summary Page 2]]></text>
            </staticText>
            <break>
                <reportElement x="0" y="60" width="100" height="1" uuid="6f92ae5a-2786-407f-b2bf-62c32287ae88"/>
            </break>
            <staticText>
                <reportElement x="0" y="70" width="100" height="20" uuid="0e61769d-5bd0-4224-b905-425e90e0d87b"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Summary Page 3]]></text>
            </staticText>
        </band>
    </summary>
</jasperReport>

结果(第3页,共7页)

result