2011-09-08 139 views
4

嗨,我正在使用iReports生成其中一個報表並將其鎖定在一個位置。在iReport中將子報表中的值返回給主報表

的情況是這樣的:

我用我的主要報告的一個子報告,我希望查詢執行後要返回一個變量(浮點)從子報告主報告。我只是空值回主報告我浪費了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> 

Sub報表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> 

在此先感謝..

回答

4

在子報表變量中,您有calculation=System。當你使用scriptlet自己計算變量時應該使用這個。您沒有在報告中附加適當的scriptlet,因此該變量不會被計算。

你可能想改變變量的定義,使其在報表開始計算的,這樣的事情:報告運行時

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

resetType="Report"永遠不會重置變量。 calculation="Nothing"將評估數據集中每一行的variableExpression。

儘管這些屬性可以省略,因爲它們是使用的默認值。然後您應該會發現該變量已正確初始化。

4

儘管問題已經得到解答,但我想強調calculation="System"主要報告(調用子報告的報告)中變量的重要性。

我浪費了很多時間搞清楚了這一點之前...

在對主報告JRXML上面的僞代碼中,變量「A」具有正確calculation="System"。注意:@GenericJon的答案涵蓋了SubReport中的變量。

1

我與@ bubba_hego99同意,鍵入計算如下:

系統中沒有計算研究,只是設置爲系統,或重視的其他運營商。

沒有沒有調整,只需設置變量。

sum execute sum($ Feild(「A」))。

....

你想知道其他計算的細節,你應該看到ireport文檔。

0

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

當使用報表returnValue,返回不能在同一頻段作爲子報表本身的變量。

情緒化完成我想在樂隊的渲染結束。

因此添加另一個樂隊並在裏面使用你的變量。

+0

實際上,您可以在同一個細節帶中使用'returnValue',至少從6.4開始。我目前使用它來控制樂隊的外觀(將'returnValue'設置爲一個變量,然後在'printWhenExpression'中使用該變量),並控制'textField'的內容(再次使用'returnValue'在'textFieldExpression'的邏輯中設置一個變量'$ V {something}','textField'的'evaluateTime ='Report'')。 – sboggs10

4

JasperReports Ultimate Guide

從子報表來的值可僅在打印含有該報表整個頻帶。如果您需要使用與您的子報告放置在同一波段的文本字段打印此值,請將文本字段的評估時間設置爲波段

相關問題