在iReport中将子报表中的值返回到主报表 [英] Returning a value from Sub Report to Main Report in iReport

查看:74
本文介绍了在iReport中将子报表中的值返回到主报表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我每次使用iReports生成其中一个报告并停留在一个地方。

Hi every one I am using iReports for generating one of the reports and stuck at one place.

情况如下:

我在主报表中使用了一个子报表,并且我希望在执行查询后将变量(浮点数)从子报表返回到主报表。而我只是将空值返回主报告我已经浪费了2天谷歌搜索和搜索但问题仍然存在..

I am using one sub report in my main report and i want to return a variable (float) back to the main report from sub report after the query has been executed. And i'm just getting null values back to main report i have wasted 2 days googling and searching but problem is still there..

贝娄是我的JRXMLS的虚拟代码(完全相同)和快照......

bellow is my dummy code of my JRXMLS (perfectly same) and snaps...

主要报告JRXML

  <?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="anuj" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
            <defaultValueExpression><![CDATA["/home/anuj/Reports/"]]></defaultValueExpression>
        </parameter>
        <queryString language="SQL">
            <![CDATA[select * from "SensorType"]]>
        </queryString>
        <field name="SensorTypeId" class="java.lang.Integer"/>
        <field name="SensorTypeName" class="java.lang.String"/>
        <variable name="A" class="java.lang.Integer" resetType="None" calculation="System"/>
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
            <band splitType="Stretch"/>
        </title>
        <pageHeader>
            <band splitType="Stretch"/>
        </pageHeader>
        <columnHeader>
            <band splitType="Stretch"/>
        </columnHeader>
        <detail>
            <band height="23" splitType="Stretch">
                <textField>
                    <reportElement x="71" y="3" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{SensorTypeId}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="202" y="3" width="112" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{SensorTypeName}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
        <columnFooter>
            <band height="5" splitType="Stretch"/>
        </columnFooter>
        <pageFooter>
            <band height="1" splitType="Stretch"/>
        </pageFooter>
        <summary>
            <band height="42" splitType="Stretch">
                <subreport>
                    <reportElement x="183" y="16" width="257" height="26"/>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    <returnValue subreportVariable="A" toVariable="A"/>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "anuj_subreport1.jasper"]]></subreportExpression>
                </subreport>
                <textField>
                    <reportElement x="71" y="22" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$V{A}]]></textFieldExpression>
                </textField>
            </band>
        </summary>
    </jasperReport>

子报告JRXML

<?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="anuj_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="149"/>
    <queryString>
        <![CDATA[select Sum("SensorTypeId") from "SensorType";]]>
    </queryString>
    <field name="sum" class="java.lang.Long"/>
    <variable name="A" class="java.lang.Integer" resetType="None" calculation="System">
        <variableExpression><![CDATA[$F{sum}]]></variableExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="169" y="59" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[sum]]></text>
            </staticText>
            <textField>
                <reportElement x="216" y="59" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{sum}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
        <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

提前致谢..

推荐答案

在子报表变量中,您有 calculate = System 。当您使用scriptlet自己计算变量时,应该使用此方法。您没有附加到报表的相应scriptlet,因此永远不会计算变量。

In the subreport variable, you have calculation=System. This should be used when you are calculating the variable yourself using a scriptlet. You do not have an appropriate scriptlet attached to the report, so the variable is never being calculated.

您可能希望更改变量定义,以便在子报表的开头,如下所示:

You probably want to change the variable definition so that it is calculated at the start of the subreport, something like this:

<variable name="A" class="java.lang.Integer" resetType="Report" calculation="Nothing">
    <variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>

resetType =报告永远不会重置报告运行时的变量。
calculation =Nothing将评估数据集中每一行的variableExpression。

resetType="Report" will never reset the variable while the report is running. calculation="Nothing" will evaluate the variableExpression for every row in the dataset.

这些属性虽然可以省略,因为它们是使用的默认值。然后,您应该发现变量已正确初始化。

These attributes could be omitted though, as they are the default values used. You should then find that the variable is initialised correctly.

这篇关于在iReport中将子报表中的值返回到主报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆