您可以借助Conditional Styles轻松做到这一点。
样品
测试csv数据源:
name,price,discount_price
Shirt,"100,00","90,99"
Trousers,"80,00",
"Sun glasses","199,99","120,00"
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="two_textfields_one_on_other" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="cb5d2807-1ef9-4ffb-8df8-fcb781054bb0">
<style name="priceStyle" hAlign="Left">
<conditionalStyle>
<conditionExpression><![CDATA[$F{discount_price} == null || $F{discount_price} == 0.]]></conditionExpression>
<style forecolor="#3333FF" hAlign="Center" vAlign="Top"/>
</conditionalStyle>
</style>
<queryString>
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="price" class="java.lang.Double"/>
<field name="discount_price" class="java.lang.Double"/>
<detail>
<band height="40" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement uuid="93ad241e-5257-4664-b3ab-f6448f786775" x="0" y="0" width="114" height="20"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement markup="styled"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement uuid="6b53f6c6-5d48-4879-8934-d446150e1375" style="priceStyle" positionType="Float" x="0" y="20" width="114" height="20"/>
<box>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement markup="styled"/>
<textFieldExpression><![CDATA[($F{discount_price} != null && $F{discount_price} > 0.) ? "<style isStrikeThrough='true'>"+ $F{price} + " $</style> " + $F{discount_price} + " $" : $F{price} + " $"]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
在此示例中,我更改了文本的前景色(将其设为Blue)和文本的水平对齐方式(将其设为Central,默认为Left)以防折扣价不存在(未设置或等于 0.0)。
iReport中的设计如下所示:
结果将是(通过iReport中的预览):
笔记:
为了更好地格式化,我在示例中使用了标记语法。您还可以在这篇文章中阅读有关样式的信息