![](http://static.oschina.net/uploads/img/201501/21145433_D56d.jpg)
左边是主报表,右边的是子报表
我测试用的后台方法是一个Servlet,导出的是pdf.
1
//
定义子报表的数据
2
Map parameters
=
new
HashMap();
3
4
parameters.put(
"
ReportTitle
"
,
"
Address Report
"
);
5
parameters.put(
"
BaseDir
"
, reportFile.getParentFile());
6
parameters.put(
"
IsBgView
"
,
true
);
7![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
8
//
定义主报表的数据
9
List
<
Map
>
datas
=
new
ArrayList();
10
11
Map temp
=
null
;
12
List
<
Map
>
devices
=
null
;
13
//
这里去遍历rtu这个对象
14
for
(WrRRtu rtu : wrtest.getAll())
{
15
devices=new ArrayList();
16
temp = new HashMap();
17
temp.put("recid", rtu.getRecid());
18
temp.put("rtumc", rtu.getRtumc());
19
temp.put("rtudm", rtu.getRtudm());
20
//这里给主报表添加数据
21
datas.add(temp);
22
//通过rtu去查它的子数据
23
for(WrRDevice dev :wrdtest.getWrRDeviceByRtuRecid(rtu.getRecid())){
24
temp = new HashMap();
25
devices.add(temp);
26
//当前rtu所有的子数据
27
temp.put("recid", dev.getRecid());
28
}
29
//把当前rtu的子数据放在parameters里
30
parameters.put(rtu.getRecid(), new JRListDataSource(devices));
31
}
32
//
我这是到处PDF的
33
byte
[] bytes
=
JasperRunManager.runReportToPdf(reportFile.getPath(), parameters,
new
JRListDataSource(datas));
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
2
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
3
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
4
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
5
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
6
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
7
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
8
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
9
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
10
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
11
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
12
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
13
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
14
![](http://static.oschina.net/uploads/img/201501/21145434_6Fy1.gif)
15
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
16
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
17
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
18
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
19
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
20
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
21
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
22
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
23
![](http://static.oschina.net/uploads/img/201501/21145434_Vi7Z.gif)
24
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
25
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
26
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
27
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
28
![](http://static.oschina.net/uploads/img/201501/21145434_pmgk.gif)
29
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
30
![](http://static.oschina.net/uploads/img/201501/21145434_kYyI.gif)
31
![](http://static.oschina.net/uploads/img/201501/21145434_MZqS.gif)
32
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
33
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
主报表中设置子报表的代码:
1
<
subreport isUsingCache
=
"
true
"
>
2
<
reportElement key
=
"
subreport-1
"
x
=
"
181
"
y
=
"
0
"
width
=
"
285
"
height
=
"
22
"
/>
3
<
dataSourceExpression
><!
[CDATA[$P
{REPORT_PARAMETERS_MAP}
.get($F
{recid}
)]]
></
dataSourceExpression
>
4
<
subreportExpression
class
=
"
java.lang.String
"
><!
[CDATA[$P
{SUBREPORT_DIR}
+
"
DepartmentList_department_person.jasper
"
]]
></
subreportExpression
>
5
</
subreport
>
子报表中,仍旧按传统的方法取就OK了.
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
2
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)
3
![](http://static.oschina.net/uploads/img/201501/21145434_6Fy1.gif)
4
![](http://static.oschina.net/uploads/img/201501/21145434_6Fy1.gif)
5
![](http://static.oschina.net/uploads/img/201501/21145434_eeZG.gif)