JasperReports:如何根据参数构造标题头

时间:2012-04-06 08:03:49

标签: jasper-reports

如何根据参数构建标题标题?

例如,我按日期,周,月和年有一份报告。我有一个参数,但我不知道如何在标题中显示它。

我想要的是:

If I put parameter $P{date1} and $P{date2} then it will show 'Date range from $P{date1} and $P{date2}' for header 1

else if parameter $P{w}$P{month}$P{year} then 'For weekly $P{w}$P{month}$P{year}' for header 2

else if parameter $P{month}$P{year} then 'For monthly $P{month}$P{year}' for header 3

else if parameter $P{q}$P{year} then 'For quarter $P{q}$P{year}' for header 4

else if parameter $P{year} then 'For yearly $P{year}' for header 5

更新: - 我运行报告时遇到新问题,输出无法正确显示。 这是我的代码: -

<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="Company Profile by Date Registered" pageWidth="595" pageHeight="842" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="daterangefrom" class="java.lang.String">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <parameter name="daterangeto" class="java.lang.String">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <parameter name="monthy" class="java.lang.String">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <parameter name="yrs" class="java.lang.String">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <parameter name="quarter" class="java.lang.String">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <parameter name="weeks" class="java.lang.String">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[select a.entered,
a.modified,
a.org_id,
a.name,
a.organization_type,
c.description as companygroup,lead,
a.industry_temp_code,
b.description,
a.equity_structure,a.operation_date from organization a
left join lookup_industry_group b on a.industry_temp_code = b.code
left join lookup_company_group c on c.code = a.company_group
left join ticket d on d.org_id = a.org_id
where
((d.entered BETWEEN $P{daterangefrom} AND $P{daterangeto}) or ($P{daterangefrom} =' ' AND $P{daterangeto} =' '))
AND
((datename(month,d.entered) = $P{monthy}) or ($P{monthy} = ' '))
AND
((datename(year,d.entered) = $P{yrs}) or ($P{yrs} = ' '))
AND
((datepart(qq, d.entered) = $P{quarter}) or ($P{quarter} = ' '))
AND
((DATEPART(day, d.entered - 1) / 7 + 1 = $P{weeks}) or  ($P{weeks} = ' '))]]>
    </queryString>
    <field name="entered" class="java.sql.Timestamp"/>
    <field name="modified" class="java.sql.Timestamp"/>
    <field name="org_id" class="java.lang.Integer"/>
    <field name="name" class="java.lang.String"/>
    <field name="organization_type" class="java.lang.String"/>
    <field name="companygroup" class="java.lang.String"/>
    <field name="lead" class="java.lang.String"/>
    <field name="industry_temp_code" class="java.lang.Integer"/>
    <field name="description" class="java.lang.String"/>
    <field name="equity_structure" class="java.lang.String"/>
    <field name="operation_date" class="java.sql.Timestamp"/>
    <title>
        <band height="79" splitType="Stretch">
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement x="18" y="14" width="537" height="41" isRemoveLineWhenBlank="true"/>
                <textElement/>
                <textFieldExpression><![CDATA[(($P{daterangefrom} != null) && ($P{daterangeto} != null)) ?  "Date range from " + $P{daterangefrom} + " and " + $P{daterangeto}
: (($P{weeks} != null) && ($P{monthy} != null) && ($P{yrs} != null)) ? "For weekly " +$P{weeks} + $P{monthy} + $P{yrs}
: (($P{monthy} != null) && ($P{yrs} != null)) ? "For monthly " + $P{monthy} + $P{yrs}
: (($P{quarter} != null) &&  ($P{yrs} != null)) ? "For quarter " + $P{quarter} + $P{yrs}
: (($P{yrs} != null)) ? "For yearly " + $P{yrs} : null]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

1 个答案:

答案 0 :(得分:0)

您可以将 textField 元素添加到Title频段并设置 textFieldExpression 属性。

您应该在表达式中使用条件运算符 ? :

示例(jrxml文件的片段):

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <parameter name="date1" class="java.lang.String">
        <defaultValueExpression><![CDATA[null]]></defaultValueExpression>
    </parameter>
    <parameter name="date2" class="java.lang.String">
        <defaultValueExpression><![CDATA[null]]></defaultValueExpression>
    </parameter>
    <parameter name="w" class="java.lang.String">
        <defaultValueExpression><![CDATA[null]]></defaultValueExpression>
    </parameter>
    <parameter name="q" class="java.lang.String">
        <defaultValueExpression><![CDATA[null]]></defaultValueExpression>
    </parameter>
    <parameter name="month" class="java.lang.String">
        <defaultValueExpression><![CDATA[null]]></defaultValueExpression>
    </parameter>
    <parameter name="year" class="java.lang.String">
        <defaultValueExpression><![CDATA[null]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="18" y="14" width="537" height="41"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{date1} != null && $P{date2} != null ?  "Date range from " + $P{date1} + " and " + $P{date2}
: $P{w} != null && $P{month} != null && $P{year} ? "For weekly " + $P{w} + $P{month} + $P{year}
: $P{month} != null && $P{year} != null ? "For monthly " + $P{month} + $P{year}
: $P{q} != null &&  $P{year} != null ? "For quarter " + $P{q} + $P{year}
: $P{year} != null ? "For yearly " + $P{year} : null]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>