是否可以在页脚中控制Jasper Reports字段用法?

时间:2013-04-24 19:09:53

标签: jasper-reports

我正在尝试添加一个带有简单表达式的文本字段:

$F{foo}

到页面页脚区域。但是,无论我选择什么评估时间,它似乎总是从第一行显示字段foo的值。是否可以通过 last 行显示价值?

1 个答案:

答案 0 :(得分:1)

您可以尝试以下示例:

<?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="show_last_row_in_footer" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="106e6d12-70ca-44b4-a55d-845952f57346">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="id" class="java.lang.String"/>
    <sortField name="name"/>
    <variable name="firstValueOnPage" class="java.lang.String" resetType="Page" calculation="First">
        <variableExpression><![CDATA[$F{id}]]></variableExpression>
    </variable>
    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement uuid="5f3d2dfa-b44c-43eb-ac49-aa4ebb81b733" 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 textAlignment="Center">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Id]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="5f3d2dfa-b44c-43eb-ac49-aa4ebb81b733" x="100" 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 textAlignment="Center">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="50" splitType="Stretch">
            <textField>
                <reportElement uuid="55abd358-2770-4337-a117-3f8592ce0a34" x="100" y="0" width="100" height="50"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="55abd358-2770-4337-a117-3f8592ce0a34" x="0" y="0" width="100" height="50"/>
                <box leftPadding="10">
                    <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>
    <pageFooter>
        <band height="43" splitType="Stretch">
            <textField evaluationTime="Page">
                <reportElement uuid="e147d45b-45a3-4749-829f-b648a2c7805f" x="0" y="0" width="163" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA["The last id on page: " + $F{id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="bbf6f99a-c60c-42e9-8147-97b9f3ee4316" x="326" y="0" width="138" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA["The first id on page: " + $V{firstValueOnPage}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement uuid="e147d45b-45a3-4749-829f-b648a2c7805f" x="163" y="0" width="163" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA["The last id for report: " + $F{id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="9ecd61c0-98fa-4282-a0d1-cd8cd325f987" x="435" y="23" width="80" height="20"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement uuid="1c663c4d-1279-447a-91d6-6d03bc53a841" x="515" y="23" width="40" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
</jasperReport>

我已使用此 CSV数据源测试了样本:

Oslo,45
Berne,22
Boston,32
Chicago,39
Chicago,35
New York,44
Chicago,11
Dallas,47
Oslo,42
Dallas,43
Paris,5
San Francisco,48
Paris,18
Dallas,4
Boston,23
Dallas,0
Dallas,19
Dallas,10
Lyon,38
Lyon,2
Dallas,40
Dallas,36
Dallas,37
Lyon,28
Lyon,17
New York,46
New York,41
Paris,25
San Francisco,7
Berne,9

结果报告的第一页(内置于 iReport v 4.8.0 ):

The first page of report generated via iReport

结果报告的最后一页将是(内置于 iReport v 4.8.0 中):

enter image description here

要获取页面上的最后一行值(蓝色),我使用的表达式默认为 evaluationTime evaluationTime 等于页面价值。

为了获取页面上的第一行值(红色)我使用了具有以下属性的变量( firstValueOnPage ):
resetType 页面
计算第一

为了显示整个报告的最后一行(绿色)值,我使用 evaluationTime 等于报告值。


我使用过 iReport 4.8.0

我已经通过 name 字段添加了排序。没有排序,您将获得相同的数据行为。