Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

jasper reports - JasperReports' iReport - generating a grand total

I have a variable datatype set up in the group footer band that generates a subtotal for the counts in each group in my report. Works great. I would like a grand total to generate on the last page of my report, simply summing up the subtotal values. This has been harder to figure out. Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use two variables with different resetType - for calculating sum in group (with resetType="Group" resetGroup="groupName" calculation="Sum" properties) and for calculating total sum for whole report (with resetType="Report" calculation="Sum" properties).

The sample (jrxml file):

<?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="grand_total_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[SELECT DOCUMENTID, POSITIONNO, PRODUCTID, QUANTITY FROM POSITIONS WHERE PRODUCTID < 10 AND POSITIONNO > 18 ORDER BY POSITIONNO]]>
    </queryString>
    <field name="DOCUMENTID" class="java.lang.Integer"/>
    <field name="POSITIONNO" class="java.lang.Integer"/>
    <field name="PRODUCTID" class="java.lang.Integer"/>
    <field name="QUANTITY" class="java.lang.Integer"/>
    <variable name="quantitySumInGroup" class="java.lang.Integer" resetType="Group" resetGroup="positionNoGroup" calculation="Sum">
        <variableExpression><![CDATA[$F{QUANTITY}]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <variable name="quantityTotalSum" class="java.lang.Integer" calculation="Sum">
        <variableExpression><![CDATA[$F{QUANTITY}]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <group name="positionNoGroup">
        <groupExpression><![CDATA[$F{POSITIONNO}]]></groupExpression>
        <groupHeader>
            <band height="20">
                <textField>
                    <reportElement x="0" y="0" width="400" 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" isUnderline="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA["Position no: " + $F{POSITIONNO}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
        <groupFooter>
            <band height="20">
                <textField>
                    <reportElement x="0" y="0" width="400" height="20"/>
                    <textElement>
                        <font isBold="false" isItalic="true" isUnderline="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA["Sum in group: " + $V{quantitySumInGroup}]]></textFieldExpression>
                </textField>
            </band>
        </groupFooter>
    </group>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{DOCUMENTID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{PRODUCTID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="200" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{QUANTITY}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <lastPageFooter>
        <band height="20">
            <textField>
                <reportElement x="0" y="0" width="400" height="20"/>
                <textElement>
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <textFieldExpression><![CDATA["Total sum: " + $V{quantityTotalSum}]]></textFieldExpression>
            </textField>
        </band>
    </lastPageFooter>
</jasperReport>

The result will be:

The result in iReport preview


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...