当所有数据为零时,如何隐藏交叉表列?

时间:2016-02-23 15:24:53

标签: jasper-reports crosstab

当所有值都为0时,如何在交叉表中隐藏由measure(详细信息部分)生成的列。当前正在获取一个值为零的列。谁能指出解决方案?

jrxml代码

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.1.final using JasperReports Library version 6.0.0  -->
<!-- 2016-02-23T23:05:34 -->
<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" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="48809477-2290-4dda-a6f9-5d348ff0b70b">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
    <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>
    <style name="Crosstab 1_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 1_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 1_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 1_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>
    <style name="Style1" isBlankWhenNull="true"/>
    <style name="Crosstab 2_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 2_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 2_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 2_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 language="SQL">
        <![CDATA[select 
id,
name,
cost,
0 as new
from product]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="NAME" class="java.lang.String"/>
    <field name="COST" class="java.math.BigDecimal"/>
    <field name="NEW" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <summary>
        <band height="352" splitType="Stretch">
            <crosstab>
                <reportElement x="10" y="40" width="299" height="120" uuid="05be91e1-9bd6-407e-8feb-ad21aedcb054"/>
                <rowGroup name="ID1" width="60">
                    <bucket class="java.lang.Integer">
                        <bucketExpression><![CDATA[$F{ID}]]></bucketExpression>
                    </bucket>
                    <crosstabRowHeader>
                        <cellContents mode="Opaque" style="Crosstab 2_CH">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20" uuid="ed1b3cbe-2a65-4ee8-9882-50b1817b941c"/>
                                <textFieldExpression><![CDATA[$V{ID1}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabRowHeader>
                    <crosstabTotalRowHeader>
                        <cellContents>
                            <staticText>
                                <reportElement x="0" y="0" width="-2147483648" height="-2147483648" uuid="9c778658-e79c-4919-8e40-196b735ce152"/>
                                <text><![CDATA[Total ID1]]></text>
                            </staticText>
                        </cellContents>
                    </crosstabTotalRowHeader>
                </rowGroup>
                <columnGroup name="NAME1" height="20">
                    <bucket class="java.lang.String">
                        <bucketExpression><![CDATA[$F{NAME}]]></bucketExpression>
                    </bucket>
                    <crosstabColumnHeader>
                        <cellContents mode="Opaque" style="Crosstab 2_CH">
                            <textField isBlankWhenNull="true">
                                <reportElement x="0" y="0" width="60" height="20" isRemoveLineWhenBlank="true" uuid="a705ac4c-f769-4b2b-84ae-62965d542558"/>
                                <textFieldExpression><![CDATA[$V{NAME1}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabColumnHeader>
                    <crosstabTotalColumnHeader>
                        <cellContents>
                            <staticText>
                                <reportElement x="0" y="0" width="-2147483648" height="-2147483648" uuid="70643653-511b-4b84-8d7c-e0cc09aac39e"/>
                                <text><![CDATA[Total NAME1]]></text>
                            </staticText>
                        </cellContents>
                    </crosstabTotalColumnHeader>
                </columnGroup>
                <measure name="NEW_MEASURE1" class="java.lang.Integer" calculation="Sum">
                    <measureExpression><![CDATA[$F{NEW}]]></measureExpression>
                </measure>
                <crosstabCell width="60" height="20">
                    <cellContents mode="Opaque" style="Crosstab 2_CD">
                        <textField isBlankWhenNull="true">
                            <reportElement x="0" y="0" width="60" height="20" isRemoveLineWhenBlank="true" uuid="b0423e55-9ba8-4bc8-b032-5773c2e71ce1">
                                <printWhenExpression><![CDATA[$V{NEW_MEASURE1}==null?true:false]]></printWhenExpression>
                            </reportElement>
                            <textFieldExpression><![CDATA[($V{NEW_MEASURE1}.intValue()==0)?null:$V{NEW_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" columnTotalGroup="NAME1">
                    <cellContents mode="Opaque" style="Crosstab 2_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="df784b91-a1cc-4cd6-b7f8-56a271e0947a"/>
                            <textFieldExpression><![CDATA[$V{NEW_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" rowTotalGroup="ID1">
                    <cellContents mode="Opaque" style="Crosstab 2_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="70e0669b-1684-4cc8-8671-0c29ac37921c"/>
                            <textFieldExpression><![CDATA[$V{NEW_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
                <crosstabCell width="60" height="20" rowTotalGroup="ID1" columnTotalGroup="NAME1">
                    <cellContents mode="Opaque" style="Crosstab 2_CT">
                        <textField>
                            <reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="1da6c415-c92a-4624-aae2-59802fe85538"/>
                            <textFieldExpression><![CDATA[$V{NEW_MEASURE1}]]></textFieldExpression>
                        </textField>
                    </cellContents>
                </crosstabCell>
            </crosstab>
        </band>
    </summary>
</jasperReport>

编辑:无法在查询中进行过滤,因为它会减少行数。即使列值为0,我也喜欢显示行。

1 个答案:

答案 0 :(得分:0)

在填写报告后,您无法删除jrxml中的整个列,因此您需要的是首先必须填充报告(需要循环所有记录以查看是否全部为0)。

您的解决方案是:

  1. 使用正确的过滤器(where)查找正确的查询sql,它允许您保留记录但不包括生成列的值。

  2. 使用预查询(在示例java中)了解最终查询应如何通过参数将此查询传递给jasper报告。

  3. 使用,动态生成报告,首先循环数据源以查找需要包含哪些列。

  4. Post在导出,删除和移动物体之前详细说明了Jasper Print。