迭代子报告Jasper报告中的设置字段

时间:2017-03-27 18:23:32

标签: java jasper-reports

我使用JasperReports制作了一个报告,它已经可以使用,但我想从Set类型对象的值构造一个String。我正在使用以下表达式来填充我的文本字段:

$F{resultat}.getTypeResultats().iterator().next().getDescription()+ ". " +
(($F{resultat}.getTypeResultats().iterator().hasNext()) ? 
$F{resultat}.getTypeResultats().iterator().next().getDescription() : "")

我从Field获得了预期值,但它始终获得Set中的第一个元素。

如何迭代我的设置?

感谢您的帮助。

这是子报告:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 3.7.5  -->
<!-- 2017-03-27T14:34:04 -->
<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="FicheEpisode_Declarations" pageWidth="552" pageHeight="792" columnWidth="552" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.scriptlethandling" value="0"/>
    <property name="ireport.encoding" value="UTF-8"/>
    <property name="net.sf.jasperreports.awt.ignore.missing.font" value="true"/>
    <property name="ireport.zoom" value="1.771561000000001"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <import value="org.apache.commons.lang.StringUtils"/>
    <import value="net.sf.jasperreports.engine.*"/>
    <import value="java.util.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>
    <subDataset name="Dataset1">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
    </subDataset>
    <field name="dateDeclaration" class="java.util.Date"/>
    <field name="dateReception" class="java.util.Date"/>
    <field name="declarantAutre" class="java.lang.String"/>
    <field name="casMaladie" class="com.openpole.mado.bo.CasMaladie"/>
    <field name="modeDeclaration" class="com.openpole.mado.bo.ModeDeclaration"/>
    <field name="medecinDeclarant" class="com.openpole.mado.bo.Medecin"/>
    <field name="laboratoireDeclarant" class="com.openpole.mado.bo.Laboratoire"/>
    <field name="resultat" class="com.openpole.mado.bo.Resultat"/>
    <background>
        <band height="11" splitType="Stretch"/>
    </background>
    <title>
        <band height="1" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="86" splitType="Stretch">
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="3" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.decl.typeDeclaration} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="18" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.decl.dateDeclaration} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="46" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.decl.methodeDeclaration} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="32" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.decl.dateReception} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="yyyy-MM-dd" isBlankWhenNull="true">
                <reportElement key="fldDateDeclaration" mode="Transparent" x="154" y="18" width="92" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.util.Date"><![CDATA[$F{dateDeclaration}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="yyyy-MM-dd" isBlankWhenNull="true">
                <reportElement key="fldDateReception" mode="Transparent" x="154" y="32" width="92" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.util.Date"><![CDATA[$F{dateReception}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="fldModeDeclaration" mode="Transparent" x="154" y="46" width="275" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{modeDeclaration}.getDescription()]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="fldNomDeclarant" mode="Transparent" x="154" y="3" width="273" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{medecinDeclarant} != null ?
  $R{rapport.ficheEpisode.decl.medecin} + " (" + $F{medecinDeclarant}.getPrenom() + " " + $F{medecinDeclarant}.getNom() + ")":
$F{laboratoireDeclarant} != null ?
  $R{rapport.ficheEpisode.decl.laboratoire} + " (" + $F{laboratoireDeclarant}.getDescription() + ")":
$R{rapport.ficheEpisode.decl.autre} + " (" + StringUtils.trimToEmpty($F{declarantAutre}) + ")"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="60" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.res.sourceInfection} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="75" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.res.typeResultat} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="fldMicroOrganisme-1" mode="Transparent" x="154" y="61" width="275" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{resultat}.getMicroOrganismes().iterator().next().getDescription()]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="fldMicroOrganisme-1" mode="Transparent" x="154" y="75" width="275" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{resultat}.getTypeResultats().iterator().next().getDescription()+ ". " +(($F{resultat}.getTypeResultats().iterator().hasNext()) ? $F{resultat}.getTypeResultats().iterator().next().getDescription() : "")]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

1 个答案:

答案 0 :(得分:0)

您可以使用Java 8方法和lambda表达式从列表中连接值。请参阅herehere如何在报告中启用lambda表达式。

您需要的表达式类似于

<textFieldExpression><![CDATA[$F{resultat}.getTypeResultats().stream().map(e -> e.getDescription()).collect(java.util.stream.Collectors.joining(", "))]]></textFieldExpression>