在iReport中的列表中创建子报表 [英] Creating Subreport within list in iReport
问题描述
我有一个目标列表,每个目标中都有另一个arraylist。我希望使用子报表显示子arraylist中的详细信息。我想为子arraylist中的每个对象都有一个子报告。
I have a list of goals, each of which have another arraylist in it. I want to have the details in the child arraylist to be displayed using a subreport. I would like to have a subreport for each of the objects in the child arraylist.
我面临的问题是,我似乎找不到办法将arraylist指定为子报表的数据源。在尝试创建数据源时,我无法选择构建列表的数据集的字段,只能选择主报表的字段。
The issue I am facing is that, I can't seem to find a way to specify the arraylist as data source for the subreport. When trying to create datasource, I can't select fields of the dataset on which the list is built, only the fields of the main report can be selected.
是否可以在iReport中执行此操作。我已经坚持了很长一段时间了。任何帮助表示赞赏。 :)
Is it possible to do this in iReport.? I am stuck with this for quite some time now. Any help is appreciated. :)
推荐答案
根据给出的信息,这将是答案。
With information given this would be the answer.
主bean (你的目标?),包含其他bean的 List
( SubBean
) 。
Main bean (your goals?), containing List
of other bean (SubBean
).
public class Bean {
private String var1;
private List<SubBean> subBeans;
public String getVar1() {
return var1;
}
public void setVar1(String var1) {
this.var1 = var1;
}
public List<SubBean> getSubBeans() {
return subBeans;
}
public void setSubBeans(List<SubBean> subBean) {
this.subBeans = subBean;
}
}
子bean
public class SubBean {
private String var2;
public SubBean(String var2){
this.var2 = var2;
}
public String getVar2() {
return var2;
}
public void setVar2(String var2) {
this.var2 = var2;
}
}
如何将SubBean列表传递给a子报告。
-
您需要主报告中的字段subBeans。
You need the field subBeans in you main report.
<field name="subBeans" class="java.util.List"/>
我建议您将.jasper文件的位置作为参数传递(注意jasper报告需要绝对路径)。在主报告中
I suggest that you pass the location of the .jasper file as a parameter (Note jasper report needs absolute path) es. in main report
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false"/>
并在java中传递它(在示例sub中)工作目录中的文件夹jasper是subreport.jasper的位置。
and in java pass it (in example sub folder "jasper" in working directory is the location of the subreport.jasper)
paramMap.put("SUBREPORT_DIR", new File("jasper").getAbsolutePath() + File.separator);
-
现在只需打电话给你的子报告(需要从这样的主报告中加入.jasper。
Now just call your subreport (needs to be complied into .jasper) from main report like this.
<subreport>
<reportElement x="105" y="4" width="400" height="100"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{subBeans})]]></dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "subreport.jasper"]]></subreportExpression>
</subreport>
因此:
我正在创建一个新的 JRDataSource 子报告在当前 Bean <中传递
List
SubBean
/ code>
I'm creating a new JRDataSource for the sub report passing the List
of SubBean
in the current Bean
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{subBeans})
表示已编译的subreport.jasper的绝对位置
indicating the absolute location of the compiled subreport.jasper
$P{SUBREPORT_DIR} + "subreport.jasper"
所以现在你的子报告中你可以使用 SubBean
的字段: var2
,只需在此定义如下: subreport.jrxml
so now in your subreport you can use the field: var2
of the SubBean
, just define it like this in the subreport.jrxml
<field name="var2" class="java.lang.String"/>
这篇关于在iReport中的列表中创建子报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!