Returned value from subeport to main report is nul

2019-01-20 15:01发布

问题:

I want to simply return a value from sub report to main report, but always shows null when the report is executed. Using iReport 5.1.0.

Main report

<?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="report4" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ce028d85-f7e8-4abd-ad6b-e3b2ba04d14e">
<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["C:\\Users\\DellXFR\\"]]></defaultValueExpression>
</parameter>
<variable name="z" class="java.lang.Integer" calculation="System">
    <variableExpression><![CDATA[8]]></variableExpression>
</variable>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="79" splitType="Stretch"/>
</title>
<detail>
    <band height="125" splitType="Stretch">
        <subreport>
            <reportElement uuid="0c501730-d98a-4982-9953-2939f127ad9e" x="40" y="10" width="200" height="100"/>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <returnValue subreportVariable="x" toVariable="z"/>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "report4_subreport1.jasper"]]></subreportExpression>
        </subreport>
        <textField evaluationTime="Band">
            <reportElement uuid="9020a8d9-5799-4907-b8a3-704f41ffdcc0" x="399" y="73" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$V{z}]]></textFieldExpression>
        </textField>
    </band>
</detail>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>
  </jasperReport>

Subreport

<?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="report4_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="78290b3a-34c2-496e-86ff-e213ca2c1039">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="SQL">
    <![CDATA[select * from ot;]]>
</queryString>
<field name="salscheme_id" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="ot_amount" class="java.lang.Double">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<variable name="x" class="java.lang.Integer">
    <variableExpression><![CDATA[1]]></variableExpression>
</variable>
<group name="salscheme_id">
    <groupExpression><![CDATA[$F{salscheme_id}]]></groupExpression>
</group>
<background>
    <band splitType="Stretch"/>
</background>
<detail>
    <band height="125" splitType="Stretch"/>
</detail>
    </jasperReport>

回答1:

Pass variables from subreport to main report as follows:

  1. In the main report, create a variable:

    <variable name="subReportValue" class="java.lang.Integer"/>
    

    Note: Do not set calculation value.

  2. In the subreport, also create a variable:

    <variable name="returnValue" class="java.lang.Integer" calculation="First">
      <variableExpression><![CDATA[$F{myField}]]></variableExpression>
    </variable>
    

    Set the calculation and what you like to return. Note: Respect the class value of the main report variable, they need to be same class.

  3. In the main report, set the subreport return value in the subreport tag:

    <returnValue subreportVariable="returnValue" toVariable="subReportValue"/>
    
  4. If you like to put the subReportValue in the detail band, just must set the evalutationTime of the textField, hence it needs to be after subreport (otherwise its still null).

    <textField evaluationTime="Band">
            <reportElement x="251" y="109" width="100" height="20"/>
            <textElement lineSpacing="Single"/>
            <textFieldExpression class="java.lang.Integer"><![CDATA[$V{subReportValue}]]></textFieldExpression>
    </textField>
    

If it is not in the detail band set evaluationTime="Report" for grouping. To understand the different evaluationTime see EvaluationTimeEnum

Re-compile the subreport when finished, since the main report references the compiled .jasper file, not the .jrxml source.



回答2:

set evaluation time of element to "band"