如何使用JasperReports为单个报表传递多个结果集? [英] How to pass several resultsets for single report using JasperReports?

查看:83
本文介绍了如何使用JasperReports为单个报表传递多个结果集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的报告中,我有一组字段和两个数据集.我想一次执行三个过程来运行报告.执行字段集的第一个过程,数据集的剩余两个过程.我通过一个结果集来执行报告,效果很好.但是我想再传递两个来自execute()和port()方法的结果集.是否可以使用JRResultSetDataSource或任何其他选项传递多个结果集?

In my report I have a set of fields and two datasets. I want to execute three procedure at a time to run a report. First procedure for execute set of fields and remaining two procedure for datasets. I pass one resultset to execute a report is working fine. But I want to pass two more resultset from execute() and port() methods. Is it possible to pass multiple resultset using JRResultSetDataSource or any other option?

public ResultSet execute() throws ClassNotFoundException {
    Statement stmt;
    ResultSet resultset = null;
    Connection con = null;
    try {
        String selectstatement = "CALL P_Select_Salary2 ('2013-01-01', '2013-01-31', 1, 'Salary_OA')";
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/compliance?user=root&password=root");
        stmt = con.createStatement();
        resultset = stmt.executeQuery(selectstatement);

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        close(con);
        close(resultset);
    }
    return resultset;
}

public ResultSet port() throws ClassNotFoundException {
    Statement stmt;
    ResultSet resultset = null;
    Connection con = null;
    try {
        String selectstatement = "CALL P_Select_Salary3 ('2013-01-01', '2013-01-31', 1, 'Salary')";
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/compliance?user=root&password=root");
        stmt = con.createStatement();
        resultset = stmt.executeQuery(selectstatement);

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        close(con);
        close(resultset);
    }
    return resultset;
}

public void generateReport() {
    Connection connection = null;
    Statement stmt;
    ResultSet resultset = null;
    try {
        String selectstatement = "CALL P_Select_Salary ('2013-01-01', '2013-02-28', 1, 'Salary_Summary')";
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/compliance?user=root&password=root");
        stmt = connection.createStatement();
        resultset = stmt.executeQuery(selectstatement);
        JRResultSetDataSource resultsetdatasource = new JRResultSetDataSource(resultset);
        String realpath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("common/reports/wageslip.jasper");
        jasperprint = JasperFillManager.fillReport(realpath, new HashMap(), resultsetdatasource);
        HttpServletResponse httpservlet = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpservlet.addHeader("Content-disposition", "attachment;filename=wageslip.pdf");
        ServletOutputStream servletout = httpservlet.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperprint, servletout);
        FacesContext.getCurrentInstance().responseComplete();
    } catch (net.sf.jasperreports.engine.JRException JRexception) {
        logger.info("JRException Exception" + JRexception.getMessage());
        JsfUtil.addErrorMessage("No Datas between FromDate and ToDate");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(connection);
        close(resultset);
    }
}

推荐答案

您可以在参数映射中发送任意数量的结果集.

You can send as many resultsets as you want in the parameters map.

Map reportParams = new HashMap(); reportParams.put("ds1",新 JRBeanCollectionDataSource(beanCollection1)); reportParams.put("ds2", 新的JRBeanCollectionDataSource(beanCollection2));

Map reportParams = new HashMap(); reportParams.put("ds1", new JRBeanCollectionDataSource(beanCollection1)); reportParams.put("ds2", new JRBeanCollectionDataSource(beanCollection2));

JasperPrint jrprint = JasperFillManager.fillReport(jasperReport,reportParams,新 JREmptyDataSource());

JasperPrint jrprint = JasperFillManager.fillReport(jasperReport,reportParams, new JREmptyDataSource());

确保在报表中声明具有相同名称(ds1,ds2)的参数,并将ParameterClass设置为

Make sure to declare the parameters in the report with the same names (ds1, ds2), and set the ParameterClass as

net.sf.jasperreports.engine.data.JRBeanCollectionDataSource

net.sf.jasperreports.engine.data.JRBeanCollectionDataSource

现在,您可以使用$ P {ds1},$ P {ds2}等来检索它们. 您尚未指定所需的内容,但实际上可以使用参数进行任何操作,例如设置表的数据源等之一.

Now you can retrieve them with $P{ds1},$P{ds2} and so on. You haven't specified what you need them for, but you can do practically anything with the parameters, like set one of them of the datasource of a table etc.

评论后编辑:

我有一个列表组件,将其设置为Connection/Datasource Expression=$P{list1}, 其中$ P {list1}是类型net.sf.jasperreports.engine.JRResultSetDataSource的参数.

I have a list component, to which I set Connection/Datasource Expression=$P{list1}, where $P{list1} is a parameter of type net.sf.jasperreports.engine.JRResultSetDataSource.

我的列表组件将如下所示:

My list component will look like this:

<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="dataset1">
                        <dataSourceExpression><![CDATA[$P{ds1}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="66" width="400">
                        <textField>
                            <reportElement x="10" y="10" width="100" height="20"/>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{empname}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>

如您所见,我有元素 Dataset1是添加列表组件时(如果您使用iReport进行设计的话)在报表中自动添加的数据集. 现在,在dataset1(如我所说的是一个子数据集,因此它允许参数,字段,变量)下,我声明以下字段:

As you can see, I have the element Dataset1 is the dataset added automatically in the report when you added the list component (if you use iReport for the design). Now, under dataset1 (which, as I said, is a subdataset, so it allows parameters, fields, variables), I declare the fields:

<subDataset name="dataset1">
        <field name="empno" class="java.lang.Integer"/>
        <field name="empname" class="java.lang.String"/>
    </subDataset>

就是这样.我已经尝试了这个确切的代码,对我有用.

That's it. I have tried this exact code, works for me.

这篇关于如何使用JasperReports为单个报表传递多个结果集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆