17

我需要汇总我在子报表中计数的项目。为此,我认为我需要将该变量的值添加到每次迭代的另一个变量中,或者按该值“增加”它。为每个组调用 subReport,我得到该组的总数。我需要添加变量值,而不是数据库列/字段。

returnValue从接收一个整数subReport,它本身就是子报告中的行数。我想获得总计,因为subReport我的主 SQL 查询中的不同结果(每个结果都针对一个 GROUP)被多次调用。我想将所有结果相加,但我得到了一个null值。我尝试将一个操作添加到subReportas a newreturnValue并选择Sum作为操作,但这也产生了一个null.


   <variable name="itemCount" class="java.lang.Integer" resetType="None"/>
   <variable name="grandCount" 
      class="java.lang.Integer" 
      incrementType="Group" 
      incrementGroup="ITEM_BUNDLE">
      <variableExpression><![CDATA[$V{itemCount}]]></variableExpression>
   </variable>

... <returnValue subreportVariable="countItems" toVariable="itemCount"/>

4

3 回答 3

7

将属性添加calculation="Sum"variable name="grandCount"

或作为参数传递grandCount给子报表

<subreportParameter name="grandCount">
<subreportParameterExpression><![CDATA[$P{grandCount}]]></subreportParameterExpression>
</subreportParameter>

在子报表中使用参数grantCount的 initialValue声明变量countItems

<variable name="countItems" .... >
   <variableExpression><![CDATA[$P{itemCount} + $P{grandCount}]]></variableExpression>
   <initialValueExpression><![CDATA[$P{grandCount}]]></initialValueExpression>
</variable>

并返回

<returnValue subreportVariable="countItems" toVariable="grandCount" calculation="Sum"/>
于 2009-11-17T09:40:43.407 回答
1

只有当波段(组)等于子报告所在的波段时,您才可能尝试增加变量(我将其命名为 totalSum)。为此,您需要报告中的一个字段来为您提供当前乐队(组)。

<variable name="totalSum" 
         class="java.lang.Integer" 
         resetType="Report" 
         incrementType="Group" 
         incrementGroup="ITEM_BUNDLE"
         calculation="Nothing">
 <variableExpression>
 <![CDATA[new Boolean($F{reportPart}.equals("The_band_with_the_subreport")).booleanValue() ? $V{returnValue} : $V{totalSum}]]>
 </variableExpression>
 <initialValueExpression>
           <![CDATA[new Integer(0)]]>
 </initialValueExpression>
</variable>

我不确定这是否有效,我没有上下文来测试它。但您也可以尝试第二种解决方案- 使用三个变量。例如,您将子报表返回的值(比如说returnValue)保存在一个变量中,并使用另外两个变量来保存总和 - 一个直到子报表被调用(比如说partialSum),第二个变量存储两个变量之间的总和returnValue 和 partialSum。我们称之为总和。然后你会有这样的总和:

<variable name="totalSum" 
         class="java.lang.Integer" 
         resetType="Report" 
         incrementType="Group" 
         incrementGroup="ITEM_BUNDLE"
         calculation="Nothing">
   <variableExpression>
        <![CDATA[$V{returnValue} +  $V{partialSum}]]>
   </variableExpression>
   <initialValueExpression>
           <![CDATA[new Integer(0)]]>
   </initialValueExpression>
</variable>

对于 partialSum,你会有这样的东西:

<variable name="partialSum" 
         class="java.lang.Integer"
         resetType="Report"
         calculation="Sum"
         incrementType="None">
    <variableExpression>
        <![CDATA[new Boolean($F{reportPart}.equals("The_band_with_the_subreport")).booleanValue() ? $V{returnValue} : new Integer(0)]]>
    </variableExpression>
    <initialValueExpression>
         <![CDATA[new Integer(0)]]>
    </initialValueExpression>
  </variable>

我希望这能有所帮助。从 iRport 直接在您要使用的报告上进行所有这些设置会更容易。

于 2009-11-17T09:54:56.333 回答
1

自从我使用 iReport 以来,我并不完全确定如何在 JRXML 中编写它。在 iReport 中,我创建了一个新变量,类类型为“整数”,计算类型为“系统”。计算类型在这里很重要。

在变量表达式中,您将需要类似 $V{grandCount} = $V{grandCount} + $V{itemCount}

注意:JasperReports 逐个区域呈现,因此您将无法在子报表区域之前的区域中使用 grandCount 变量。

希望我不会太晚

于 2009-11-12T13:08:41.803 回答