【问题标题】:Iterate Set Field in a subReport Jasper Report在子报表 Jasper 报表中迭代设置字段
【发布时间】:2017-03-29 01:41:24
【问题描述】:

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

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

我从字段中获取预期值,但它总是获取集合中的第一个元素。

如何迭代我的集合?

感谢您的帮助。

这是子报告:

<?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>

【问题讨论】:

  • 您不需要手动迭代。 JR引擎自己做。您应该发布简单的小示例来重现问题:jrxml(主 + 子报告)+ Java 代码(如果有)
  • @AlexK 不幸的是,我的主要报告的源代码的行数超过了 stackoverflow 所允许的行数。我希望这足够有用。感谢您的帮助
  • 创建一个minimal reproducible example,从而创建一个你正在尝试做的小例子,我们可以轻松地复制它。
  • 您希望如何打印集合中的值(当有多个值时)?在同一行,用分隔符连接?还是在不同的线路上?
  • @dada67 确切地说,当有多个值用逗号分隔时。我有一组包含我要显示的信息的对象。我已经得到了第一个对象的价值。但我想知道是否可以使用 JR 表达式迭代 java 集合,因为我总是得到集合的第一个元素

标签: java jasper-reports


【解决方案1】:

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

您需要的表达式类似于

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多