Jasper 报告:收集 bean 的第一个元素被跳过

Jasper reports: the first element of the collection bean is skipped

当使用抽象报告和内容页面加上 bean 集合作为数据源时,将跳过集合的第一个元素

有人遇到过同样的问题吗?我已经尝试了多种方法,并在此处和 Jasper Reports 社区中阅读了很多帖子,但都没有成功

摘要书代码(主要):

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 6.17.0.final using JasperReports Library version 6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd  -->
    <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="Abstract_refactored" pageWidth="595" pageHeight="842" sectionType="Part" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="d2716064-8ae4-40cf-a575-33afba400e3a">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <property name="net.sf.jasperreports.print.create.bookmarks" value="true"/>
        <property name="com.jaspersoft.studio.book.group.cover.header" value="Cover and Table of Contents"/>
        <property name="com.jaspersoft.studio.book.group.cover.footer" value="Backcover"/>
        <parameter name="ubersichtCollectionRefactored" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
        <parameter name="SUBREPORT_DIR" class="java.lang.String" evaluationTime="Early">
            <defaultValueExpression><![CDATA["/Users/Beer/JaspersoftWorkspace/MyReports/"]]></defaultValueExpression>
        </parameter>
        <parameter name="CollectionBeanParam" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
        <group name="cover"/>
        <detail>
            <part evaluationTime="Now" uuid="6b9e8284-92b6-4004-a896-77e6650f3afa">
                <p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd" usingCache="false">
                    <subreportParameter name="REPORT_DATA_SOURCE">
                        <subreportParameterExpression><![CDATA[$P{ubersichtCollectionRefactored}]]></subreportParameterExpression>
                    </subreportParameter>
                    <subreportParameter name="ubersichtCollectionRefactored">
                        <subreportParameterExpression><![CDATA[$P{ubersichtCollectionRefactored}]]></subreportParameterExpression>
                    </subreportParameter>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "ubersicht_refactored.jasper"]]></subreportExpression>
                </p:subreportPart>
            </part>
            <part uuid="3bbe9fbc-42db-4b15-8120-416514e99d4c">
                <p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
                    <subreportParameter name="CollectionBeanParam">
                        <subreportParameterExpression><![CDATA[$P{CollectionBeanParam}]]></subreportParameterExpression>
                    </subreportParameter>
                    <subreportParameter name="REPORT_DATA_SOURCE">
                        <subreportParameterExpression><![CDATA[$P{CollectionBeanParam}]]></subreportParameterExpression>
                    </subreportParameter>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "investmentcosts.jasper"]]></subreportExpression>
                </p:subreportPart>
            </part>
        </detail>
    </jasperReport>

内容页:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.17.0.final using JasperReports Library version 6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd  -->
<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="investmentcosts" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="febeabfd-a22b-490f-8e0b-ff52efdc9ffa">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="investmentCostDataset" uuid="1fe5fdcb-a4b2-4546-bd90-2f5578170233">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <field name="product" class="java.lang.String"/>
        <field name="quantity" class="java.lang.Integer"/>
        <field name="cost" class="java.lang.String"/>
    </subDataset>
    <parameter name="CollectionBeanParam" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <detail>
        <band height="257" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="10" width="524" height="150" uuid="5922b981-1b64-4daa-b492-c1b03192d003">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" whenNoDataType="AllSectionsNoDetail">
                    <datasetRun subDataset="investmentCostDataset" uuid="9d4a3bba-0367-4a02-9a12-5d69ea0f77c8">
                        <datasetParameter name="REPORT_DATA_SOURCE">
                            <datasetParameterExpression><![CDATA[$P{CollectionBeanParam}]]></datasetParameterExpression>
                        </datasetParameter>
                        <dataSourceExpression><![CDATA[$P{CollectionBeanParam}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="260" uuid="7e3b0b3c-03c7-438d-8d74-c70c439d2ac6">
                        <jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
                        <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="260" height="30" uuid="88e907e3-8090-4c62-8b42-692390955241"/>
                                <textElement textAlignment="Center" rotation="None"/>
                                <text><![CDATA[Produkt]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="260" height="30" uuid="66da7f58-d69f-4a13-b6d5-a8e37d7707b8"/>
                                <textElement textAlignment="Left">
                                    <paragraph leftIndent="2"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$F{product}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="130" uuid="c098c3a0-7e77-4a47-9b72-530d5a4b10ff">
                        <jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
                        <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="130" height="30" uuid="f64de092-ac20-4984-9b55-569910a31b6c"/>
                                <textElement textAlignment="Center" rotation="None"/>
                                <text><![CDATA[Menge]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="130" height="30" uuid="a62bdcb7-9257-4b0e-a0e9-10a94efc65f1"/>
                                <textElement textAlignment="Center"/>
                                <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="130" uuid="2fe1cd34-cfc4-426b-b37a-9c1393bffe5b">
                        <jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
                        <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="130" height="30" uuid="67b1e188-c745-4651-a9a9-892d71c31255"/>
                                <textElement textAlignment="Center" rotation="None"/>
                                <text><![CDATA[Preis]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="130" height="30" uuid="2888a61e-7a85-4117-91fc-cab41dc5e00a"/>
                                <textElement textAlignment="Center"/>
                                <textFieldExpression><![CDATA[$F{cost}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

添加 java 代码:

    File fileAbstractBook = ResourceUtils.getFile("classpath:report/Abstract_refactored.jrxml");
    InputStream inputAbstractBook = new FileInputStream(fileAbstractBook.getAbsolutePath());
    JasperDesign  jasperAbstractBook = JRXmlLoader.load(inputAbstractBook);
    JasperReport abstractBook = JasperCompileManager.compileReport(jasperAbstractBook);
    
    JRBeanCollectionDataSource dataSource1 = new JRBeanCollectionDataSource(investmentCostsList);
    JRBeanCollectionDataSource dataSource2 = new JRBeanCollectionDataSource(ubersichtList);
    Map<String, Object> params = new HashMap<>();
    

    //Path path = Paths.get(fileInvestmentCosts.getAbsolutePath());
    Path path = Paths.get(fileSub.getAbsolutePath());
    System.out.println("Path of the report resources"+ path.getParent());

    params.put("CollectionBeanParam", dataSource1);//ubersichtCollectionBeanParam
    params.put("ubersichtCollectionRefactored", dataSource2);//ubersichtCollectionBeanParam
    params.put("SUBREPORT_DIR", path.getParent() + "/");
    JasperPrint print = JasperFillManager.fillReport(abstractBook, params, new JREmptyDataSource());

您将 $P{CollectionBeanParam} 作为用于主报告数据集的 REPORT_DATA_SOURCE 和作为数据源传递给 [=25] 的 CollectionBeanParam 传递=] 子数据集。主数据集使用数据源实例中的第一条记录,然后将其传递给 table 子数据集,子数据集迭代剩余的记录。

从外观上看,investmentcosts 部分的主数据集不会从数据源读取任何内容,因此您应该做的是传递一个空数据源作为 REPORT_DATA_SOURCE对于部分。如

        <part uuid="3bbe9fbc-42db-4b15-8120-416514e99d4c">
            <p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
                <subreportParameter name="CollectionBeanParam">
                    <subreportParameterExpression><![CDATA[$P{CollectionBeanParam}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="REPORT_DATA_SOURCE">
                    <subreportParameterExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></subreportParameterExpression>
                </subreportParameter>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "investmentcosts.jasper"]]></subreportExpression>
            </p:subreportPart>
        </part>

请注意,在 table datasetRun 中,您同时将 REPORT_DATA_SOURCE 作为参数和 dataSourceExpression。你只需要一个,尽管同时拥有两个应该不会破坏任何东西。