在报表生成中,很经常会有这样的需求,比如状态为同意的,字体颜色为绿色,拒绝的字体颜色为红色。这时候如果想在表达式上通过edit Expression来控制是做不到的,目前探索到的有以下两种方式:
1、采用PrintWhenExpression的方式
采用该方式,通过在同个位置重叠放置多个一样的元素(除了字体一样),然后通过PrintWhenExpression表达式来控制是否显示,比如$P{text}.equals( "运行" )字体颜色为绿色,$P{text}.equals( "停止" )字体颜色为红色,来根据文本值控制元素是否打印显示,XML如下:
<textField> <reportElement x="0" y="0" width="555" height="61" uuid="01553907-42d1-4a7b-9b59-c5d34affd2bf"> <printWhenExpression><![CDATA[$P{text}.equals( "关闭" )]]></printWhenExpression> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="15" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <textFieldExpression><![CDATA[$P{text}]]></textFieldExpression> </textField> <textField> <reportElement x="0" y="0" width="555" height="61" forecolor="#33CC00" uuid="7bbe7eb5-bd51-4683-b760-3b14835046f1"> <printWhenExpression><![CDATA[$P{text}.equals( "运行" )]]></printWhenExpression> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="15" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <textFieldExpression><![CDATA[$P{text}]]></textFieldExpression> </textField>
可以看到,两个元素的x,y和width,包括其他元素基本一致;
2、采用conditionStyle来控制
采用conditionStyle,需要事先新建一个style,然后添加conditionStyle,具体的步骤为:
a)新建style,并在该style下新建conditionStyle,conditionStyle可以设置Expression来控制,如下:
b)点击新建的元素,在右边的属性,style选择刚新建的那个样式,保存。
通过以上的步骤,生成报表,JasperReport会自动根据文本值和conditionStyle来给记录设置不通的样式。
以上两种实现的运行结果如下:
最后附上Java代码和jrxml:
package com.zzq.test.myjasper; import java.io.File; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JREmptyDataSource; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.export.SimpleExporterInput; import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput; public class ConditionStyle { private static final String NAME = System.getProperty("java.class.path").split(";")[0] + File.separator + "conditionStyle"; /** * 将jrxml编译成jasper的格式,并保存在classpath目录下 * @return * @throws Exception */ public static void compile() throws Exception { JasperCompileManager.compileReportToFile(NAME + ".jrxml"); } /** * 将数据填充到编译后的jasper,重新生成一个jrprint的文件 * @throws Exception */ public static void fillData() throws Exception { Map<String, Object> params = new HashMap<String, Object>(); params.put("text", "运行"); //此处不能直接用JasperFillManager.fillReportToFile(NAME + ".jasper", params)调用,需要多传一个JREmptyDataSource,否则导出的文件有问题,具体PDF空白,excel文件有问题 JasperFillManager.fillReportToFile(NAME + ".jasper", params, new JREmptyDataSource()); } /** * 将报表导出成pdf * @throws Exception */ public static void printPDF() throws Exception { JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(new File(NAME + ".jrprint")); File destFile = new File(NAME + ".pdf"); JRPdfExporter exporter = new JRPdfExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile)); exporter.exportReport(); } public static void main(String[] args) throws Exception { compile(); fillData(); printPDF(); } }
<?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="conditionStyle" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3cca5dad-fa4c-46a2-ad12-603ef6a0190b"> <property name="ireport.zoom" value="1.0"/> <property name="ireport.x" value="0"/> <property name="ireport.y" value="0"/> <style name="colorStyle"> <conditionalStyle> <conditionExpression><![CDATA[$P{text}.equals( "关闭" )]]></conditionExpression> <style forecolor="#000000"/> </conditionalStyle> <conditionalStyle> <conditionExpression><![CDATA[$P{text}.equals( "运行" )]]></conditionExpression> <style forecolor="#33CC00"/> </conditionalStyle> <conditionalStyle> <conditionExpression><![CDATA[$P{text}.equals( "停止" )]]></conditionExpression> <style forecolor="#CC3300"/> </conditionalStyle> </style> <parameter name="text" class="java.lang.String"/> <background> <band splitType="Stretch"/> </background> <title> <band height="59" splitType="Stretch"> <staticText> <reportElement x="0" y="0" width="555" height="59" uuid="e27c138d-f95d-413e-8ed5-89a339791456"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="20" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <text><![CDATA[通过ConditionStyle来控制显示的文字颜色]]></text> </staticText> </band> </title> <pageHeader> <band height="35" splitType="Stretch"/> </pageHeader> <columnHeader> <band height="61" splitType="Stretch"> <textField> <reportElement x="0" y="0" width="555" height="61" uuid="01553907-42d1-4a7b-9b59-c5d34affd2bf"> <printWhenExpression><![CDATA[$P{text}.equals( "关闭" )]]></printWhenExpression> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="15" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <textFieldExpression><![CDATA[$P{text}]]></textFieldExpression> </textField> <textField> <reportElement x="0" y="0" width="555" height="61" forecolor="#33CC00" uuid="7bbe7eb5-bd51-4683-b760-3b14835046f1"> <printWhenExpression><![CDATA[$P{text}.equals( "运行" )]]></printWhenExpression> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="15" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <textFieldExpression><![CDATA[$P{text}]]></textFieldExpression> </textField> <textField> <reportElement x="0" y="0" width="555" height="61" forecolor="#CC0033" uuid="7bbe7eb5-bd51-4683-b760-3b14835046f1"> <printWhenExpression><![CDATA[$P{text}.equals( "停止" )]]></printWhenExpression> </reportElement> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="15" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <textFieldExpression><![CDATA[$P{text}]]></textFieldExpression> </textField> </band> </columnHeader> <detail> <band height="125" splitType="Stretch"> <textField> <reportElement style="colorStyle" x="0" y="41" width="555" height="44" uuid="b510ee7c-eba6-42fd-bce7-30638772dea4"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="15" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/> </textElement> <textFieldExpression><![CDATA[$P{text}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band height="45" splitType="Stretch"/> </columnFooter> <pageFooter> <band height="54" splitType="Stretch"/> </pageFooter> <summary> <band height="42" splitType="Stretch"/> </summary> </jasperReport>
jasperReport+iReport中conditionStyle的使用
原文:http://my.oschina.net/zzq350623/blog/521356