【问题标题】:Print When Expression to print only the last element in the groupPrint When Expression 仅打印组中的最后一个元素
【发布时间】:2021-09-23 22:45:54
【问题描述】:

我在 oracle 表中有以下数据集,并希望从该数据生成 CSV 文件。

我的数据

CaseID 金额 1000 10 1000 20 1000 50 2000 30 2000 10 3000 30 3000 20 3000 20

有必要从上述数据中显示以下输出。

需要的输出

CaseID 金额合计 1000 10 1000 20 1000 50 80 2000 30 2000 10 40 3000 30 3000 20 3000 20 70

聚合列是通过根据 caseID 组累积 Amount 的总和来填充的。

组摘要应显示在组的最后一个元素上。但不是在下一行,应该显示在同一行和下一列,与最后一个元素匹配。

我尝试在其他场景中使用“Print When Expression”,但无法在此场景中使用。我也发现了类似的问题,但不满足这个要求。所以想知道可能的解决方案。

【问题讨论】:

    标签: jasper-reports


    【解决方案1】:

    您可以使用evaluationTime="Auto" 执行类似的操作。您可以有一个文本元素,该元素显示组中最后一条记录的值,而其他记录为空。这和根本不打印元素是不一样的,但是你不能使用 print when 表达式,因为它没有延迟评估。

    evaluationTime="Auto" 使用变量的重置类型来决定读取变量值的时刻。每个组都有一个自动创建的计数变量,该变量随组一起重置,如果您创建一个在每条记录上重置的新变量,您可以使用它来确定当前记录是否是组中的最后一条记录。

    整个报告看起来像这样

    <?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="report" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="jasperreports" whenResourceMissingType="Key" isIgnorePagination="true" uuid="816687ff-bb19-4f6b-a2b6-53634ce095fc">
        <style name="dd" isDefault="true" fontSize="10"/>
        <field name="CaseID" class="java.lang.Integer"/>
        <field name="Amount" class="java.lang.Integer"/>
        <variable name="AmountSum" class="java.lang.Integer" resetType="Group" resetGroup="CaseIDGroup" calculation="Sum">
            <variableExpression><![CDATA[$F{Amount}]]></variableExpression>
        </variable>
        <variable name="GroupCurrentCount" class="java.lang.Integer" resetType="None">
            <variableExpression><![CDATA[$V{CaseIDGroup_COUNT}]]></variableExpression>
        </variable>
        <group name="CaseIDGroup">
            <groupExpression><![CDATA[$F{CaseID}]]></groupExpression>
        </group>
        <detail>
            <band height="20">
                <textField>
                    <reportElement x="0" y="0" width="100" height="20" uuid="af7a5ea9-ffcb-4a7f-aaa8-7d5cab06a579"/>
                    <textFieldExpression><![CDATA[$F{CaseID}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="100" y="0" width="100" height="20" uuid="a7e2795e-456a-4c6e-946f-8315df453b1f"/>
                    <textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
                </textField>
                <textField evaluationTime="Auto" isBlankWhenNull="true">
                    <reportElement x="200" y="0" width="100" height="20" uuid="afda4fcc-78fc-46f9-8c5c-2e0c4f04dfa5"/>
                    <textFieldExpression><![CDATA[$V{GroupCurrentCount}.equals($V{CaseIDGroup_COUNT}) ? $V{AmountSum} : null]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>
    

    【讨论】:

    • 嗨dada67,非常感谢您的回答和解释。真的很有用。
    • 我用你的 jrxml 和 csv 数据源进行了测试,它提供了更好的方法来解决这个问题,唯一的问题是聚合列不提供金额的累积总和。相反,它仅显示 caseid 组中的最后一个元素值。我需要计算累计。
    • 上述示例中存在小错误,我已通过将计算=Sum 添加到 AmountSum 变量来纠正它。无论如何,谢谢你的方法。
    • 对,我错过了变量的计算类型。我编辑了我的回复,感谢您注意到错误。
    • 谢谢你,无论如何我已经投票并接受了基于你的方法的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多