主报表中的多个子报表使用相同的数据源 [英] Multiple Subreports in Main Report using same datasource

查看:183
本文介绍了主报表中的多个子报表使用相同的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份包含2个子报告的主报告。我正在使用自定义数据源来获取报告内容。但是,在jasper studio中预览主报表时,只显示一个子报表(无论哪个子报表首先出现)。

I have a main report with 2 subreports. I am using a custom datasource to fetch the report contents. But only one subreport gets displayed when the main report is previewed in jasper studio (whichever subreport comes first).

例如。只显示report1.jrxml。如果删除该子报告,则显示report2.jrxml。

For eg. only report1.jrxml gets displayed.If I remove that subreport then report2.jrxml gets displayed.

main.jrxml

main.jrxml

<detail>
    <band height="250">
        <subreport runToBottom="true">
            <reportElement positionType="Float" x="0" y="130" width="1960" height="120" uuid="89a9f872-756e-4c82-922d-537cfde30cca"/>
            <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
            <subreportExpression><![CDATA["report1.jrxml"]]></subreportExpression>
        </subreport>
    </band>
    <band height="250">
        <subreport runToBottom="true">
            <reportElement positionType="Float" x="0" y="90" width="1960" height="120" uuid="892c0849-9532-48cb-94c0-f2e87528232a"/>
            <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
            <subreportExpression><![CDATA["report2.jrxml"]]></subreportExpression>
        </subreport>
    </band>
</detail>

我试过以下内容:


  1. 将子报表放在不同的详细信息区域中。

  2. 将位置类型设置为浮动。

  3. 设置 Run To Bottomproperty toTrue。


推荐答案

问题在于尝试使用多个子报表的 相同数据源 。第一个子报告的数据源已耗尽,因此后续子报告没有可用的数据。

The Problem was with trying to use the same datasource for multiple subreports. The datasource gets exhausted for the first subreport and so no data is available for the subsequent sub reports.

解决方案:

您必须使用 JRRewindableDataSource

感谢lucianc 社区答案

Thanks to lucianc Community answer

总结任务:

创建一个包装RewindableDSWrapper,它会倒回数据源并委托对它的所有调用。

Create a wrapper RewindableDSWrapper that rewinds a data source and delegates all the calls to it.

package com.jasper.api;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRRewindableDataSource;

public class RewindableDSWrapper implements JRRewindableDataSource {

  private final JRRewindableDataSource ds;

  public RewindableDSWrapper(JRRewindableDataSource ds) {

    this.ds = ds;

    try {
        this.ds.moveFirst();
    } catch (JRException e) {
        e.printStackTrace();
    }

  }

  public boolean next() throws JRException {

    return ds.next();

  }

  public Object getFieldValue(JRField jrField) throws JRException {

    return ds.getFieldValue(jrField);

  }

  public void moveFirst() throws JRException {

    ds.moveFirst();

   }

 }

在您的自定义中数据源类实现JRRewindableDataSource接口。

In your custom data source class implement JRRewindableDataSource interface.

public void moveFirst() throws JRException {
    // provide logic for rewinding datasource
}

在你的jrxml文件中,如果你的数据源是自定义的,那么

In your jrxml file, if your datasource is custom one then

<dataSourceExpression><![CDATA[new com.jasper.api.RewindableDSWrapper((JRRewindableDataSource)$P{REPORT_DATA_SOURCE})]]></dataSourceExpression>

您将REPORT_DATA_SOURCE转换为JRRewindableDataSource,因为编译器会尝试将其强制转换为JRDataSource。

you cast REPORT_DATA_SOURCE to JRRewindableDataSource as the compiler will try to cast it to JRDataSource.

还要将包含RewindableDSWrapper类的jar文件添加到工作室的classpath中。

Also add the jar file containing RewindableDSWrapper class to classpath in your studio.

这篇关于主报表中的多个子报表使用相同的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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