最近,在用作報表,其中有一部分報表中須要合併行,其實用分組解決起來沒什麼難度.可是客戶給的Excel就是那樣的,沒辦法.百度.google,搜了一天,確實搜到了.有人已經實現了.還提供了代碼.謝謝這位
heis朋友了.下面是我實現一個小例子的效果圖.數據是從數據庫取出的動態數據.
左邊是主報表,右邊的是子報表
![](http://static.javashuo.com/static/loading.gif)
java
因爲這測試時在一個商業的項目中,因此只把主要的代碼貼出來.
我測試用的後臺方法是一個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.javashuo.com/static/loading.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));
主報表中設置子報表的代碼:
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了.