如何在 JasperReports 中使用 printWhenExpression

2022-09-03 05:18:23

有人能告诉我如何使用 JasperReports 的 printWhenExpression 吗?


答案 1

是否有相对于布尔值的错误?

因为您需要使用布尔值而不是基元类型。

所以:

$F{mesure} != "PH"
($F{userfd4}).equals("1") ? true : false 

会给.cannot cast from boolean to Boolean

( $F{mesure}.startsWith("PH") ? Boolean.TRUE:Boolean.FALSE ) 
($F{userfd4}).equals("1") ? Boolean.TRUE : Boolean.FALSE  

会是正确的。

请参阅此示例


2015年11月更新(7年后)

Petter Friberg在评论中指出:

jasper report 6.0 中,这不是必需的:
您可以返回任何一个表达式,或者像这样简单的表达式就可以了。booleanBoolean$F{fieldName}.equals("hello")


您可以在 demo/samples/tableofcontents/reports/TocPart.jrxml 中看到该命令的演示。

<reportElement style="Sans_Bold" positionType="Float" x="50" y="0" width="100" height="15" isRemoveLineWhenBlank="true" uuid="db8b68c6-4430-4199-8967-3ab5c077cb56">
    <property name="local_mesure_unitx" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.x" value="px"/>
    <printWhenExpression><![CDATA[$F{level} == 1]]></printWhenExpression>
</reportElement>

答案 2

另一张海报在解释如何使用它的技术细节方面做得很好,所以我将尝试解释人们可能发现它有用的情况。

基本上,它允许您根据布尔表达式显示或隐藏单元格的内容。例如,您可能希望仅在某人年满 18 岁时显示该人的姓名,然后在姓名字段中使用 printwhenexpression,如下所示:

$F{age} >= 18

推荐