This is a tutorial for Jasper Subreporting with dataresource for beginners. My project is going to be a single page with details of a CV. It will look like below
![]() |
| Figure 1: What CV will look like |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
public static void main(String[] args) {
try {
Object[] cols=
{"Subject", "Result"};
DefaultTableModel dtm=new DefaultTableModel(cols, 0);
dtm.addRow(new Object[]{"Mathematics","B"});
dtm.addRow(new Object[]{"Physics","A"});
dtm.addRow(new Object[]{"Chemistry","B"});
JRTableModelDataSource jrtAL = new JRTableModelDataSource(dtm); //Add DefaultTableModel to the //JRTableModel
Map SUB_DATA1=new HashMap(); //This will hold A/L Data
SUB_DATA1.put("jrtAL", jrtAL);
dtm=new DefaultTableModel(cols, 0);
dtm.addRow(new Object[]{"Mathematics","B"});
dtm.addRow(new Object[]{"Physics","A"});
dtm.addRow(new Object[]{"Chemistry","B"});
JRTableModelDataSource jrtOL = new JRTableModelDataSource(dtm);
Map SUB_DATA2=new HashMap();
SUB_DATA2.put("jrtOL", jrtOL);
List data=new ArrayList();
Map dataMap=new HashMap();
dataMap.put("topic", "Web Developing\nCertificate");
dataMap.put("desc", "Diploma in Web Developing\n(Global Solution Technologies) ");
data.add(dataMap);
dataMap=new HashMap();
dataMap.put("topic",
"Computer Certificates");
dataMap.put("desc", "Diploma in computer Technology\n(Microtech Computer Systems)
");
data.add(dataMap);
JRMapCollectionDataSource subDS=new JRMapCollectionDataSource(data);
Map SUB_DATA3=new HashMap();
SUB_DATA3.put("subDS", subDS);
String repSource = "./src/report/CVReport.jrxml";
Map
param.put("SUB_DATA1", SUB_DATA1);
param.put("SUB_DATA2", SUB_DATA2);
param.put("SUB_DATA3", SUB_DATA3);
///////////////////////////////////////////////////////
Map simpleMasterMap = new HashMap
();
String name="San Buddy CV";
String address="No 1, Somewhere,
Somewhere ";
String resTel="091-1234567";
String phone="071-
6204835";
String email="somebody@gmail.com";
String profile="To excel in the
field of web developing relating to business and academic industry, "
+ "with highly proven leadership skills involving
developing projects, managing projects and ability to"
+ " work with own initiative or and as part of team, is now
seeking a profession in the field of web "
+ "application development and willing to dedicate myself strictly to adhere
the employment ethics and to"
+ " thrive my best for the respective company�s success. ";
simpleMasterMap.put("name", name);
simpleMasterMap.put("address", address);
simpleMasterMap.put("resTel", resTel);
simpleMasterMap.put("phone", phone);
simpleMasterMap.put("email", email);
simpleMasterMap.put("profile", profile);
List
simpleMasterList = new ArrayList();
simpleMasterList.add(simpleMasterMap);
JRMapCollectionDataSource simpleDS = new JRMapCollectionDataSource(simpleMasterList);
JasperReport jr = JasperCompileManager.compileReport(repSource);
JasperPrint jp =
JasperFillManager.fillReport(jr, param, simpleDS);
JasperViewer.viewReport(jp);
}
catch (JRException ex) {
JOptionPane.showMessageDialog(null, "Jasper Error: \n"+ex.getMessage());
}
}
|
With this, jasper report produced a report like this
Here,
- Advanced Level
- Ordinary Level
- Extra Educational Qualifications
were made with subreports. This is how it looked like in IReport 4.5
CVReport.jrxml
CVSubOL.jrxml
CVSub3.jrxml
CVExtra.jrxml
1
2
3
|
<parameter name="SUB_DATA1" class="java.util.HashMap"/>
<parameter name=
"SUB_DATA1" class="java.util.HashMap"/>
<parameter name="SUB_DATA1" class="java.util.HashMap
"/>
|
I have used three detail bands to insert subreports. You can add subreports by drag and dropping a sub report to the editing report or by editing XML. Subreport is also a normal jasper report and only difference it makes is, we use it as a sub report to a main report. Here is the code of the subreport code in the main report.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<band height="85" splitType="Stretch">
<subreport>
<reportElement x="0" y="22" width="555" height="47"/>
<subreportParameter
name="id1">
<subreportParameterExpression><![CDATA[$P{SUB_DATA1}.get("jrtAL")]]
></subreportParameter>
<dataSourceExpression><![CDATA[$P{SUB_DATA1}.get("jrtAL")]]> <subreportExpression> <![CDATA[$P{SUBREPORT_DIR} + "CVSub3.jasper"]]> </subreport> <staticText> <reportElement x="19" y="2" width= "163" height="20"/> <textElement> <font fontName="Calibri" size= "15" isBold="true"/> </textElement> <text><![CDATA[Advanced Level (2011)]]> </staticText> </band> |
I have used SUB_DATA1 jrtAL dataresource for first CVSub3 subreport. As SUB_DATA1 is a HashMap class parameter, you can use get(String) method to get a object.
In CVSub3, I have used two fields "Subject" and "Result" to print the table in detail band. Simply it will do the work of sub reporting.
*You must change the language of the main report to groovy and subreport language to java. Otherwise it will bring out a compile error.
Likewise you can create other two subreports and little change that I have done in SUB_DATA3 wont affect it. There, I have used a JRMapCollectionDataSource "subDS" and you can get details using two fields "desc" and "topic".
Other two subreport declarations are below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<band height="83">
<subreport>
<reportElement
x="0" y="20" width="555" height="48"/>
<subreportParameter name="id2">
<subreportParameterExpression><![CDATA[$P{SUB_DATA2}.get("jrtOL")]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{SUB_DATA2}.get("jrtOL
")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "CVSubOL.jasper
"]]></subreportExpression>
</subreport>
<staticText>
<reportElement x="19" y="0" width="163" height="20"/>
<textElement>
<font fontName="Calibri" size="15" isBold="true"/>
</textElement>
<text><![CDATA[Ordinary Level (2008)]]>
</staticText> </band> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<band height="86">
<subreport>
<reportElement x="0" y="26" width="555" height="60"/>
<subreportParameter name="id2">
<subreportParameterExpression><![CDATA[$P{SUB_DATA3}.get
("subDS")]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{SUB_DATA3}.get("subDS")]]></dataSourceExpression>
<subreportExpression>![CDATA[$P{SUBREPORT_DIR} + "CVExtra.jasper"]]></subreportExpression>
</subreport>
<staticText>
<reportElement x="19" y="0" width=
"276" height="26"/>
<textElement>
<font fontName="Calibri" size=
"15" isBold="true"/>
</textElement>
<text><![CDATA[Extra
Educational Qualifications]]></text>
</staticText>
</band>
|




No comments:
Post a Comment