如何在“打印表达式”时使用动态变量?

时间:2013-10-01 02:42:27

标签: jasper-reports ireport

我正在使用 iReport 制作PDF报告 我的目标是想在我的3个对象元素中使用动态变量(变量名称 pgnum ),并使用 pgnum < / strong>在“print when expression”对象属性中 我有三个对象元素,它们是文本字段,帧名称的frameA和帧名称的frameB。

我想在表达式中打印出类似的东西:

  • text field =在表达式时打印: $ V {pgnum}%6 == 1
  • frameA =表达时打印: $ V {pgnum}%6 == 1
  • frameB =表达时打印: $ V {pgnum}%6!= 1

如果我的PDF总共有14页,那么我想要的结果是:

  • 当第7页和第13页时,将打印文本字段和frameA。
  • 另一页(第7页和第13页除外)只打印frameB。

我在 iReport 中一​​直尝试过但 pgnum 始终是静态结果,即使在文本字段中,评估时间属性也是每一页。并且框架A也永远不会打印,因为我认为 pgnum 始终设置为零(0)值,并且永远不会增量。 因此结果是框架B始终​​在所有页面中打印。

您能帮助我使用 iReport 解决此问题吗? 或者你能建议甚至帮助我使用其他jasper报告,比如动态报告或动态jasper来解决这个问题吗?

NB:

  • 动态变量的名称是pgnum。
  • 我的pgnum初始值为0(零)
  • 并且具有此类属性=&gt; class =“java.lang.Integer”incrementType =“Page”calculation =“Sum”

感谢阅读。 我很快就会等你回复。

这是我的 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="deploy_details" language="groovy" pageWidth="502" pageHeight="842" columnWidth="502" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <style name="table_CH" mode="Opaque" backcolor="#999999">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <variable name="pgnum" class="java.lang.Integer" incrementType="Page" calculation="Count">
        <variableExpression><![CDATA[pgnum+1]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <detail>
        <band height="762" splitType="Stretch">
            <frame>
                <reportElement isPrintRepeatedValues="false" mode="Opaque" x="56" y="66" width="114" height="43" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" backcolor="#000000">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 == 1]]></printWhenExpression>
                </reportElement>
                <staticText>
                    <reportElement mode="Transparent" x="15" y="12" width="83" height="18" forecolor="#FFFFFF"/>
                    <textElement/>
                    <text><![CDATA[FrameA]]></text>
                </staticText>
            </frame>
            <frame>
                <reportElement isPrintRepeatedValues="false" mode="Opaque" x="185" y="66" width="114" height="43" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" backcolor="#000000">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 != 1]]></printWhenExpression>
                </reportElement>
                <staticText>
                    <reportElement mode="Transparent" x="15" y="12" width="83" height="18" forecolor="#FFFFFF"/>
                    <textElement/>
                    <text><![CDATA[FrameB]]></text>
                </staticText>
            </frame>
            <textField isStretchWithOverflow="true" evaluationTime="Page" isBlankWhenNull="true">
                <reportElement isPrintRepeatedValues="false" x="122" y="35" width="100" height="20" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 == 1]]></printWhenExpression>
                </reportElement>
                <box>
                    <topPen lineWidth="3.25"/>
                    <leftPen lineWidth="3.25"/>
                    <bottomPen lineWidth="3.25"/>
                    <rightPen lineWidth="3.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA["textField"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

感谢Alex K的回复,但你的答案仍然没有解决我的问题,我已经尝试并按照你的所有命令来解决这个问题,但我仍然得到静态值的pgnum(不是增量)。

你之前建议我使用dataSource,现在我已经在测试包中包含了新的jrxml和dataSource示例文件的问题。先生,我有一个新问题,我创建的结果(dummyData)总是在test.pdf中打印“空”,你能再次帮助我吗?你能解决我的主要目标吗?

谢谢 我在等你的回复

这是链接: dataSource, jrxml

1 个答案:

答案 0 :(得分:14)

你的变量的表达是错误的。

正确的表达式是,如果您想使用 0 值开始计算,并为每个新页面按 1 增加 pgnum 值:

<variable name="pgnum" class="java.lang.Integer" incrementType="Page">
    <variableExpression><![CDATA[$V{pgnum} + 1]]></variableExpression>
    <initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>

这是伪代码的类似物:

int pgnum = 0;

// iterating thru records
// ....
    if (isNewPageStart()) {
        pgnum = pgnum + 1; 
    }

如果要为 pgnum 设置初始值(例如,借助参数 initialValue ),并通过增加 pgnum 值每个新页面1 ,表达式为:

<parameter name="initialValue" class="java.lang.Integer" isForPrompting="false">
    <defaultValueExpression><![CDATA[5]]></defaultValueExpression>
</parameter>

<variable name="pgnum" class="java.lang.Integer" incrementType="Page">
    <variableExpression><![CDATA[$V{pgnum} + 1]]></variableExpression>
    <initialValueExpression><![CDATA[$P{initialValue}]]></initialValueExpression>
</variable>

这是伪代码的类似物:

int initialValue = 5;
int pgnum = initialValue;

// iterating thru records
// ....
    if (isNewPageStart()) {
        pgnum = pgnum + 1; 
    }

借助此表达式,您还可以为每个新页面将 pgnum 的值增加1:

<variable name="pgnum" class="java.lang.Integer" incrementType="Page" calculation="Count">
    <variableExpression><![CDATA[100]]></variableExpression>
    <initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>

variableExpression 应包含任何非null值。我设置了 100 值,但我可以设置例如 5 9


有关变量的信息

来自JasperReports Ultimate Guide的报价 - 主题变量

Variable Name
Just as for parameters and fields, the name attribute of the ‹variable› element is 
mandatory and allows referencing the variable by its declared name in report expressions.

Reset Type
The value of a report variable can change with every iteration, but it can be brought 
back to the value returned by its initial value expression at specified times during the
report-filling process. This behavior is controlled using the resetType attribute, 
which indicates when the variable should be reinitialized during the report-filling process.

There are five reset types for a variable:
* No reset: The variable will never be initialized using its initial value expression
and will only contain values obtained by evaluating the variable’s expressio (resetType="None").
* Report-level reset: The variable is initialized only once, at the beginning of the
 report-filling process, with the value returned by the variable’s initial value expression (resetType="Report").
* Page-level reset: The variable is reinitialized at the beginning of each new page (resetType="Page").
* Column-level reset: The variable is reinitialized at the beginning of each new column (resetType="Column").
* Group-level reset: The variable is reinitialized every time the group specified by 
the resetGroup attributes breaks (resetType="Group"). The default value for this attribute is resetType="Report".


Increment Type
This property lets you choose the exact moment to increment the variable. By default, 
variables are incremented with each record in the data source, but in reports with multiple 
levels of data grouping, some variables might calculate higher-level totals and would need to 
be incremented only occasionally, not with every iteration through the data source. 

This attribute uses the same values as the resetType attribute, as follows:
* Row-level increment: The variable is incremented with every record during the iteration through the data source (incrementType="None").
* Report-level increment: The variable never gets incremented during the report-filling process (incrementType="Report").
* Page-level increment: The variable is incremented with each new page (incrementType= "Page").
* Column-level increment: The variable is incremented with each new column (incrementType="Column").
* Group-level increment: The variable is incremented every time the group specified by the incrementGroup attributes breaks (incrementType="Group").

CALCULATIONS
As mentioned, variables can perform built-in types of calculations on their corresponding
expression values. The following subsections describe all the possible values for the 
calculation attribute of the  element.

Calculation Nothing
This is the default calculation type that a variable performs. It means that the variable’s
value is recalculated with every iteration in the data source and that the value returned is
obtained by simply evaluating the variable’s expression.

Calculation Count
A count variable includes in the count the non-null values returned after evaluating the
 variable’s main expression, with every iteration in the data source. Count variables must
 always be of a numeric type. However, they can have non-numeric expressions as their main
 expression since the engine does not care about the expression type, but only counts for the
 non-null values returned, regardless of their type.

Only the variable’s initial value expression should be numeric and compatible with the
 variable’s type, since this value will be directly assigned to the count variable when initialized.

Calculation DistinctCount
This type of calculation works just like the Count calculation, the only difference being
 that it ignores repeating values and counts only for distinct non-null values.

Calculation Sum
The reporting engine can sum up the values returned by the variable’s main expression if you
 choose this type of calculation; but make sure the variable has a numeric type. You cannot
 calculate the sum of a java.lang.String or java.util.Date type of report variable unless a
 customized variable incrementer is used, as explained in the “Incrementers” section later in this chapter.

Calculation Average
The reporting engine can also calculate the average for the series of values obtained by
 evaluating the variable’s expression for each record in the data source. This type of
 calculation can be performed only for numeric variables (see the following “Incrementers”
 section, later in this chapter for details). 

Calculation Lowest and Highest
Choose this type of calculation when you want to obtain the lowest or highest value in the
 series of values obtained by evaluating the variable’s expression for each data source record.

Calculation StandardDeviation and Variance
In some special reports, you might want to perform more advanced types of calculations on
 numeric expressions. JasperReports has built-in algorithms to obtain the standard deviation
 and the variance for the series of values returned by evaluation of a report variable’s expression.

Calculation System
This type of calculation can be chosen only when you don’t want the engine to calculate any
 value for your variable. That means you are calculating the value for that variable
 yourself, almost certainly using the scriptlets functionality of JasperReports.

For this type of calculation, the only thing the engine does is to conserve the value you
 have calculated yourself, from one iteration in the data source to the next.

Calculation First
When using the calculation type First, the variable will keep the value obtained after the 
first incrementation and will not change it until the reset event occurs.

Here is a simple report variable declaration that calculates the sum for a numeric report
field called Quantity:
    ‹variable name="QuantitySum" class="java.lang.Double" calculation="Sum"›
        ‹variableExpression>$F{Quantity}‹/variableExpression›
    ‹/variable›
If you want the sum of this field for each page, here’s the complete variable declaration:
    ‹variable name="QuantitySum" class="java.lang.Double" resetType="Page" calculation="Sum"›
        ‹variableExpression›$F{Quantity}‹/variableExpression›
        ‹initialValueExpression>new Double(0)‹/initialValueExpression›
    ‹/variable›
In this example, our page sum variable will be initialized with zero at the beginning of each new page.

在您的情况下,工作模板将是:

<?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="deploy_details" language="groovy" pageWidth="502" pageHeight="842" columnWidth="502" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="7e1f4be9-dd35-4266-9eb3-ca71387dda65">
    <style name="table_CH" mode="Opaque" backcolor="#999999">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <variable name="pgnum" class="java.lang.Integer" incrementType="Page">
        <variableExpression><![CDATA[$V{pgnum} + 1]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <detail>
        <band height="762" splitType="Stretch">
            <frame>
                <reportElement uuid="e15c7831-1b8e-4f62-beb2-61e268dfbd73" mode="Opaque" x="56" y="66" width="114" height="43" backcolor="#000000">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 == 1]]></printWhenExpression>
                </reportElement>
                <staticText>
                    <reportElement uuid="ac19b45c-2ff9-41a9-9dd4-8189e51c7bdc" x="15" y="12" width="83" height="18" forecolor="#FFFFFF"/>
                    <textElement markup="none"/>
                    <text><![CDATA[FrameA]]></text>
                </staticText>
            </frame>
            <frame>
                <reportElement uuid="983ad5a2-5d79-4b6d-b1de-41428e4c7ccb" mode="Opaque" x="185" y="66" width="114" height="43" backcolor="#000000">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 != 1]]></printWhenExpression>
                </reportElement>
                <staticText>
                    <reportElement uuid="bbd99be7-18c1-4e11-a77e-6ab9e5b79500" x="15" y="12" width="83" height="18" forecolor="#FFFFFF"/>
                    <textElement/>
                    <text><![CDATA[FrameB]]></text>
                </staticText>
            </frame>
            <textField isStretchWithOverflow="true" evaluationTime="Page" isBlankWhenNull="true">
                <reportElement uuid="ba06215f-ef20-463c-baf3-891c33472f72" isPrintRepeatedValues="false" x="122" y="35" width="100" height="20" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true">
                    <printWhenExpression><![CDATA[$V{pgnum} % 6 == 1]]></printWhenExpression>
                </reportElement>
                <box>
                    <topPen lineWidth="3.25"/>
                    <leftPen lineWidth="3.25"/>
                    <bottomPen lineWidth="3.25"/>
                    <rightPen lineWidth="3.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA["textField"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

结果将是(通过 iReport 中的预览),页面#1:

enter image description here

对于第2页:

enter image description here