【问题标题】:Group several same value field into a single cell将多个相同的值字段分组到一个单元格中
【发布时间】:2012-04-05 14:30:43
【问题描述】:

首先,记录按表格组件显示在表格中,而不是在报表中。

结果如下所示:

 YEARS MONTHS SUMMONTH SUMQUARTER   
 ----- ------ -------- ----------  
  2009 Jan      130984     432041
       Feb      146503
       Mar      154554     
       Apr      147917     435150 
       May      131822     
       Jun      155411     
       Jul      144000     424806 
       Aug      130369     
       Sep      150437     
       Oct      112137     400114 
       Nov      152057     
       Dec      135920     
 =====================================
       Jan-Dec  1692111
 =====================================
  2010 Jan      139927     417564 
       Feb      154940     
       Mar      122697     
       Apr      163257     413305 
       May      124999     
       Jun      125049     
       Jul      145127     427612 
       Aug      138804     
       Sep      143681     
       Oct      143398     406381 
       Nov      125351     
       Dec      137632     
 =====================================
       Jan-Dec  1664862
 =====================================

sumquarter 列显示一年中每个季度的总和。

重复字段值时不打印。

问题是如何对sumquarter的列进行分组,让每行第一个打印的重复值加入下一个重复值成为单个单元格,直到遇到非重复值?

您可以简单地在图像中看到它。 下面是表格显示的图像,我更喜欢的解决方案是将这 3 个月的总和分组到一个单元格中。

图片如下:

【问题讨论】:

    标签: jasper-reports ireport tabular


    【解决方案1】:

    您可以使用此示例:

    <?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="year_sum_quarter" 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"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="year" class="java.lang.Integer"/>
        <field name="month" class="java.lang.String"/>
        <field name="sum" class="java.lang.Integer"/>
        <field name="q" class="java.lang.Integer"/>
        <variable name="yearSum" class="java.lang.Integer" resetType="Group" resetGroup="yearGroup" calculation="Sum">
            <variableExpression><![CDATA[$F{sum}]]></variableExpression>
        </variable>
        <variable name="qSum" class="java.lang.Integer" resetType="Group" resetGroup="quaterGroup" calculation="Sum">
            <variableExpression><![CDATA[$F{sum}]]></variableExpression>
        </variable>
        <group name="yearGroup">
            <groupExpression><![CDATA[$F{year}]]></groupExpression>
            <groupFooter>
                <band height="20">
                    <textField>
                        <reportElement x="100" y="0" width="100" height="20"/>
                        <box leftPadding="10">
                            <topPen lineWidth="1.0"/>
                            <leftPen lineWidth="0.0"/>
                            <bottomPen lineWidth="1.0"/>
                            <rightPen lineWidth="0.0"/>
                        </box>
                        <textElement/>
                        <textFieldExpression><![CDATA["Jan-Dec, " + $F{year}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="200" y="0" width="100" height="20"/>
                        <box leftPadding="0">
                            <topPen lineWidth="1.0"/>
                            <leftPen lineWidth="0.0"/>
                            <bottomPen lineWidth="1.0"/>
                            <rightPen lineWidth="0.0"/>
                        </box>
                        <textElement/>
                        <textFieldExpression><![CDATA[$V{yearSum}]]></textFieldExpression>
                    </textField>
                    <staticText>
                        <reportElement x="0" y="0" width="100" height="20"/>
                        <box>
                            <topPen lineWidth="1.0"/>
                            <leftPen lineWidth="1.0"/>
                            <bottomPen lineWidth="1.0"/>
                            <rightPen lineWidth="0.0"/>
                        </box>
                        <textElement textAlignment="Center" verticalAlignment="Middle">
                            <font isBold="true" isItalic="true"/>
                        </textElement>
                        <text><![CDATA[]]></text>
                    </staticText>
                    <staticText>
                        <reportElement x="300" y="0" width="100" height="20"/>
                        <box>
                            <topPen lineWidth="1.0"/>
                            <leftPen lineWidth="0.0"/>
                            <bottomPen lineWidth="1.0"/>
                            <rightPen lineWidth="1.0"/>
                        </box>
                        <textElement textAlignment="Center" verticalAlignment="Middle">
                            <font isBold="true" isItalic="true"/>
                        </textElement>
                        <text><![CDATA[]]></text>
                    </staticText>
                </band>
            </groupFooter>
        </group>
        <group name="quaterGroup">
            <groupExpression><![CDATA[$F{year} + $F{q}]]></groupExpression>
        </group>
        <columnHeader>
            <band height="50">
                <staticText>
                    <reportElement x="100" y="30" width="100" height="20"/>
                    <box>
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true" isItalic="true"/>
                    </textElement>
                    <text><![CDATA[Month]]></text>
                </staticText>
                <staticText>
                    <reportElement x="0" y="30" width="100" height="20"/>
                    <box>
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true" isItalic="true"/>
                    </textElement>
                    <text><![CDATA[Year]]></text>
                </staticText>
                <staticText>
                    <reportElement x="200" y="30" width="100" height="20"/>
                    <box>
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true" isItalic="true"/>
                    </textElement>
                    <text><![CDATA[Month Sum]]></text>
                </staticText>
                <staticText>
                    <reportElement x="300" y="30" width="100" height="20"/>
                    <box>
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true" isItalic="true"/>
                    </textElement>
                    <text><![CDATA[Quarter Sum]]></text>
                </staticText>
            </band>
        </columnHeader>
        <detail>
            <band height="20" splitType="Stretch">
                <staticText>
                    <reportElement x="300" y="0" width="100" height="20"/>
                    <box>
                        <topPen lineWidth="0.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="0.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true" isItalic="true"/>
                    </textElement>
                    <text><![CDATA[]]></text>
                </staticText>
                <staticText>
                    <reportElement x="0" y="0" width="100" height="20"/>
                    <box>
                        <topPen lineWidth="0.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="0.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true" isItalic="true"/>
                    </textElement>
                    <text><![CDATA[]]></text>
                </staticText>
                <textField>
                    <reportElement x="0" y="0" width="100" height="20">
                        <printWhenExpression><![CDATA[$V{yearGroup_COUNT} == 1]]></printWhenExpression>
                    </reportElement>
                    <box leftPadding="10">
                        <topPen lineWidth="0.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="0.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{year}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="200" y="0" width="100" height="20"/>
                    <box leftPadding="10">
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{sum}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="100" y="0" width="100" height="20"/>
                    <box leftPadding="10">
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{month}]]></textFieldExpression>
                </textField>
                <textField evaluationTime="Group" evaluationGroup="quaterGroup" isBlankWhenNull="false">
                    <reportElement stretchType="RelativeToBandHeight" isPrintRepeatedValues="false" x="300" y="0" width="100" height="20" printWhenGroupChanges="quaterGroup">
                        <printWhenExpression><![CDATA[$V{quaterGroup_COUNT} == 1]]></printWhenExpression>
                    </reportElement>
                    <box leftPadding="10">
                        <topPen lineWidth="0.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="0.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement/>
                    <textFieldExpression><![CDATA[$V{qSum}]]></textFieldExpression>
                </textField>
                <line>
                    <reportElement x="300" y="0" width="100" height="1" printWhenGroupChanges="quaterGroup">
                        <printWhenExpression><![CDATA[$V{quaterGroup_COUNT} == 1]]></printWhenExpression>
                    </reportElement>
                </line>
            </band>
        </detail>
    </jasperReport>
    

    结果将是(pdf格式):

    在这个示例中,我在 Year 列的 Detail 波段中使用了两个元素: 一个 textField 只有垂直边框和 printWhenExpression: "$V{yearGroup_COUNT} == 1" 属性(我只为整个 yearGroup 显示一次) 和一个 staticText,没有任何文字,只有垂直边框。

    我在Quarter Sum 列的Detail 波段中使用了三个元素:
    一个 textField 只有垂直边框和 printWhenExpression: "$V{quaterGroup_COUNT} == 1" 属性(我只为整个 quaterGroup 显示一次),一个 staticText 没有任何文本,只有垂直边框和 line 元素用于绘制水平边框 @ 987654334@ 财产。

    【讨论】:

    • 感谢您的帮助...但正如我上面提到的,输出应该显示在表格组件中,而不是报表组件中。我有一个表组件包含一个数据集,用于通过 sql 检索这些记录......这个解决方案也可以在表组件中完成吗?再次感谢...
    • 我使用 printWhenExpression 方法..但由于我无法将线元素放入表格中,所以...如何在代码中显示水平线?谢谢
    • 添加line 元素没有任何问题。您应该添加它并将此元素的 height 属性设置为 1 值:
    • @user1264222 您能否发布您的 jrxml,我正在尝试做同样的事情:在表格中合并跨多行的单元格(如您的情况下的年份列)(不是(子)-报告),但我不知道如何引用该组。当我在表中使用 $V{yearGroup_COUNT} 时,编译器找不到它。
    • 嗨@Alex K..感谢您的回答..我有同样的要求,但我对 Year 列的底部边框有点困惑。您没有在任何地方指定底部边框宽度 = 1。那么它是怎么来的呢?
    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2020-09-09
    相关资源
    最近更新 更多