在iReport中将子报表的值返回到主报表

发布于 2024-12-03 08:12:15 字数 6204 浏览 2 评论 0原文

大家好,我正在使用 iReports 生成一份报告,但卡在一个地方。

情况是这样的:

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

下面是我的 JRXMLS 的虚拟代码(完全一样)和快照...

主报告 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>

提前致谢..

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

The situation is like this:

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..

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

Main Report 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>

Sub Report 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>

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

邮友 2024-12-10 08:12:15

尽管问题已经得到解答,但我想强调在主报表(调用子报表的报表)中的变量上使用calculation="System"的重要性。

在弄清楚这一点之前我浪费了很多时间...

在上面的主报告 JRXML 的虚拟代码中,变量“A”正确地具有 calculation="System"。注意:@GenericJon 的答案涵盖了 SubReport 中的变量。

Although the question is already answered, I would like to highlight the importance of having calculation="System" on the variable in the Main Report (the report calling the SubReport).

I wasted a lot of time before figuring this out...

In the dummy-code for Main Report JRXML above, the variable "A" correctly has calculation="System". NB: The answer by @GenericJon covers the variable in the SubReport.

盗心人 2024-12-10 08:12:15

在子报表变量中,您有 calculation=System。当您使用 scriptlet 自己计算变量时,应该使用它。您没有将适当的脚本附加到报告,因此永远不会计算该变量。

您可能想要更改变量定义,以便在子报表开始时计算它,如下所示:

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

resetType="Report" 在报表运行时永远不会重置变量。
calculation="Nothing" 将计算数据集中每一行的变量表达式。

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

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="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.

我们的影子 2024-12-10 08:12:15

来自 JasperReports 终极指南

仅当打印包含子报表的整个带区时,来自子报表的值才可用。如果您需要使用与子报表位于同一带中的文本字段来打印此值,请将文本字段的评估时间设置为 Band

From the JasperReports Ultimate Guide:

The value coming from the subreport is available only when the whole band containing the subreport is printed. If you need to print this value using a textfield placed in the same band as your subreport, set the evaluation time of the textfield to Band

ゃ人海孤独症 2024-12-10 08:12:15

我同意@bubba_hego99,计算类型如下:

系统不计算,只是设置为系统,或赋值为其他运算符。

什么都不用计算,只需设置变量即可。

sum 执行 sum($Feild("A")).

....

你想了解其他计算的细节,你应该去看ireport文档。

i am agree with @bubba_hego99, the calculation type as below:

system no caculation, just set as system, or valued as other operator.

nothing no calulation, just set the variable.

sum execute sum($Feild("A")).

....

The detail you want to know other caculation, you should be to see the ireport document.

断桥再见 2024-12-10 08:12:15

请注意,在 JasperReports 的早期版本中:

使用子报表returnValue时,返回的变量不得与子报表本身位于同一区域。

我认为这种矫揉造作是在乐队渲染结束时完成的。

因此,添加另一个带并在其中使用您的变量。

Note, in earlier versions of JasperReports:

When using a subreport returnValue, the variable returned must not be in the same band as the subreport itself.

The affectation is done I think at the end of the rendering of the band.

So add another band and use your variable inside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文