JasperReports - 带表的子报表 - 参数传递

时间:2014-04-07 21:43:31

标签: java jasper-reports

我正在使用JRBeanCollectionDataSource对象来处理数据,并试图弄清楚如何安排Java对象(我猜想,JRXML中的相关设置)。

具体来说,我已经定义了一个非常独立的子报告,它使用了一个Table。为了填充表的行,我传入一个数据源,它是一个数据bean数组,作为参数传递给报告。

但是,当将该报告作为主报告中的子报告嵌入时,我不清楚我应该如何传递相应的数据对象。但我似乎无法找到任何这方面的例子。如果您知道任何示例,那就太棒了!

否则,到目前为止我所得到的:

每个子报告都有一些参数和3个表格。

数据Bean(每个基本上都是表的一行):

public class DataBean {
    public String c1_name;
    public String c2_name;
    public String c1_value;
    public String c2_value;

    public String getC1_name() {
        return c1_name;
    }
    public void setC1_name(String s) {
        this.c1_name = s;
    }
    public String getC2_name() {
        return c2_name;
    }
    public void setC2_name(String s) {
        this.c2_name = s;
    }
    public String getC1_value() {
        return c1_value;
    }
    public void setC1_value(String s) {
        this.c1_value = s;
    }
    public String getC2_value() {
        return c2_value;
    }
    public void setC2_value(String s) {
        this.c2_value = s;
    }
}

Master Bean(包含所有3个表的数据集和每个子报告所需的3个参数):

import java.util.ArrayList;

public class MasterBean {
    private ArrayList<DataBean> t1; // Table 1
    private ArrayList<DataBean> t2a; // Table 2, column 1
    private ArrayList<DataBean> t2b; // Table 2, column 2
    private String disclaimer;
    private String event_detail;
    private String event_type;

    public ArrayList<DataBean> getT1() {
        return t1;
    }
    public void setT1(ArrayList<DataBean> beanlist) {
        this.t1 = beanlist;
    }

    public ArrayList<DataBean> getT2a() {
        return t2a;
    }
    public void setT2a(ArrayList<DataBean> beanlist) {
        this.t2a = beanlist;
    }

    public ArrayList<DataBean> getT2b() {
        return t2b;
    }
    public void setT2b(ArrayList<DataBean> beanlist) {
        this.t2b = beanlist;
    }

    public String getDisclaimer() {
        return disclaimer;
    }
    public void setDisclaimer(String s) {
        this.disclaimer = s;
    }

    public String getEvent_detail() {
        return event_detail;
    }
    public void setEvent_detail(String s) {
        this.event_detail = s;
    }

    public String getEvent_type() {
        return event_type;
    }
    public void setEvent_type(String s) {
        this.event_type = s;
    }
}

为了填写报告(有趣的部分),我有点想要它,因为我没有找到任何文档:

            ... (snip) ...
            DataBeanList DataBeanList = new DataBeanList();

            ArrayList<DataBean> t1_1 = DataBeanList.getDataBeanList(0);
            ArrayList<DataBean> t2a_1 = DataBeanList.getDataBeanList(1);
            ArrayList<DataBean> t2b_1 = DataBeanList.getDataBeanList(2);
            MasterBean sub1 = new MasterBean();
            sub1.setT1(t1_1);
            sub1.setT2a(t2a_1);
            sub1.setT2b(t2b_1);
            sub1.setDisclaimer("Disclaimer 1 goes here");
            sub1.setEvent_detail("Event Detail 1 goes here");
            sub1.setEvent_type("Event Type 1 goes here");

            ArrayList<DataBean> t1_2 = DataBeanList.getDataBeanList(0);
            ArrayList<DataBean> t2a_2 = DataBeanList.getDataBeanList(1);
            ArrayList<DataBean> t2b_2 = DataBeanList.getDataBeanList(2);
            MasterBean sub2 = new MasterBean();
            sub2.setT1(t1_2);
            sub2.setT2a(t2a_2);
            sub2.setT2b(t2b_2);
            sub2.setDisclaimer("Disclaimer 2 goes here");
            sub2.setEvent_detail("Event Detail 2 goes here");
            sub2.setEvent_type("Event Type 2 goes here");

            ArrayList<DataBean> t1_3 = DataBeanList.getDataBeanList(0);
            ArrayList<DataBean> t2a_3 = DataBeanList.getDataBeanList(1);
            ArrayList<DataBean> t2b_3 = DataBeanList.getDataBeanList(2);
            MasterBean sub3 = new MasterBean();
            sub3.setT1(t1_3);
            sub3.setT2a(t2a_3);
            sub3.setT2b(t2b_3);
            sub3.setDisclaimer("Disclaimer 3 goes here");
            sub3.setEvent_detail("Event Detail 3 goes here");
            sub3.setEvent_type("Event Type 3 goes here");

            ArrayList<MasterBean> masterList = new ArrayList<MasterBean>();
            masterList.add(sub1);
            masterList.add(sub2);
            masterList.add(sub3);

            JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(masterList);

            Map params = new HashMap();

            params.put("report_type","Master Report Type");

            try {

/deaton/jasper/jasperreports-5.5.1/demo/samples/testme/reports/CASummary.jrxml“);                     JasperReport jasperSub = JasperCompileManager.compileReport(“/ home / deaton / jasper / jasperreports-5.5.1 / demo / samples / testme / reports / CAEventDetails.jrxml”);                     params.put( “subreportParameter”,jasperSub); params,tdatasource); params,new JREmptyDataSource(2));                     JasperFillManager.fillReportToFile(“build / reports / CASummary.jasper”,params,datasource);                 ......(剪辑)...

对于子报告,这里是JRXML中的一些有趣的部分:

    ... (snip) ...
    <subDataset name="instrument_details" uuid="6d672249-f7b2-44ca-9f8a-6cb4995fdfab">
            <field name="c1_name" class="java.lang.String"/>
            <field name="c1_value" class="java.lang.String"/>
            <field name="c2_name" class="java.lang.String"/>
            <field name="c2_value" class="java.lang.String"/>
    </subDataset>
    <subDataset name="event_information" uuid="f2f954af-98c1-4f2b-9c57-e666d309fdce">
            <field name="c1_name" class="java.lang.String"/>
            <field name="c1_value" class="java.lang.String"/>
            <field name="c2_name" class="java.lang.String"/>
            <field name="c2_value" class="java.lang.String"/>
    </subDataset>
    <parameter name="disclaimer" class="java.lang.String"/>
    <parameter name="event_detail" class="java.lang.String"/>
    <parameter name="event_type" class="java.lang.String"/>

    <parameter name="DS1" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <parameter name="DS2" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <parameter name="DS3" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>

    <field name="event_comments" class="java.lang.String"/>
    <field name="event_description" class="java.lang.String"/>

    ... (skip down) ...

                    <componentElement>
                            <reportElement key="table" style="table" x="0" y="20" width="572" height="20" uuid="a3e03a5d-48cd-4bf4-82f4-df7de7958a75"/>
                            <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="instrument_details" uuid="1e25358d-f600-4ea3-80b3-f15d4438992c">
                                            <datasetParameter name="REPORT_DATA_SOURCE">
                                                    <datasetParameterExpression><![CDATA[$P{DS1}]]></datasetParameterExpression>
                                            </datasetParameter>
                                    </datasetRun>
                                    <jr:column width="100" uuid="f4c26fd2-95ae-49a4-8b6f-8fbe6f47f810">
                                            <jr:detailCell style="table_TD" height="20" rowSpan="1">
                                                    <textField>
                                                            <reportElement x="0" y="0" width="100" height="20" uuid="578b42c1-3065-419a-8758-4e1b4946ca04"/>
                                                            <textFieldExpression><![CDATA[$F{c1_name}]]></textFieldExpression>
                                                    </textField>
                                            </jr:detailCell>
                                    </jr:column>
                                    <jr:column width="186" uuid="ceb83fa5-ed21-4cca-b374-aae224688297">
                                            <jr:detailCell style="table_TD" height="20" rowSpan="1">
                                                    <textField>
                                                            <reportElement x="0" y="0" width="186" height="20" uuid="d5cdb3c8-af89-4af7-bd94-ee6fda45f327"/>
                                                            <textFieldExpression><![CDATA[$F{c1_value}]]></textFieldExpression>
                                                    </textField>
                                            </jr:detailCell>
                                    </jr:column>
                                    <jr:column width="100" uuid="ca9810a7-6684-49e2-b673-ed84278a5d20">
                                            <jr:detailCell style="table_TD" height="20" rowSpan="1">
                                                    <textField>
                                                            <reportElement x="0" y="0" width="100" height="20" uuid="e6765684-2042-47e7-858d-b2b0d9ae8eaf"/>
                                                            <textFieldExpression><![CDATA[$F{c2_name}]]></textFieldExpression>
                                                    </textField>
                                            </jr:detailCell>
                                    </jr:column>
                                    <jr:column width="186" uuid="c99af79c-5373-41c4-a05a-4538da4504df">
                                            <jr:detailCell style="table_TD" height="20" rowSpan="1">
                                                    <textField>
                                                            <reportElement x="0" y="0" width="186" height="20" uuid="e4079374-640c-4a79-b315-a1f33079bfd3"/>
                                                            <textFieldExpression><![CDATA[$F{c2_value}]]></textFieldExpression>
                                                    </textField>
                                            </jr:detailCell>
                                    </jr:column>
                            </jr:table>
                    </componentElement>
    ... (above repeated for other tables) ...

最后,我怀疑我已经离开的部分,主报告,我无能为力:

    ... (snip) ...
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
            <defaultValueExpression><![CDATA["/home/deaton/jasper/jasperreports-5.5.1/demo/samples/testme/build/reports/"]]></defaultValueExpression>
    </parameter>
    <parameter name="report_type" class="java.lang.String"/>
    <parameter name="master_source" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <parameter name="subreportParameter" class="net.sf.jasperreports.engine.JasperReport"/>

    <field name="disclaimer" class="java.lang.String"/>
    <field name="event_detail" class="java.lang.String"/>
    <field name="event_type" class="java.lang.String"/>
    <field name="t1" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <field name="t2a" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <field name="t2b" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>

    ... (skip a bit) ...

    <detail>
            <band height="125" splitType="Stretch">
                    <subreport>
                            <reportElement x="0" y="0" width="572" height="125" uuid="aba34d2d-04ee-4ff7-803b-14424c825b3f"/>
                            <subreportParameter name="disclaimer">
                                    <subreportParameterExpression>
                                            <![CDATA[$F{disclaimer}]]>
                                    </subreportParameterExpression>
                            </subreportParameter>

                            <subreportParameter name="event_detail">
                                    <subreportParameterExpression>
                                            <![CDATA[$F{event_detail}]]>
                                    </subreportParameterExpression>
                            </subreportParameter>

                            <subreportParameter name="event_type">
                                    <subreportParameterExpression>
                                            <![CDATA[$F{event_type}]]>
                                    </subreportParameterExpression>
                            </subreportParameter>

                            <subreportParameter name="DS1">
                                    <subreportParameterExpression>
                                            <![CDATA[$F{t1}]]>
                                    </subreportParameterExpression>
                            </subreportParameter>

                            <subreportParameter name="DS2">
                                    <subreportParameterExpression>
                                            <![CDATA[$F{t2a}]]>
                                    </subreportParameterExpression>
                            </subreportParameter>

                            <subreportParameter name="DS3">
                                    <subreportParameterExpression>
                                            <![CDATA[$F{t2b}]]>
                                    </subreportParameterExpression>
                            </subreportParameter>

                            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "CAEventDetails.jasper"]]></subreportExpression>
                    </subreport>
            </band>
    </detail>
    ... (snip) ...

当我运行它时,它编译,填写报告,但然后......没有。没有数据进入我的报告。我甚至无法在主报表中看到我的子报表中的任何静态元素(尝试使用带有倍数的空数据集,但仍然是zilch)。

所以...我怀疑我做了一些根本错误的事情,但我似乎无法弄清楚如何根据主数据源将参数适当地传递给子报告,或者作为a的子记录参数。我尝试了很多组合,但是一切似乎都无法编译,或者没有给我任何数据。

0 个答案:

没有答案