Search code examples
javajasper-reports

Jasper pass formatted text as field Jasper change format


I have a simple report like this:

enter image description here

I pass the info formatted like this:

{ image deleted }

But when I see the report I see something completely shuffle or unformatted like this:

enter image description here

I want the text formatted like I pass from Java. What I am doing wrong?

Update

The periodoDisfrutado is a field from type java.lang.String and I pass from my Java a java.lang.String:

enter image description here

My Java class the method which is send the data to the report:

enter image description here

My XML file is

<group name="empleado">
    <groupExpression><![CDATA[$F{empleado}]]></groupExpression>
    <groupHeader>
        <band height="117">
            <textField>
                <reportElement x="7" y="8" width="547" height="20" forecolor="#FF0000"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA["Empleado: " + $F{empleado}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="7" y="33" width="148" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA["Dias de Vacaciones/Año: " + $F{diasTotal}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="161" y="33" width="119" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA["Dias disfrutados: " + $F{gastados}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="281" y="33" width="105" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA["Dias restantes: " + $F{restantes}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="false">
                <reportElement x="176" y="59" width="378" height="56"/>
                <textElement textAlignment="Left"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{periodoDisfrutado}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="7" y="59" width="169" height="20"/>
                <textElement/>
                <text><![CDATA[Periodos de vacaciones disfrutados:]]></text>
            </staticText>
        </band>
    </groupHeader>
</group>

Solution

  • It is as I suspected you are passing a String with page break, spaces and maybe tabs. Jasper report will not try to format as table, it will simply display your string and depending on font type, etc you will have different result.

    If you like to create a table format instead of a String you need to pass a JRDataSource, and use for example a subreport to display the data.

    Note: This will also greatly improve your export to excel.

    The best way to setup this datasource depends on what kind of current datasource you are using, where are the $F{periodoDisfrutado} coming from?