【发布时间】:2020-06-02 00:34:17
【问题描述】:
我有一份包含 2 个波段的报告:
- 用于显示 Group=N 的行的详细信息带,即如果 Group N,则隐藏
- 组页脚,用于显示具有 Group=Y 的行的聚合,即如果 Group Y,则隐藏
两个波段都显示日期、名称和金额。组页脚聚合 Amount 字段。 我想禁止重复日期。
例如,给定以下数据:
+------------+------+-------+--------+
| Date | Name | Group | Amount |
+------------+------+-------+--------+
| 2020-06-01 | A | Y | 5 |
| 2020-06-01 | A | Y | 10 |
| 2020-06-01 | D | N | 2 |
| 2020-06-01 | Z | Y | 4 |
| 2020-06-02 | B | N | 1 |
| 2020-06-02 | G | Y | 6 |
| 2020-06-02 | G | Y | 3 |
+------------+------+-------+--------+
它应该显示:
+------------+------+--------+
| Date | Name | Amount |
+------------+------+--------+
| 2020-06-01 | A | 15 |
| | D | 2 |
| | Z | 4 |
| 2020-06-02 | B | 1 |
| | G | 9 |
+------------+------+--------+
使用 isPrintRepeatedValues="false",它会复制不同波段中的日期:
+------------+------+--------+
| Date | Name | Amount |
+------------+------+--------+
| 2020-06-01 | A | 15 |
| 2020-06-01 | D | 2 |
| | Z | 4 |
| 2020-06-02 | B | 1 |
| 2020-06-02 | G | 9 |
+------------+------+--------+
添加一个:
<group name="DateGroup">
<groupExpression><![CDATA[$F{Date}]]></groupExpression>
</group>
并使用表达式抑制日期字段:
<printWhenExpression><![CDATA[$V{DateGroup_COUNT} == 1]]</printWhenExpression>
也不起作用,如下图所示。我已经包含了 Group 和 DateGroup_Count 值用于调试:
+------------+------+--------+-------+-----------------+
| Date | Name | Amount | Group | DateGroup_COUNT |
+------------+------+--------+-------+-----------------+
| | A | 15 | Y | 2 |
| | D | 2 | N | 3 |
| | Z | 4 | Y | 4 |
| 2020-06-02 | B | 1 | N | 1 |
| | G | 9 | Y | 3 |
+------------+------+--------+-------+-----------------+
2020-06-01 应该显示在第一行,但在上面的 DateGroup_COUNT 值表示聚合带中的最后一行,因此不应显示 Date 字段。
请注意,以上可能在单个频段中实现。它代表了使用单一波段无法实现的报告的简化版本:
- 在每个波段中以不同方式计算“金额”列
- 在每个波段中有不同的字段,需要选择性地显示/隐藏。这些也会重叠
用于数据源的 CSV:
Date,Name,Group,Amount
2020-06-01,A,Y,5
2020-06-01,A,Y,10
2020-06-01,D,N,2
2020-06-01,Z,Y,4
2020-06-02,B,N,1
2020-06-02,G,Y,6
2020-06-02,G,Y,3
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="duplicatetest" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="datasuppressiontestdata"/>
<field name="Date" class="java.util.Date"/>
<field name="Name" class="java.lang.String"/>
<field name="Group" class="java.lang.String"/>
<field name="Amount" class="java.math.BigDecimal"/>
<variable name="groupTotal" class="java.math.BigDecimal" resetType="Group" resetGroup="Aggregate" calculation="Sum">
<variableExpression><![CDATA[$F{Amount}]]></variableExpression>
</variable>
<group name="DateGroup">
<groupExpression><![CDATA[$F{Date}]]></groupExpression>
</group>
<group name="Aggregate">
<groupExpression><![CDATA[$F{Group}]]></groupExpression>
<groupFooter>
<band height="30">
<printWhenExpression><![CDATA[EQUALS($F{Group}, "Y")]]></printWhenExpression>
<textField evaluationTime="Group" evaluationGroup="DateGroup" pattern="yyyy-MM-dd">
<reportElement isPrintRepeatedValues="false" x="0" y="0" width="130" height="30" printWhenGroupChanges="DateGroup">
<printWhenExpression><![CDATA[$V{DateGroup_COUNT} == 1]]></printWhenExpression>
</reportElement>
<textElement>
<paragraph lineSpacing="Fixed"/>
</textElement>
<textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="134" y="0" width="116" height="30"/>
<textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="260" y="0" width="100" height="30"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{groupTotal}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="458" y="0" width="100" height="30"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{DateGroup_COUNT}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="389" y="0" width="68" height="30"/>
<textFieldExpression><![CDATA[$F{Group}]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<columnHeader>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="130" height="30"/>
<text><![CDATA[Date]]></text>
</staticText>
<staticText>
<reportElement x="134" y="0" width="116" height="30"/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="260" y="0" width="100" height="30"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Amount]]></text>
</staticText>
<staticText>
<reportElement x="458" y="0" width="100" height="30"/>
<textElement textAlignment="Right"/>
<text><![CDATA[DateGroup_COUNT]]></text>
</staticText>
<staticText>
<reportElement x="390" y="0" width="68" height="30"/>
<text><![CDATA[Group]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="40" splitType="Stretch">
<printWhenExpression><![CDATA[!EQUALS($F{Group}, "Y")]]></printWhenExpression>
<textField evaluationTime="Group" evaluationGroup="DateGroup" pattern="yyyy-MM-dd">
<reportElement isPrintRepeatedValues="false" x="0" y="0" width="130" height="30" printWhenGroupChanges="DateGroup">
<printWhenExpression><![CDATA[$V{DateGroup_COUNT} == 1]]></printWhenExpression>
</reportElement>
<textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="134" y="0" width="116" height="30"/>
<textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="260" y="0" width="100" height="30"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="458" y="0" width="100" height="30"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{DateGroup_COUNT}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="389" y="0" width="68" height="30"/>
<textFieldExpression><![CDATA[$F{Group}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
【问题讨论】:
-
我不认为建议的链接答案直接适用。第一个答案是一个很好的解决不同问题的方法,并提示我添加一个 DateGroup,但这仍然不能正确处理聚合带中的 Date 字段的显示。在这个阶段,我看不出第二个链接的答案是相关的。
标签: jasper-reports