如何将主报表数据源传递给子报表(JasperReports)?
我正在使用 JasperReports,并为其报告填充 JRDataSource
。 现在,我想将主 REPORT_DATA_SOURCE
传递给子报表。我该怎么做?
据我所知,REPORT_DATA_SOURCE
是一个消耗性对象,所以它只能使用一次,对吧?我可以复制这个数据源并传递吗?
顺便说一句:我使用 iReport 来创建布局。
I'm using JasperReports and I fill the JRDataSource
for ther report.
Now, I want to pass the main REPORT_DATA_SOURCE
to the subreport. How can I do this?
As far as I know the REPORT_DATA_SOURCE
is a consumable object, so it can only be used once, right?. Can I copy this data source and pass it?
BTW: I use iReport for creating the layout.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过内置的
REPORT_DATA_SOURCE
参数传递数据源。示例:
您可以基于变量、参数或字段创建数据源的新实例。
示例:
另一个示例:
或者您可以通过参数传递数据源:
注意:
在子报表中使用相同的(与主报表)数据源可能会导致子报表中丢失第一行 >。您可以阅读为什么我的子报告中缺少第一条记录? 帖子了解如何避免此问题。
You can pass datasource via the built-in
REPORT_DATA_SOURCE
parameter.The example:
You can create new instance of datasource based on variable, parameter or field.
The sample:
Another sample:
Or you can pass the datasource via the parameter:
Note:
Using the same (with master report) datasource in subreport can cause the effect of loosing the first row in the subreport. You can read Why is the first record missing from my subreport? post for understanding how to avoid this issue.
我们假设数据源参数是“dataSourceParam”,数据源值(列表)是“dataSourceList”
在java类中,我们放置:
在主报告模板中,我们放置参数声明:
然后在子报告标记中,我们放置:
we suppose that the datasource parameter is "dataSourceParam" and the datasource value (list) is "dataSourceList"
in java class we put :
in main report template we put in parameters declaration :
then in subreport tag we put :
是的,您需要小心如何传递数据源。使用 SQL 连接,您只需传递一个连接表达式,例如
$P{REPORT_CONNECTION}
。然后子报表有自己的SQL查询。在您的情况下,您想传递实际数据。根据详细信息,它可能就像定义一个参数映射表达式(如
$P{REPORT_PARAMETERS_MAP}
)一样简单。它位于同一窗口的不同选项卡上,您可以在 iReport 中设置子报表连接。通常这足以传递数据源。但您可能需要一些代码来处理事情。请考虑此示例,其中包含在子报表中重复使用的 CSV 数据源。不能只使用 JRParameter.REPORT_DATA_SOURCE 对象的原因是因为索引行指针永远不会重置,因此将该原始对象传递到子报表中将使记录集过早关闭。我们用一个最小的辅助类解决了这个问题:
Yes, you need to be careful about how to pass a data source. With a SQL connection, you can just pass a Connection Expression like
$P{REPORT_CONNECTION}
. Then the subreport has its own SQL query.In your case you want to pass the actual data. Depending on the details, it might be as simple as just defining a Parameter Map Expression like
$P{REPORT_PARAMETERS_MAP}
. It's on a different tab in that same window where you set the subreport connection in iReport. Often this is sufficient to pass the data source.But you might need a little code to handle things. Consider this example with a CSV data source re-used in subreports. The reason why you can't just use the JRParameter.REPORT_DATA_SOURCE object is because the index row pointer is never reset, so passing that original object into the subreport will bring the recordset to its close prematurely. We solved this with a minimal helper class:
“REPORT_DATA_SOURCE”是一个消耗性对象,您可以使用任意多的时间。
我已经测试数据源为 xml 文件数据源,并且不会像 ALEX 所说的那样出现。
“这不会丢失子报告中的第一行。”
我想可能是我使用xpath来选择,所以每次都不会丢失记录。
如果使用 JDBC 数据库作为数据源,请将 sql 作为参数传递给子报表。
如果您使用 ResultSet 作为参数,当您在详细区域中定义子报表时,可能会丢失一条记录。
"REPORT_DATA_SOURCE" is a consumable object, You can used as much time as you want.
I have test the datasource as xml File dataSource, and won't be appeared as ALEX said.
"this will not be loosing the first row in subreport."
I think may be i use the xpath to select, so every time won't be loss records.
If you use JDBC Database as the datasource, pealse pass the sql as parameter to subreport.
If you use ResultSet as the parameter, maybe loss one record as you definition the subreport in detail bands.
这是一个已经回答的老问题,但我可以将潜在的 bean 传递给子报告,避免丢失第一个记录或将所有记录传递给子报告。
该解决方案的优点是子报表可以用作主报表,并且“简单地”将实际记录作为子报表数据源传递(使用groovy作为报表语言):
It's an old question already answered but i get to pass the undelying bean to the subreport, avoiding the loss of the first record or passing all the records to subreport.
This solution has the advantage that the subreport can be used as main report and is "simply" pass the actual record as subreport datasource (using groovy as report lang):
我遇到过这样的情况:子报表中有一个表格。子报表只有标题区和摘要区,摘要中包含表格。我也想对表使用子报表数据源,但无法使接受的答案中的任何一种方法都起作用。因此,这里有一种在 6.6.0 版本中效果很好的替代方法:
在主报告中:
这里的关键是
List
作为参数传递,NOT作为数据源,例如:子报表包括:
请注意,在子报表中,您还需要包含属性
whenNoDataType="AllSectionsNoDetail"
或类似的内容,否则子报表将为空白,因为它没有数据。I had a situation where I had a table in a subreport. The subreport only has a title band and a summary band, with the table in the summary. I wanted to use the subreport datasource for the table as well, but could not get either of the approaches in the accepted answer to work. So here as an alternative approach that is working great in version 6.6.0:
In the main report:
The key here is that the
List
is being passed as a parameter and NOT as a datasource like:The subreport then includes:
Note that in the subreport you also need to include the property
whenNoDataType="AllSectionsNoDetail"
or something like that, otherwise the subreport will be blank because it has no data.只是为了完成 Alex K 的答案,真正让它对我有用的是克隆数据源如下:
Just to complete Alex K's answer, what really made it work for me was to clone the data source as follows: