【问题标题】:How to prevent joining values in Row Group (column) of Crosstab?如何防止在交叉表的行组(列)中加入值?
【发布时间】:2017-03-03 09:22:13
【问题描述】:

有没有办法取消合并交叉表中具有相同值的单元格?

问题是相同值的单元格正在合并。

我希望重复该值并具有单元格边框。例如,在一个行组中,如果我有 2 列,col1 的值为“abc”,col2 的值为 1,2,3,如果有 3 行,则 col1 是一个大单元格,“abc”只打印一次。

这是我不想要的,而是希望它被打印 3 次。

这是我需要的演示文稿:

目前我有:

<?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="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a53b72b8-0dda-468b-bffd-3e28d792a2d6">
    <style name="Crosstab_CH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Crosstab_CG" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Crosstab_CT" mode="Opaque" backcolor="#005FB3">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Crosstab_CD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <queryString>
        <![CDATA[select * from test]]>
    </queryString>
    <field name="col1" class="java.lang.String"/>
    <field name="col2" class="java.lang.Integer"/>
    <field name="col3" class="java.lang.String"/>
    <field name="col4" class="java.lang.Integer"/>
    <field name="col5" class="java.lang.Integer"/>
    <summary>
        <band height="214" splitType="Stretch">
            <crosstab>
                <reportElement x="0" y="0" width="555" height="214" uuid="a8663f8c-217c-4ef3-ab2c-a11120aea80e"/>
                <rowGroup name="col11" width="60">
                    <bucket class="java.lang.String">
                        <bucketExpression><![CDATA[$F{col1}]]></bucketExpression>
                    </bucket>
                    <crosstabRowHeader>
                        <cellContents mode="Opaque" style="Crosstab_CH">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20" uuid="6e0cb2ba-ea94-4b5e-a774-3092f6b0bd06"/>
                                <textFieldExpression><![CDATA[$V{col11}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabRowHeader>
                    <crosstabTotalRowHeader>
                        <cellContents>
                            <staticText>
                                <reportElement x="0" y="0" width="-2147483648" height="-2147483648" uuid="4787314d-3cb1-4f29-99d4-1443453e5d1b"/>
                                <text><![CDATA[Total col11]]></text>
                            </staticText>
                        </cellContents>
                    </crosstabTotalRowHeader>
                </rowGroup>
                <rowGroup name="col21" width="60">
                    <bucket class="java.lang.Integer">
                        <bucketExpression><![CDATA[$F{col2}]]></bucketExpression>
                    </bucket>
                    <crosstabRowHeader>
                        <cellContents mode="Opaque" style="Crosstab_CH">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20" uuid="bf3d5e77-12a9-44cc-94d1-3f2a317f32d1"/>
                                <textFieldExpression><![CDATA[$V{col21}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabRowHeader>
                    <crosstabTotalRowHeader>
                        <cellContents>
                            <staticText>
                                <reportElement x="0" y="0" width="-2147483648" height="-2147483648" uuid="c21bf242-edbb-4533-9d7d-7120282703a5"/>
                                <text><![CDATA[Total col21]]></text>
                            </staticText>
                        </cellContents>
                    </crosstabTotalRowHeader>
                </rowGroup>
                <columnGroup name="col31" height="20">
                    <bucket class="java.lang.String">
                        <bucketExpression><![CDATA[$F{col3}]]></bucketExpression>
                    </bucket>
                    <crosstabColumnHeader>
                        <cellContents mode="Opaque" style="Crosstab_CH">
                            <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                            <textField>
                                <reportElement x="0" y="0" width="120" height="20" uuid="320dded3-882b-4e8d-a821-ce94996a72d2">
                                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                                </reportElement>
                                <textFieldExpression><![CDATA[$V{col31}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabColumnHeader>
                    <crosstabTotalColumnHeader>
                        <cellContents>
                            <staticText>
                                <reportElement x="0" y="0" width="-2147483648" height="-2147483648" uuid="e78a53ad-4437-41d1-8cea-ab1a2fdf09d3"/>
                                <text><![CDATA[Total col31]]></text>
                            </staticText>
                        </cellContents>
                    </crosstabTotalColumnHeader>
                </columnGroup>
                <measure name="col4_MEASURE1" class="java.lang.Integer" calculation="Count">
                    <measureExpression><![CDATA[$F{col4}]]></measureExpression>
                </measure>
                <measure name="col5_MEASURE1" class="java.lang.Integer" calculation="Count">
                    <measureExpression><![CDATA[$F{col5}]]></measureExpression>
                </measure>
                <crosstabCell width="120" height="20">
                    <cellContents mode="Opaque" style="Crosstab_CD">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="20" uuid="ddc2801e-7492-4848-b7f7-422d7267cadb"/>
                            <box>
                                <topPen lineWidth="0.5"/>
                                <leftPen lineWidth="0.5"/>
                                <bottomPen lineWidth="0.5"/>
                                <rightPen lineWidth="0.5"/>
                            </box>
                            <textFieldExpression><![CDATA[$V{col4_MEASURE1}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="60" y="0" width="60" height="20" uuid="38136ac4-94aa-46ef-91ca-b32ac1d19aba">
                                <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                            </reportElement>
                            <box>
                                <topPen lineWidth="0.5"/>
                                <leftPen lineWidth="0.5"/>
                                <bottomPen lineWidth="0.5"/>
                                <rightPen lineWidth="0.5"/>
                            </box>
                            <textFieldExpression><![CDATA[$V{col5_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" columnTotalGroup="col31">
                    <cellContents mode="Opaque" style="Crosstab_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="10" forecolor="#FFFFFF" uuid="0b620468-123a-434e-8a87-be8125b86470"/>
                            <textFieldExpression><![CDATA[$V{col4_MEASURE1}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="0" y="10" width="60" height="10" forecolor="#FFFFFF" uuid="d3c3da1b-7c66-48e5-8328-9c775e036229"/>
                            <textFieldExpression><![CDATA[$V{col5_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" rowTotalGroup="col11">
                    <cellContents mode="Opaque" style="Crosstab_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="10" forecolor="#FFFFFF" uuid="08db97ef-0f91-4301-a552-2eec171c2b91"/>
                            <textFieldExpression><![CDATA[$V{col4_MEASURE1}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="0" y="10" width="60" height="10" forecolor="#FFFFFF" uuid="2fa36822-c633-4954-b6ad-bb2633dd827b"/>
                            <textFieldExpression><![CDATA[$V{col5_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" rowTotalGroup="col11" columnTotalGroup="col31">
                    <cellContents mode="Opaque" style="Crosstab_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="10" forecolor="#FFFFFF" uuid="a1545b3f-ac13-4e97-8720-3b526af18207"/>
                            <textFieldExpression><![CDATA[$V{col4_MEASURE1}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="0" y="10" width="60" height="10" forecolor="#FFFFFF" uuid="02c1d1d7-8b20-461e-8bd9-b2e4ceff02ed"/>
                            <textFieldExpression><![CDATA[$V{col5_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" rowTotalGroup="col21">
                    <cellContents mode="Opaque" style="Crosstab_CG">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="10" uuid="94b61c57-3b18-4134-a48f-36394cf8ac28"/>
                            <textFieldExpression><![CDATA[$V{col4_MEASURE1}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="0" y="10" width="60" height="10" uuid="b8b84ab6-a800-427e-8738-40dc17f8b7ae"/>
                            <textFieldExpression><![CDATA[$V{col5_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" rowTotalGroup="col21" columnTotalGroup="col31">
                    <cellContents mode="Opaque" style="Crosstab_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="10" forecolor="#FFFFFF" uuid="e34172fb-29af-43aa-af6b-50bb63949236"/>
                            <textFieldExpression><![CDATA[$V{col4_MEASURE1}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="0" y="10" width="60" height="10" forecolor="#FFFFFF" uuid="232ba479-f365-4fb8-8402-4a9d50f79c6c"/>
                            <textFieldExpression><![CDATA[$V{col5_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
            </crosstab>
        </band>
    </summary>
</jasperReport>

【问题讨论】:

  • 嗨,Alex,我当然必须使用交叉表来满足我的要求。问题是相同值的单元格正在合并。我希望重复该值并具有单元格边框。例如,在一个行组中,如果我有 2 列,col1 的值为“abc”,col2 的值为 1,2,3,如果有 3 行,则 col1 是一个大单元格,“abc”只打印一次。这是我不想要的,而是希望它被打印 3 次。我无法粘贴任何图片,因为这个编辑器没有给我任何这样做的条件
  • 嗨,我已经在下面提到的链接中上传了图片。说不合适的交叉表是 Jasper 给我的,我想要的是名为 'required crosstab' 的图片。 tinypic.com/m/jjy5qc/1 , tinypic.com/m/jjy5qd/1
  • 此外,许多 jasper 教程都说,有一个复选框,如果我选中它,合并单元格会出现,否则会显示为 unmerged 。我在设计编辑器中找不到它,请告诉我在 jasper 的设计布局中哪里可以找到它?
  • Alex,我已将 jrxml 放入问题中。请看一下

标签: jasper-reports crosstab


【解决方案1】:

您的样本中的 col1col2行组。这个名称 (Row Group) 告诉我们 JasperReports 引擎将所有 非唯一 值合并到组中,以防使用 Crosstab零件。这就是为什么应该始终对值进行排序以正确分组数据(如下图所示)。

Row Groups中的数据会被JR引擎分组,一个组被绘制成一个单元格

Does this mean that the problem has no solution? - 不,让我们解决它!

看起来您想“加入”col1col2 列值。这意味着 col1 Row Group 的值可以添加到 col2 Row Group 的“列”中。

示例

数据源

用简单的csv数据源测试样本就足够了。

col1,col2,col3,col4,col5
abcd,1,b,1c,434
abcd,2,c,2c,333
abcd,3,c,4c,333
abcd,3,c,4c,333
abcd,5,c,4c,333
abcd,3,e,3g,909

以下示例中此数据源的数据适配器名称为 values.csv。文件的第一行被跳过 - 它包含列的名称。

报告模板

我们应该将第一列 (col1 Row Group) 的宽度设置为零,并增加第二列 (col2 Row Group) 的宽度以放置额外的值 col1。在这种情况下,JR 引擎 为每个唯一的 col2 值绘制两个值。我们仍然需要第一列,因为我们需要 col1 的值(col1 Row Group 的存储桶)。

Jaspersoft Studio (JSS) 中的设计如下:

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="Crosstab. Merged values" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="values.csv"/>
    <style name="Crosstab_CH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Crosstab_CD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <field name="col1" class="java.lang.String"/>
    <field name="col2" class="java.lang.Integer"/>
    <field name="col3" class="java.lang.String"/>
    <field name="col4" class="java.lang.Integer"/>
    <field name="col5" class="java.lang.Integer"/>
    <summary>
        <band height="214" splitType="Stretch">
            <crosstab>
                <reportElement x="0" y="0" width="555" height="214"/>
                <crosstabHeaderCell>
                    <cellContents style="Crosstab_CH">
                        <staticText>
                            <reportElement x="0" y="0" width="60" height="20"/>
                            <box>
                                <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                            </box>
                            <text><![CDATA[Col1]]></text>
                        </staticText>
                        <staticText>
                            <reportElement x="60" y="0" width="60" height="20"/>
                            <box>
                                <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                            </box>
                            <text><![CDATA[Col2]]></text>
                        </staticText>
                    </cellContents>
                </crosstabHeaderCell>
                <rowGroup name="col11" width="0">
                    <bucket class="java.lang.String">
                        <bucketExpression><![CDATA[$F{col1}]]></bucketExpression>
                    </bucket>
                    <crosstabRowHeader>
                        <cellContents mode="Opaque" style="Crosstab_CH"/>
                    </crosstabRowHeader>
                    <crosstabTotalRowHeader>
                        <cellContents/>
                    </crosstabTotalRowHeader>
                </rowGroup>
                <rowGroup name="col21" width="120">
                    <bucket class="java.lang.Integer">
                        <bucketExpression><![CDATA[$F{col2}]]></bucketExpression>
                    </bucket>
                    <crosstabRowHeader>
                        <cellContents mode="Opaque" style="Crosstab_CD">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20"/>
                                <box>
                                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                </box>
                                <textFieldExpression><![CDATA[$V{col11}]]></textFieldExpression>
                            </textField>
                            <textField>
                                <reportElement x="60" y="0" width="60" height="20"/>
                                <box>
                                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                </box>
                                <textFieldExpression><![CDATA[$V{col21}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabRowHeader>
                    <crosstabTotalRowHeader>
                        <cellContents/>
                    </crosstabTotalRowHeader>
                </rowGroup>
                <columnGroup name="col31" height="20">
                    <bucket class="java.lang.String">
                        <bucketExpression><![CDATA[$F{col3}]]></bucketExpression>
                    </bucket>
                    <crosstabColumnHeader>
                        <cellContents mode="Opaque" style="Crosstab_CH">
                            <textField>
                                <reportElement x="0" y="0" width="120" height="20"/>
                                <textFieldExpression><![CDATA[$V{col31}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabColumnHeader>
                    <crosstabTotalColumnHeader>
                        <cellContents/>
                    </crosstabTotalColumnHeader>
                </columnGroup>
                <measure name="col4_MEASURE1" class="java.lang.Integer" calculation="Count">
                    <measureExpression><![CDATA[$F{col4}]]></measureExpression>
                </measure>
                <measure name="col5_MEASURE1" class="java.lang.Integer" calculation="Count">
                    <measureExpression><![CDATA[$F{col5}]]></measureExpression>
                </measure>
                <crosstabCell width="120" height="20">
                    <cellContents mode="Opaque" style="Crosstab_CD">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="20"/>
                            <box>
                                <topPen lineWidth="0.5"/>
                                <leftPen lineWidth="0.5"/>
                                <bottomPen lineWidth="0.5"/>
                                <rightPen lineWidth="0.5"/>
                            </box>
                            <textFieldExpression><![CDATA[$V{col4_MEASURE1}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="60" y="0" width="60" height="20"/>
                            <box>
                                <topPen lineWidth="0.5"/>
                                <leftPen lineWidth="0.5"/>
                                <bottomPen lineWidth="0.5"/>
                                <rightPen lineWidth="0.5"/>
                            </box>
                            <textFieldExpression><![CDATA[$V{col5_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
            </crosstab>
        </band>
    </summary>
</jasperReport>

输出结果

JSS中的结果:

【讨论】:

  • 非常感谢!!该解决方案就像一个魅力!但是,我现在有一个问题,当我将 col1 RowGroup 详细信息移动到 col2 RowGroup 时,以前如果我在 col1 上有一个表达式,比如 dateutils.addminutes($F{col1},24),我无法做同样的事情我将粘贴 col1 复制到 col2 中。我该如何解决这个问题? (col1 为了解释我假设它是一个日期字段)。我无法在移动的列上使用 $F 字段
  • 不客气 :) 但你可以使用$V{col11} - $F{col1} 的存储桶(&lt;bucketExpression&gt;&lt;![CDATA[$F{col2}]]&gt;&lt;/bucketExpression&gt; - 来自&lt;rowGroup name="col11" width="0"&gt;
  • 是的,答案非常有帮助!再次感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 2015-10-02
相关资源
最近更新 更多