如何将ArrayList传递给JasperReports?

时间:2010-09-02 07:17:05

标签: jasper-reports

我是JasperReports的新手。我想将ArrayList传递给子报告的子报告。

我有一个包含一个subreport1的主报告,这个subreport1封装了一个
subreport2 inside。

现在如何将arrayList传递给subreport2?

MasterReport -> SubReport -> SubReport

注意 :我正在使用iReport创建jasper模板并将datalist从我的DAO java类传递给jasper。

下面是我的Form bean Class

public class CollatReportData extends BaseItem {

    private List<CusipData> listCusipData = null;
    private String dealerID = null;
    private String tripID = null;
    private String loanNo = null;
    private String dealerName = null;
    private String tripDealerLoan = null;

    public CollatReportData() {
        super();
    }

    public List<CusipData> getListCusipData() {
        return listCusipData;
    }

    public void setListCusipData(List<CusipData> listCusipData) {
        this.listCusipData = listCusipData;
    }

    public String getDealerID() {
        return dealerID;
    }

    public void setDealerID(String dealerID) {
        this.dealerID = dealerID;
    }

    public String getTripID() {
        return tripID;
    }

    public void setTripID(String tripID) {
        this.tripID = tripID;
    }
} // and so on for other variables..

我的DAO java类位于

之下
public List<Object> getCollatData(String custName, String ctripid, 
        String dealerid, String userID) {

    final Connection conn = super.getCurrentConnection();
    String sqlQueryTrip = null;
    ResultSet rsCollat = null;
    String tripID = null;
    String dealerID = null;
    String cusSysID = null;
    String loanNo = null;
    String txnNo = null;
    String cusipNo = null;
    String cusipStatus = null;
    String parVal = null;
    String tripDealerLoan = null;
    String OldtripDealerLoan = "";
    String NewtripDealerLoan = "";

    CollatReportData reportData = null;
    CusipData cusipData = null;
    List listCusip = new ArrayList<Object>();
    List CollatList = new ArrayList<Object>();
    try {
        PreparedStatement pstmtTrip;
        sqlQueryTrip = "SELECT iscl_sys_id, iscl_trip_id, iscl_trip_dealer_id, "
                + "iscl_cus_sys_id, iscl_bankref_cd, iscl_bdas_db_cd, "
                + "iscl_loan_no, iscl_txn_no, iscl_cusip_no, iscl_status_cd, "
                + "iscl_sec_de_tx, iscl_par_val_am, iscl_market_val_am, "
                + "scl_pri_source_cd, iscl_colla_mar_val_am, iscl_mature_dt, "
                + "iscl_sec_ty, iscl_sec_rt, iscl_sec_price_am, iscl_sec_factor, "
                + "iscl_sec_margin, iscl_sec_accrued_am "
                + "FROM BDS_DBA.INVCONF_SHELL_COLLAT "
                + "WHERE iscl_cus_sys_id='" + custName + "'";
        pstmtTrip = conn.prepareStatement(sqlQueryTrip);
        rsCollat = pstmtTrip.executeQuery();
        if (rsCollat != null) {
            while (rsCollat.next()) {
                tripID = rsCollat.getString("iscl_trip_id");
                dealerID = rsCollat.getString("iscl_trip_dealer_id");
                loanNo = rsCollat.getString("iscl_loan_no");

                tripDealerLoan = tripID + dealerID + loanNo;
                cusipData = new CusipData();
                cusipData.setTripID(tripID);
                cusipData.setCusipNo(cusipNo);
                cusipData.setTripDealerLoan(tripDealerLoan);

                listCusip.add(cusipData);
            } // end rsCollat
        } // end if
        CusipData cusipData1 = new CusipData();
        List CusipList = new ArrayList<Object>();

        for (int io = 0; io < listCusip.size(); io++) {
            cusipData1 = (CusipData) listCusip.get(io);
            NewtripDealerLoan = cusipData1.getTripDealerLoan();
            tripID = cusipData1.getTripID();
            dealerID = cusipData1.getDealerID();
            loanNo = cusipData1.getLoanNo();

            if (NewtripDealerLoan.equalsIgnoreCase(OldtripDealerLoan) || OldtripDealerLoan.equalsIgnoreCase("")) {
                CusipList.add(cusipData1);

            }
            if (!NewtripDealerLoan.equalsIgnoreCase(OldtripDealerLoan)) {
                reportData = new CollatReportData();
                reportData.setTripID(tripID);
                reportData.setTripDealerLoan(NewtripDealerLoan);
                reportData.setListCusipData(CusipList);
                //and so on
                //........
                CollatList.add(reportData);

                CusipList = null;
            }
            OldtripDealerLoan = NewtripDealerLoan;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Inside LoanDetailDAO:strTripQuery:Exception" + sqlQueryTrip + e);
    }

    return CollatList;
}

上面的DAO类返回List并通过Hashmap Param将此Arraylist传递给Jasper模板。

下面是我的示例Master jasper模板,从这里我将ArrayList传递给subReport,如

     <parameter name="list" isForPrompting="false" class="java.util.List"/>

     <detail>
        <band height="100"  isSplitAllowed="true" >
            <subreport  isUsingCache="true">
                <reportElement
                    x="30"
                    y="20"
                    width="170"
                    height="40"
                    key="subreport-1"/>
                <dataSourceExpression><![CDATA[new JRBeanCollectionDataSource( $P{list})]]></dataSourceExpression>
                <subreportExpression  class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "Collateral_SubReport1.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>

其中$p{list}ArrayList。并使用此列表在subReport1 asusual中打印

在运行代码时,我收到以下错误:

Error retrieving field value from bean : tripID

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:9)

通过传递集合数据源:new BeanCollectionDataSource(yourArrayList);

来完成

然后获取JasperPrint对象:

JasperPrint jasperPrint = 
      JasperFillManager.fillReport(jasperReport, params, dataSource);

要传递到子报表,您有两个选择:

  • 如果它是每行子报告,只需将数组作为bean的属性。即List<Something>其中Something的属性类型为List<AnotherThing>
  • 如果它是整个报告的一个,请将其作为参数传递(上面params)。

答案 1 :(得分:5)

步骤:1定义一个像
的字段 字段名称=&#34; listCusipData&#34;和class =&#34; java.util.ArrayList&#34;

步骤:2在子报表dataSourceExpression中使用此表达式 new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ F {listCusipData},false)

答案 2 :(得分:0)

您可以使用列表和/或子报表,数据源和JRBeanCollectionDatasource来完成。

检查此示例:

http://siempredesdeelcurro.blogspot.com.es/2013/06/jasper-reports-crear-report-utilizando.html

这是西班牙文,但按你的名字,我想你可以阅读它;)

对于非西班牙语的人,您仍然可以使用它,因为在教程结束时您可以使用jrxml模板找到XML。