你不能用條件樣式來實現這一點。
的解決方案是創建一個能夠輸出樣式的文本自定義格式,並與markup="styled"
配置,像這樣的文本字段:
<textField>
<reportElement x="72" y="16" width="100" height="24" uuid="698866c8-7d26-4bc7-8727-b4a56d239a53"/>
<textElement markup="styled"/>
<textFieldExpression><![CDATA[NegativeNumberFormatter.format($F{A1}, "#,##0.###")]]></textFieldExpression>
</textField>
NegativeNumberFormatter可能是這樣的(使用缺省包):
import java.text.DecimalFormat;
public class NegativeNumberFormatter {
public static String format(Number n, String pattern) {
if (n != null) {
if (n.doubleValue() < 0) {
DecimalFormat df = new DecimalFormat();
if (pattern != null) {
df.applyPattern(pattern);
}
return "<font color=\"red\">" + df.format(n) + "</font>";
} else {
return "" + n;
}
}
return null;
}
}
[在JasperReports中基於條件改變文本字段數據顏色(前景色)]的可能重複(http://stackoverflow.com/questions/8754448/change-text-field-data-color-foreground-color-based- on-condition-in-jasperrepo) –
是的,但是我瞭解有條件的樣式,而不是使用特定的字段,在那個例子中,我希望更具動態性,因爲我可以將這個條件拖到許多不同的文本字段上,並且它會檢查它是否與它小於0.這個問題比其他問題稍微複雜一些。 – Matt