通用条件样式

时间:2013-08-29 17:09:40

标签: jasper-reports ireport

我需要创建一个可应用于报表上所有“单元格”/元素/对象的样式。 有没有办法做到这一点,除了为每个字段/变量创建条件?

对于使用下面代码的简单示例,我想创建一个条件样式,对于Col1..Col5来说,测试0并使背景变为蓝色,然后将其保留为白色。对于这个例子,我知道创建5个条件并不是最重要的事情,但我正在为我的报告处理60个条件。

<?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="report4a" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fe23f2e2-ee8e-403b-b870-7785fe8941a6">
    <queryString>
        <![CDATA[select 1 as col1, 0 as col2, 2 as col3, 0 as col4,  10 as col5 from dual 
        union
        select 4 as col1, 0 as col2, 7 as col3, 1 as col4,  12 as col5 from dual 
        union 
        select 5 as col1, 0 as col2, 8 as col3, 0 as col4,  16 as col5 from dual 
        union
        select 0 as col1, 0 as col2, 0 as col3, 1 as col4,  110 as col5 from dual
        union
        select 7 as col1, 0 as col2, 7 as col3, 0 as col4,  180 as col5 from dual 
        union 
        select 0 as col1, 0 as col2, 0 as col3, 1 as col4,  107 as col5 from dual ]]>
    </queryString>
    <field name="COL1" class="java.math.BigDecimal"/>
    <field name="COL2" class="java.math.BigDecimal"/>
    <field name="COL3" class="java.math.BigDecimal"/>
    <field name="COL4" class="java.math.BigDecimal"/>
    <field name="COL5" class="java.math.BigDecimal"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="077997f5-c89c-4fdf-b89e-aa43b7cf77e3" x="0" y="0" width="111" height="20"/>
                <textElement/>
                <text><![CDATA[COL1]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="1421164b-ba30-4104-bf62-1e75dd04f757" x="111" y="0" width="111" height="20"/>
                <textElement/>
                <text><![CDATA[COL2]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="03802088-f5b9-4412-aae7-64ec6c54c4c3" x="222" y="0" width="111" height="20"/>
                <textElement/>
                <text><![CDATA[COL3]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="b44d853c-0ea5-49ea-8420-e4c40d65c3c3" x="333" y="0" width="111" height="20"/>
                <textElement/>
                <text><![CDATA[COL4]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="25534117-80fa-43ee-b90c-f150d4b8e0e0" x="444" y="0" width="111" height="20"/>
                <textElement/>
                <text><![CDATA[COL5]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="8cffe1bc-e06b-48ec-96fc-a3464687a883" x="0" y="0" width="111" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{COL1}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="03ec79ef-9614-4929-b464-450b9b9e8a80" x="111" y="0" width="111" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{COL2}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="79a5d64c-18b4-4a58-89c7-d72de8072759" x="222" y="0" width="111" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{COL3}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="6a5606ff-0f1a-4dec-a494-90c493c794a0" x="333" y="0" width="111" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{COL4}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="c2c5f7b1-4d70-49a8-8745-f9337d07457b" x="444" y="0" width="111" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{COL5}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

1 个答案:

答案 0 :(得分:0)

是的,你可以这样做。

第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="styles_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fafb8cb9-d85f-40ad-a31b-0aabd9f6ba93">
    <queryString>
        <![CDATA[SELECT id, name, cost FROM product]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="NAME" class="java.lang.String"/>
    <field name="COST" class="java.math.BigDecimal"/>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement uuid="4c746058-73f8-44b1-b8a4-b503291d4c23" x="54" y="22" width="446" height="35"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Styles sample.
Without using the default style]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement uuid="f91514a2-601f-4045-a294-68be1929c712" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="1efff9bb-0a2a-438d-9e7b-3d29f1d4c537" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="c061bcd5-9a9d-4ad6-89f4-a99103bdb742" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Cost]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="848d3bcf-4c06-4a8a-b880-e5cdae5749e2" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="5557f5a7-4db6-4b58-a99f-b0ccf88c69c5" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="b77c6785-28d4-4395-b99c-2dbc336d00b3" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

结果将是(通过 iReport 中的预览):

enter image description here

第2步

现在我们决定添加默认样式( defStyle )。例如,我们将所有元素的 forecolor 更改为 blue 颜色。

我们在这种情况下的模板是:

<?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="styles_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fafb8cb9-d85f-40ad-a31b-0aabd9f6ba93">
    <style name="defStyle" isDefault="true" forecolor="#3333FF"/>
    <queryString>
        <![CDATA[SELECT id, name, cost FROM product]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="NAME" class="java.lang.String"/>
    <field name="COST" class="java.math.BigDecimal"/>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement uuid="4c746058-73f8-44b1-b8a4-b503291d4c23" x="54" y="22" width="446" height="35"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Styles sample.
Using the default style]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement uuid="f91514a2-601f-4045-a294-68be1929c712" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="1efff9bb-0a2a-438d-9e7b-3d29f1d4c537" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="c061bcd5-9a9d-4ad6-89f4-a99103bdb742" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Cost]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="848d3bcf-4c06-4a8a-b880-e5cdae5749e2" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="5557f5a7-4db6-4b58-a99f-b0ccf88c69c5" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="b77c6785-28d4-4395-b99c-2dbc336d00b3" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

结果将是(通过 iReport 预览):

enter image description here

颜色改变了。请注意,我们没有将此样式应用于任何元素(在整个报告的模板中)。

第3步

现在我们要添加另一个条件样式( pink )。如果成本大于25,我们希望用 cost 值更改 textField forecolor - 我们将其设为粉红色

我们在这种情况下的模板是:

<?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="styles_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fafb8cb9-d85f-40ad-a31b-0aabd9f6ba93">
    <style name="defStyle" isDefault="true" forecolor="#3333FF"/>
    <style name="pink" style="defStyle">
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{COST} > 25]]></conditionExpression>
            <style forecolor="#CC00CC"/>
        </conditionalStyle>
    </style>
    <queryString>
        <![CDATA[SELECT id, name, cost FROM product ORDER BY cost DESC]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="NAME" class="java.lang.String"/>
    <field name="COST" class="java.math.BigDecimal"/>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement uuid="4c746058-73f8-44b1-b8a4-b503291d4c23" x="8" y="22" width="539" height="35"/>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="html"/>
                <text><![CDATA[Styles sample.
Using the default style and the another conditional style which applied to the <i>textField</i> with <b>cost</b> field]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement uuid="f91514a2-601f-4045-a294-68be1929c712" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="1efff9bb-0a2a-438d-9e7b-3d29f1d4c537" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="c061bcd5-9a9d-4ad6-89f4-a99103bdb742" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Cost]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="848d3bcf-4c06-4a8a-b880-e5cdae5749e2" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="5557f5a7-4db6-4b58-a99f-b0ccf88c69c5" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="b77c6785-28d4-4395-b99c-2dbc336d00b3" style="pink" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

我们添加了新样式 pink ,它覆盖了 defStyle ,我们将其应用于第三列 cost <中的 textFiled / em>的。我们没有将任何其他样式应用于任何其他元素。

结果将是(通过 iReport 中的预览):

enter image description here


注意:

  1. 我们可以借助net.sf.jasperreports.style.{style_property_suffix}参数覆盖所有样式的行为。

  2. 有关Conditional Styles

  3. 的详细信息
  4. 不要忘记net.sf.jasperreports.style.evaluation.time.enabled属性。