如何在Jasper报告的子报告中水平打印列表

时间:2019-01-28 15:14:57

标签: jasper-reports subreport

我有两个模型Customer和Order,如下所示:

class Customer{
    private String customerName;
    private List<Order> orders; 
}

class Order{
     private String orderName;
}

我正在创建一个pdf文件,该文件垂直列出了所有客户名称,并水平列出了相应的订单,如下所示:

customer1                order1                     order2

customer2                order3                     order4
.
.
.

其中order1order2属于customer1,而order3order4属于customer2

我对每个客户对象所做的工作,将打印客户名称并将订单列表传递到另一个子报表,并在此子报表中,我将打印此订单列表中的所有元素,但要水平而不是垂直地打印。

这是我的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="epicsReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="98e422a6-47f1-4bf1-9436-b6cbfdc8c904">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <parameter name="ordersSubReport" class="net.sf.jasperreports.engine.JasperReport"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="customerName" class="java.lang.String"/>
    <field name="orders" class="java.util.List"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="25" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="270" height="25" uuid="26bf39b4-9072-4edc-a846-3141bff7da16">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="8d2c2571-e603-4eba-8048-3053e3282a99"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{customerName}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="312" y="0" width="200" height="25" uuid="b2675578-87cc-4a7b-8b5a-2c8af0fbde73"/>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{orders})]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{ordersSubReport}]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

我的子报表:

<?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="skillsDataReport" columnCount="10" printOrder="Horizontal" pageWidth="595" pageHeight="842" columnWidth="55" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c6d95fd0-d8af-4727-999c-9804a17721e4">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="orderName" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="25" splitType="Stretch">
            <textField>
                <reportElement x="9" y="0" width="100" height="25" uuid="1ac7d520-9918-4106-b4ad-1b446da6a01f"/>
                <textFieldExpression><![CDATA[$F{orderName}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

作为主报告的数据源,我向其传递了一个客户列表:new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{customers})

作为子报表的数据源,我为每个客户对象传递了一个订单列表:new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{orders})

代码已成功编译,但未按预期显示订单列表。我只看到印有customerName的清单,但看不到订单清单。

我在这里做错了什么?任何帮助对我来说都是胶水:)

0 个答案:

没有答案