如何在多个子报表上使用相同的JRBeanCollectionDataSource?

时间:2019-01-21 10:32:55

标签: java jasper-reports datasource subreport

我用相同的数据源调用两个子报表,但是当我打印此报表时,只有第一个子报表中填充了表格。

这就是我通过statsData1进行报告的方式。

List<BasicStats> summaryList = new ArrayList<>();
summaryList.add(new BasicStats("A", 5d, 6.5d, 25d));
summaryList.add(new BasicStats("B", 1.47d, 2.7d, 72d));
summaryList.add(new BasicStats("c", .39d, 9.5d, 91d));
summaryList.add(new BasicStats("D", 8.2d, 1.3d, 37d));
summaryList.add(new BasicStats("E", 6.8d, 4.5d, 19d));

Map<String, Object> paramsMap = new HashMap<>();
JRBeanCollectionDataSource jbcdSummary = new JRBeanCollectionDataSource(summaryList);
paramsMap.put("statsData1", jbcdSummary);

尽管,我注意到,如果我像下面那样传递2个不同的对象,则它可以工作(对jrxml进行了相关修改),我得到了两个子报表。

paramsMap.put("statsData1", new JRBeanCollectionDataSource(summaryList));
paramsMap.put("statsData2", new JRBeanCollectionDataSource(summaryList));

这与JRBeanCollectionDataSource是可消耗对象有关吗?

这是我的主要jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="main" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6a63a79b-6f04-46c7-b3b9-a067e835ea4d">
    <parameter name="subRptTitle" class="java.lang.String"/>
    <parameter name="statsData1" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="250" y="20" width="100" height="30" uuid="42f86896-5b37-4ee3-8f0c-29bf90eb3846"/>
                <textFieldExpression><![CDATA[$P{subRptTitle}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <summary>
        <band height="203" splitType="Stretch">
            <subreport>
                <reportElement x="10" y="31" width="281" height="161" uuid="73d60b1f-1b66-44b1-a8a5-d9738067aaf1"/>
                <subreportParameter name="statsData">
                    <subreportParameterExpression><![CDATA[$P{statsData1}]]></subreportParameterExpression>
                </subreportParameter>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
                <subreportExpression><![CDATA["sub-table.jasper"]]></subreportExpression>
            </subreport>
            <subreport>
                <reportElement x="310" y="31" width="273" height="161" uuid="686bb10d-7018-4cf1-b72b-c17e534ec5e0"/>
                <subreportParameter name="statsData">
                    <subreportParameterExpression><![CDATA[$P{statsData1}]]></subreportParameterExpression>
                </subreportParameter>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
                <subreportExpression><![CDATA["sub-table.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </summary>
</jasperReport>

这是我的子报告sub-table.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="main" pageWidth="200" pageHeight="842" columnWidth="200" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6a63a79b-6f04-46c7-b3b9-a067e835ea4d">
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#FBFDFF"/>
        </conditionalStyle>
    </style>
    <subDataset name="tableDataSet" uuid="2914f7c6-c2d7-448b-b0c1-090970e18ed6">
        <field name="subEntity" class="java.lang.String"/>
        <field name="total" class="java.lang.Double"/>
        <field name="pending" class="java.lang.Double"/>
        <field name="percentage" class="java.lang.Double"/>
        <variable name="vTotalSum" class="java.lang.Double" calculation="Sum">
            <variableExpression><![CDATA[$F{total}]]></variableExpression>
        </variable>
        <variable name="vPendingSum" class="java.lang.Double" calculation="Sum">
            <variableExpression><![CDATA[$F{pending}]]></variableExpression>
        </variable>
        <variable name="vPercentageSum" class="java.lang.Double" calculation="Sum">
            <variableExpression><![CDATA[$F{percentage}]]></variableExpression>
        </variable>
    </subDataset>
    <parameter name="subRptTitle" class="java.lang.String"/>
    <parameter name="statsData" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="50" y="30" width="100" height="30" uuid="42f86896-5b37-4ee3-8f0c-29bf90eb3846"/>
                <textFieldExpression><![CDATA[$P{subRptTitle}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <summary>
        <band height="203" splitType="Stretch">
            <componentElement>
                <reportElement x="9" y="11" width="182" height="181" uuid="7451c7aa-8532-471e-ad02-eb98ca21d171">
                    <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">
                    <datasetRun subDataset="tableDataSet" uuid="eecbe24c-593f-42fa-a7ab-2fbe8b59cb34">
                        <dataSourceExpression><![CDATA[$P{statsData}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="45" uuid="6ea97833-5ba6-48f1-9096-66bb2d6d5978">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30"/>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="45" height="30" uuid="afce87c3-7a33-4b79-9627-2c2f37d78609"/>
                                <text><![CDATA[subEntity]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="45" height="30" uuid="f312e4dc-bd65-4cf6-8c2c-27c1d88940b9"/>
                                <textFieldExpression><![CDATA[$F{subEntity}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="45" uuid="89672e7b-ee57-4a0d-8f6e-885c189a9c1d">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="45" height="30" uuid="c8ef39c5-ea48-41e4-a069-9d07e1ce301b"/>
                                <textFieldExpression><![CDATA[$V{vTotalSum}]]></textFieldExpression>
                            </textField>
                        </jr:tableFooter>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="45" height="30" uuid="c57bbc8c-b840-45fd-b21a-3b7121c29f71"/>
                                <text><![CDATA[total]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="45" height="30" uuid="bf1068c6-42f2-4cb4-b1f0-8b195d362247"/>
                                <textFieldExpression><![CDATA[$F{total}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="45" uuid="4b3eea40-3cca-48a8-961f-6fbe9edfb973">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="45" height="30" uuid="f4787fdd-5f5b-4f03-97e7-05e4c2c34f0f"/>
                                <textFieldExpression><![CDATA[$V{vPendingSum}]]></textFieldExpression>
                            </textField>
                        </jr:tableFooter>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="45" height="30" uuid="5aa2afde-3b32-480e-b115-9ffddf95ab64"/>
                                <text><![CDATA[pending]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="45" height="30" uuid="89a528bc-2e09-467f-b709-7434bc6732da"/>
                                <textFieldExpression><![CDATA[$F{pending}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="45" uuid="a7c26c8f-361f-4d33-820a-abd2cb3268ad">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="45" height="30" uuid="7e1115c0-35fb-48d2-b0bd-b36e5a782d67"/>
                                <textFieldExpression><![CDATA[$V{vPercentageSum}]]></textFieldExpression>
                            </textField>
                        </jr:tableFooter>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="45" height="30" uuid="20743d33-811c-411a-b7d1-c78e57ccd018"/>
                                <text><![CDATA[percentage]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="45" height="30" uuid="ebeb9419-af5d-4475-a8f7-ab13ab4a7b9f"/>
                                <textFieldExpression><![CDATA[$F{percentage}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </summary>
</jasperReport>

更改为sub-table.jrxml使其可以使用2个参数

<parameter name="statsData1" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<parameter name="statsData2" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<subreport>
                    <reportElement x="310" y="31" width="273" height="161" uuid="686bb10d-7018-4cf1-b72b-c17e534ec5e0"/>
                    <subreportParameter name="statsData">
                        <subreportParameterExpression><![CDATA[$P{statsData2}]]></subreportParameterExpression>
                    </subreportParameter>
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
                    <subreportExpression><![CDATA["sub-table.jasper"]]></subreportExpression>
                </subreport>

对此有什么更好的解决方法?

1 个答案:

答案 0 :(得分:1)

您可以在api中看到的JRBeanCollectionDataSource同时实现JRDataSourceJRRewindableDataSource

JRDataSource是一种消耗性数据源(仅next()方法),但是JRRewindableDataSource添加了moveFirst()方法,可以倒带数据源。

但是,在jrxml文件(设计文件)中执行此操作并不简单,我不建议这样做。

您所遇到的最简单的解决方案是,与其传递JRBeanCollectionDataSource而不传递List(您的summaryList),而是在您的jrxml中创建JRBeanCollectionDataSource多次,而不是根据需要。

示例

在Java中

paramsMap.put("statsData", summaryList);

在jrxml中

<parameter name="statsData" class="java.util.List"/>
...
<subreportParameter name="statsData">
    <subreportParameterExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{statsData})]]></subreportParameterExpression>
</subreportParameter>
...
  

以后的读者注意:该子报表特别是它将数据源作为参数传递(将用于填充子报表中的jr:table),通常您将在<dataSourceExpression>中传递数据源