JasperReports' iReport - generating a grand total

▼魔方 西西 提交于 2019-12-19 21:06:20

问题


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?


回答1:


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:




回答2:


Just create a new variable and in the variable expression add up all of the subtotals using their variable name

for instance:

$V{subtotal1}+$V{subtotal2}+$V{subtotal3}

After that, throw it in the 'summary' band and in the report properties check the box that says "summary on new page". That way it'll be on the last page of your report.



来源:https://stackoverflow.com/questions/9671761/jasperreports-ireport-generating-a-grand-total

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!