报告中的总计

时间:2014-09-23 05:33:11

标签: jasper-reports

这是我的Grand Total的代码,但它不会有总计?

<?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="Warehouse" language="groovy" pageWidth="612" pageHeight="792" columnWidth="572" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <queryString language="SQL">
        <![CDATA[select * from warehouse]]>
    </queryString>
    <field name="PlantUnit" class="java.lang.String"/>
    <field name="Description" class="java.lang.String"/>
    <field name="Size" class="java.lang.String"/>
    <field name="Qty" class="java.lang.String"/>
    <field name="Unit" class="java.lang.String"/>
    <field name="Unit Price" class="java.lang.String"/>
    <field name="Amount" class="java.lang.String"/>
    <variable name="TotalAmount" class="java.lang.String" calculation="Sum">
        <variableExpression><![CDATA[$F{Amount}]]></variableExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="40" splitType="Stretch">
            <staticText>
                <reportElement x="100" y="0" width="409" height="40"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="34" isBold="true"/>
                </textElement>
                <text><![CDATA[WAREHOUSE]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="15" splitType="Stretch">
            <textField pattern="MMMMM dd, yyyy">
                <reportElement x="457" y="0" width="116" height="15"/>
                <textElement textAlignment="Right">
                    <font fontName="Times New Roman" size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[new SimpleDateFormat("MMMM dd, yyyy").format(new java.util.Date())]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="12" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="50" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10" isBold="true"/>
                </textElement>
                <text><![CDATA[PU]]></text>
            </staticText>
            <staticText>
                <reportElement x="50" y="0" width="184" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10" isBold="true"/>
                </textElement>
                <text><![CDATA[DESCRIPTION]]></text>
            </staticText>
            <staticText>
                <reportElement x="234" y="0" width="150" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10" isBold="true"/>
                </textElement>
                <text><![CDATA[SIZE]]></text>
            </staticText>
            <staticText>
                <reportElement x="384" y="0" width="50" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10" isBold="true"/>
                </textElement>
                <text><![CDATA[QTY]]></text>
            </staticText>
            <staticText>
                <reportElement x="434" y="0" width="40" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10" isBold="true"/>
                </textElement>
                <text><![CDATA[UNIT]]></text>
            </staticText>
            <staticText>
                <reportElement x="473" y="0" width="50" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" isBold="true"/>
                </textElement>
                <text><![CDATA[COST]]></text>
            </staticText>
            <staticText>
                <reportElement x="523" y="0" width="50" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" isBold="true"/>
                </textElement>
                <text><![CDATA[AMOUNT]]></text>
            </staticText>
            <rectangle>
                <reportElement mode="Transparent" x="0" y="0" width="50" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="50" y="0" width="184" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="234" y="0" width="150" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="384" y="0" width="50" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="434" y="0" width="40" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="474" y="0" width="49" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="523" y="0" width="50" height="12"/>
            </rectangle>
        </band>
    </columnHeader>
    <detail>
        <band height="12" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="50" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{PlantUnit}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="50" y="0" width="184" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Description}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="234" y="0" width="150" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Size}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="384" y="0" width="50" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Qty}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="433" y="0" width="40" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman" size="10"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Unit}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="473" y="0" width="50" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Unit Price}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="523" y="0" width="50" height="12"/>
                <textElement textAlignment="Center">
                    <font fontName="Times New Roman"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
            </textField>
            <rectangle>
                <reportElement mode="Transparent" x="0" y="0" width="50" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="50" y="0" width="184" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="234" y="0" width="150" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="384" y="0" width="50" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="434" y="0" width="40" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="474" y="0" width="49" height="12"/>
            </rectangle>
            <rectangle>
                <reportElement mode="Transparent" x="523" y="0" width="50" height="12"/>
            </rectangle>
        </band>
    </detail>
    <columnFooter>
        <band height="12" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="12"/>
                <textElement>
                    <font fontName="Times New Roman" isBold="true"/>
                </textElement>
                <text><![CDATA[Total Amount]]></text>
            </staticText>
            <textField isBlankWhenNull="true">
                <reportElement x="473" y="0" width="100" height="12"/>
                <textElement textAlignment="Right">
                    <font fontName="Times New Roman"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{TotalAmount}]]></textFieldExpression>
            </textField>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="15" splitType="Stretch">
            <textField>
                <reportElement x="491" y="0" width="56" height="15"/>
                <textElement textAlignment="Right">
                    <font fontName="Times New Roman" size="12"/>
                </textElement>
                <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="547" y="0" width="25" height="15"/>
                <textElement>
                    <font fontName="Times New Roman" size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
    <summary>
        <band height="11" splitType="Stretch"/>
    </summary>
</jasperReport>

将不胜感激。

1 个答案:

答案 0 :(得分:0)

您已将amount字段作为字符串。

<field name="Amount" class="java.lang.String"/>

你的变量类型也是字符串。

<variable name="TotalAmount" class="java.lang.String" calculation="Sum">
        <variableExpression><![CDATA[$F{Amount}]]></variableExpression>
    </variable>

您需要将AmountTotalAmount变量设为Integer,以便jasper报告能够计算总和。因此将其改为以下:

<field name="Amount" class="java.lang.Integer"/>

<variable name="TotalAmount" class="java.lang.Integer" calculation="Sum">
        <variableExpression><![CDATA[$F{Amount}]]></variableExpression>
    </variable>

Note :如果Amount已经String,则将其转换为Integer,然后提供给jasper报告。