Jasper Report (3)--- 用JavaBean Collection做为数据源

1. 新建Java项目结构如下图所示(该项目中有子报表的知识这里不记录这点知识点):

2. 项目中代码片段

  1.实现JRRewindableDataSource接口里面的方法

 1 package com.iaspec.ireport.common;
 2
 3 import java.util.List;
 4
 5 import net.sf.jasperreports.engine.JRException;
 6 import net.sf.jasperreports.engine.JRField;
 7 import net.sf.jasperreports.engine.JRRewindableDataSource;
 8
 9 public class GenericDataSource implements JRRewindableDataSource{
10     int index;
11     List<PrintRecord> records;
12
13     public GenericDataSource(List<PrintRecord> records) {
14         this.records = records;
15         this.index = -1;
16     }
17
18     @Override
19     public Object getFieldValue(JRField jrField) throws JRException {
20         String fieldName = jrField.getName();
21         return records.get(index).getValue(fieldName);
22     }
23
24     @Override
25     public boolean next() throws JRException {
26         ++index;
27         return index < records.size();
28     }
29
30     public void moveFirst() {
31     }
32 }

  2.获取资源文件所在路径:

 1 package com.iaspec.ireport.common;
 2
 3 import java.io.IOException;
 4
 5 public class GetPath {
 6
 7      public String showURL() throws IOException {
 8          return this.getClass().getResource("/").getPath();
 9      }
10 }

  3.设定公用类:

 1 package com.iaspec.ireport.common;
 2
 3 import java.util.HashMap;
 4 import java.util.Map;
 5
 6 public class PrintRecord {
 7     public static final int KEY_NOT_FOUND = -1;
 8
 9     Map<String, Object> vals;
10
11     public PrintRecord() {
12         vals = new HashMap<String, Object>();
13     }
14
15     public void setValue(String key, Object value) {
16         vals.put(key, value);
17     }
18
19     public Object getValue(String key) {
20         Object val = vals.get(key);
21         if (val == null)
22             return "";
23         else
24             return val;
25     }
26
27     public int getKeyNum() {
28         return vals.size();
29     }
30 }

  4. 封装报表所需的参数

 1 package com.iaspec.ireport.core;
 2
 3 import java.math.BigDecimal;
 4 import java.util.HashMap;
 5 import java.util.LinkedList;
 6 import java.util.List;
 7 import java.util.Map;
 8
 9 import com.iaspec.ireport.common.GenericDataSource;
10 import com.iaspec.ireport.common.PrintRecord;
11 import com.iaspec.ireport.datasource.GenerateBeans;
12 import com.iaspec.ireport.dto.DailyReportSumDayDTO;
13
14 public class GenerateReportParams {
15     public static Map<String, Object> generateSumDayReport(String path) {
16         HashMap<String, Object> params = new HashMap<String, Object>();
17
18         params.put("fromDate", "2015/06/08");
19         params.put("toDate", "2015/06/09");
20         params.put("presentmentDate", "2015/06/09");
21         params.put("clearingDate", "2015/06/09");
22
23         List<DailyReportSumDayDTO> beanList1 = GenerateBeans.getDailyReportSumDay();
24         List<PrintRecord> prs1 = new LinkedList<PrintRecord>();
25
26         BigDecimal totalPresented1 = BigDecimal.ZERO;
27         BigDecimal totalPending1 = BigDecimal.ZERO;
28         BigDecimal totalAccepted1 = BigDecimal.ZERO;
29         BigDecimal totalRejected1 = BigDecimal.ZERO;
30         BigDecimal totalError1 = BigDecimal.ZERO;
31
32         for(DailyReportSumDayDTO dto : beanList1){
33             PrintRecord pr = new PrintRecord();
34             pr.setValue("channel", dto.getChannel());
35             pr.setValue("customerGroup", dto.getCustomerGroup());
36             pr.setValue("presented", dto.getPresented());
37             pr.setValue("pending", dto.getPending());
38             pr.setValue("accepted", dto.getAccepted());
39             pr.setValue("rejected", dto.getRejected());
40             pr.setValue("error", dto.getError());
41
42             totalPresented1 = dto.getPresented().add(totalPresented1);
43             totalPending1 = dto.getPending().add(totalPending1);
44             totalAccepted1 = dto.getAccepted().add(totalAccepted1);
45             totalRejected1 = dto.getRejected().add(totalRejected1);
46             totalError1 = dto.getError().add(totalError1);
47
48             prs1.add(pr);
49         }
50
51         params.put("totalPresented1", totalPresented1);
52         params.put("totalPending1", totalPending1);
53         params.put("totalAccepted1", totalAccepted1);
54         params.put("totalRejected1", totalRejected1);
55         params.put("totalError1", totalError1);
56         params.put("DataSource1", new GenericDataSource(prs1));
57
58         List<DailyReportSumDayDTO> beanList2 = GenerateBeans.getProductTypeSumDay();
59         List<PrintRecord> prs2 = new LinkedList<PrintRecord>();
60
61         BigDecimal totalPresented2 = BigDecimal.ZERO;
62         BigDecimal totalPending2 = BigDecimal.ZERO;
63         BigDecimal totalAccepted2 = BigDecimal.ZERO;
64         BigDecimal totalRejected2 = BigDecimal.ZERO;
65         BigDecimal totalError2 = BigDecimal.ZERO;
66
67         for(DailyReportSumDayDTO dto : beanList2){
68             PrintRecord pr = new PrintRecord();
69             pr.setValue("productType", dto.getProductType());
70             pr.setValue("presented", dto.getPresented());
71             pr.setValue("pending", dto.getPending());
72             pr.setValue("accepted", dto.getAccepted());
73             pr.setValue("rejected", dto.getRejected());
74             pr.setValue("error", dto.getError());
75
76             totalPresented2 = dto.getPresented().add(totalPresented2);
77             totalPending2 = dto.getPending().add(totalPending2);
78             totalAccepted2 = dto.getAccepted().add(totalAccepted2);
79             totalRejected2 = dto.getRejected().add(totalRejected2);
80             totalError2 = dto.getError().add(totalError2);
81
82             prs2.add(pr);
83         }
84
85         params.put("totalPresented2", totalPresented2);
86         params.put("totalPending2", totalPending2);
87         params.put("totalAccepted2", totalAccepted2);
88         params.put("totalRejected2", totalRejected2);
89         params.put("totalError2", totalError2);
90         params.put("DataSource2", new GenericDataSource(prs2));
91         params.put("SUBREPORT_DIR", path);
92
93         return params;
94     }
95 }

  5.创建Bean

  1 package com.iaspec.ireport.dto;
  2
  3 import java.io.Serializable;
  4 import java.math.BigDecimal;
  5
  6 public class DailyReportSumDayDTO implements Serializable {
  7
  8     private static final long serialVersionUID = 3117545431846603555L;
  9
 10     private String channel;
 11     private String customerGroup;
 12     private BigDecimal presented;
 13     private BigDecimal pending;
 14     private BigDecimal accepted;
 15     private BigDecimal rejected;
 16     private BigDecimal error;
 17     private String productType;
 18
 19     public DailyReportSumDayDTO() {
 20         super();
 21     }
 22
 23     public DailyReportSumDayDTO(String channel, String customerGroup,
 24             BigDecimal presented, BigDecimal pending, BigDecimal accepted,
 25             BigDecimal rejected, BigDecimal error, String productType) {
 26         super();
 27         this.channel = channel;
 28         this.customerGroup = customerGroup;
 29         this.presented = presented;
 30         this.pending = pending;
 31         this.accepted = accepted;
 32         this.rejected = rejected;
 33         this.error = error;
 34         this.productType = productType;
 35     }
 36
 37     public String getChannel() {
 38         return channel;
 39     }
 40
 41     public void setChannel(String channel) {
 42         this.channel = channel;
 43     }
 44
 45     public String getCustomerGroup() {
 46         return customerGroup;
 47     }
 48
 49     public void setCustomerGroup(String customerGroup) {
 50         this.customerGroup = customerGroup;
 51     }
 52
 53     public BigDecimal getPresented() {
 54         return presented;
 55     }
 56
 57     public void setPresented(BigDecimal presented) {
 58         this.presented = presented;
 59     }
 60
 61     public BigDecimal getPending() {
 62         return pending;
 63     }
 64
 65     public void setPending(BigDecimal pending) {
 66         this.pending = pending;
 67     }
 68
 69     public BigDecimal getAccepted() {
 70         return accepted;
 71     }
 72
 73     public void setAccepted(BigDecimal accepted) {
 74         this.accepted = accepted;
 75     }
 76
 77     public BigDecimal getRejected() {
 78         return rejected;
 79     }
 80
 81     public void setRejected(BigDecimal rejected) {
 82         this.rejected = rejected;
 83     }
 84
 85     public BigDecimal getError() {
 86         return error;
 87     }
 88
 89     public void setError(BigDecimal error) {
 90         this.error = error;
 91     }
 92
 93     public String getProductType() {
 94         return productType;
 95     }
 96
 97     public void setProductType(String productType) {
 98         this.productType = productType;
 99     }
100 }

  6. 封装Bean

 1 package com.iaspec.ireport.datasource;
 2
 3 import java.math.BigDecimal;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6
 7 import com.iaspec.ireport.dto.DailyReportSumDayDTO;
 8
 9 public class GenerateBeans {
10     public static List<DailyReportSumDayDTO> getDailyReportSumDay(){
11
12         List<DailyReportSumDayDTO> list = new ArrayList<DailyReportSumDayDTO>();
13
14         list.add(new DailyReportSumDayDTO("iBanking", "Personal", BigDecimal.valueOf(8888888.99), BigDecimal.valueOf(3333333.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
15         list.add(new DailyReportSumDayDTO("iBanking", "Business Banking", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
16         list.add(new DailyReportSumDayDTO("iBanking", "Corporate", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
17         list.add(new DailyReportSumDayDTO("FCDB", "Personal", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
18         list.add(new DailyReportSumDayDTO("FCDB", "Business Banking", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
19         list.add(new DailyReportSumDayDTO("FCDB", "Corporate", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
20         list.add(new DailyReportSumDayDTO("Corporate website", "Personal", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(6666666.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
21         list.add(new DailyReportSumDayDTO("Corporate website", "Business Banking", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
22         list.add(new DailyReportSumDayDTO("Corporate website", "Corporate", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
23         list.add(new DailyReportSumDayDTO("HKICL portal", "Personal", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
24         list.add(new DailyReportSumDayDTO("HKICL portal", "Business Banking", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
25         list.add(new DailyReportSumDayDTO("HKICL portal", "Corporate", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), ""));
26
27         return list;
28     }
29
30     public static List<DailyReportSumDayDTO> getProductTypeSumDay(){
31
32         List<DailyReportSumDayDTO> list = new ArrayList<DailyReportSumDayDTO>();
33
34         list.add(new DailyReportSumDayDTO("", "", BigDecimal.valueOf(6666666.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), "CASA"));
35         list.add(new DailyReportSumDayDTO("", "", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), "Credit Card"));
36         list.add(new DailyReportSumDayDTO("", "", BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), "Loans"));
37         list.add(new DailyReportSumDayDTO("", "", BigDecimal.valueOf(5555555.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99), BigDecimal.valueOf(9999999.99),BigDecimal.valueOf(9999999.99), "Insurance"));
38
39         return list;
40     }
41 }

  7.产生报表

 1 package com.iaspec.ireport.test;
 2
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.util.Map;
 7
 8 import com.iaspec.ireport.common.GetPath;
 9 import com.iaspec.ireport.core.GenerateReportParams;
10
11 import net.sf.jasperreports.engine.JREmptyDataSource;
12 import net.sf.jasperreports.engine.JRException;
13 import net.sf.jasperreports.engine.JasperRunManager;
14
15 public class GenerateReport {
16     public static void main(String[] args) throws IOException{
17         GeneratePDFReport(new GetPath().showURL(), GenerateReportParams.generateSumDayReport(new GetPath().showURL()));
18     }
19
20     private static void GeneratePDFReport(String path, Map<String, Object> sourceData){
21         try {
22             byte[] pdfStream = JasperRunManager.runReportToPdf(path + "/main.jasper", sourceData, new JREmptyDataSource());
23             File file = new File(path + "/report.pdf");
24             FileOutputStream op = new FileOutputStream(file);
25             op.write(pdfStream);
26             op.flush();
27             op.close();
28         } catch (IOException e) {
29             e.printStackTrace();
30         } catch (JRException e) {
31             e.printStackTrace();
32         }
33     }
34 }

  8. 主报表

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!-- Created with Jaspersoft Studio version 6.0.4.final using JasperReports Library version 6.0.4  -->
  3 <!-- 2015-08-21T14:03:07 -->
  4 <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="main" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="aec5b519-b625-41f6-a025-4d38559e41b0">
  5     <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
  6     <parameter name="fromDate" class="java.lang.String"/>
  7     <parameter name="toDate" class="java.lang.String"/>
  8     <parameter name="presentmentDate" class="java.lang.String"/>
  9     <parameter name="clearingDate" class="java.lang.String"/>
 10     <parameter name="totalPresented1" class="java.math.BigDecimal"/>
 11     <parameter name="totalPending1" class="java.math.BigDecimal"/>
 12     <parameter name="totalAccepted1" class="java.math.BigDecimal"/>
 13     <parameter name="totalRejected1" class="java.math.BigDecimal"/>
 14     <parameter name="totalError1" class="java.math.BigDecimal"/>
 15     <parameter name="DataSource1" class="net.sf.jasperreports.engine.JRDataSource"/>
 16     <parameter name="DataSource2" class="net.sf.jasperreports.engine.JRDataSource"/>
 17     <parameter name="totalPresented2" class="java.math.BigDecimal"/>
 18     <parameter name="totalPending2" class="java.math.BigDecimal"/>
 19     <parameter name="totalAccepted2" class="java.math.BigDecimal"/>
 20     <parameter name="totalRejected2" class="java.math.BigDecimal"/>
 21     <parameter name="totalError2" class="java.math.BigDecimal"/>
 22     <parameter name="SUBREPORT_DIR" class="java.lang.String"/>
 23     <group name="Group1">
 24         <groupExpression><![CDATA[$V{PAGE_NUMBER}]]></groupExpression>
 25         <groupFooter>
 26             <band height="26">
 27                 <subreport>
 28                     <reportElement x="0" y="4" width="802" height="20" uuid="886efada-a73b-4085-82e6-fd9bb9de79ee"/>
 29                     <subreportParameter name="SUBREPORT_DIR">
 30                         <subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportParameterExpression>
 31                     </subreportParameter>
 32                     <subreportParameter name="totalPresented2">
 33                         <subreportParameterExpression><![CDATA[$P{totalPresented2}]]></subreportParameterExpression>
 34                     </subreportParameter>
 35                     <subreportParameter name="totalPending2">
 36                         <subreportParameterExpression><![CDATA[$P{totalPending2}]]></subreportParameterExpression>
 37                     </subreportParameter>
 38                     <subreportParameter name="totalAccepted2">
 39                         <subreportParameterExpression><![CDATA[$P{totalAccepted2}]]></subreportParameterExpression>
 40                     </subreportParameter>
 41                     <subreportParameter name="totalRejected2">
 42                         <subreportParameterExpression><![CDATA[$P{totalRejected2}]]></subreportParameterExpression>
 43                     </subreportParameter>
 44                     <subreportParameter name="totalError2">
 45                         <subreportParameterExpression><![CDATA[$P{totalError2}]]></subreportParameterExpression>
 46                     </subreportParameter>
 47                     <dataSourceExpression><![CDATA[$P{DataSource2}]]></dataSourceExpression>
 48                     <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub2.jasper"]]></subreportExpression>
 49                 </subreport>
 50             </band>
 51         </groupFooter>
 52     </group>
 53     <background>
 54         <band splitType="Stretch"/>
 55     </background>
 56     <title>
 57         <band height="60" splitType="Stretch">
 58             <staticText>
 59                 <reportElement x="0" y="0" width="40" height="20" uuid="5b9f0876-dad0-41e7-964f-95e17721cdb8"/>
 60                 <text><![CDATA[Period :]]></text>
 61             </staticText>
 62             <staticText>
 63                 <reportElement x="0" y="20" width="90" height="20" uuid="de730f40-cfcd-4cc9-a921-c15681b48d96"/>
 64                 <text><![CDATA[Presentment date :]]></text>
 65             </staticText>
 66             <staticText>
 67                 <reportElement x="0" y="40" width="70" height="20" uuid="dd8bd899-66cb-473f-9c7e-662e90091eb8"/>
 68                 <text><![CDATA[Clearing date :]]></text>
 69             </staticText>
 70             <staticText>
 71                 <reportElement x="261" y="0" width="280" height="20" uuid="31a859b9-73f6-4330-8980-2a372bd5d755"/>
 72                 <textElement textAlignment="Center">
 73                     <font fontName="Arial Black" isBold="true"/>
 74                 </textElement>
 75                 <text><![CDATA[China Citic Bank International]]></text>
 76             </staticText>
 77             <staticText>
 78                 <reportElement x="236" y="20" width="330" height="20" uuid="18aaca0c-0688-43f1-98e0-ac3dccadcd9c"/>
 79                 <textElement textAlignment="Center"/>
 80                 <text><![CDATA[e-Cheque Daily Activity Summary Report (Day D)]]></text>
 81             </staticText>
 82             <staticText>
 83                 <reportElement x="658" y="0" width="52" height="20" uuid="bada66f8-be8d-408b-becd-dd92d197b8db"/>
 84                 <text><![CDATA[Print Date : ]]></text>
 85             </staticText>
 86             <staticText>
 87                 <reportElement x="658" y="20" width="52" height="20" uuid="44c04286-222f-46ae-aac3-1b5796c79e0f"/>
 88                 <text><![CDATA[Print Time :]]></text>
 89             </staticText>
 90             <textField pattern="yyyy/MM/dd">
 91                 <reportElement x="710" y="0" width="92" height="20" uuid="e0e8ea12-b6d2-41f4-af36-665b3c5d8e84"/>
 92                 <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
 93             </textField>
 94             <textField pattern="HH:mm:ss">
 95                 <reportElement x="710" y="20" width="92" height="20" uuid="a0b2c790-5575-4187-9177-a3cea6256ccb"/>
 96                 <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
 97             </textField>
 98             <textField>
 99                 <reportElement x="40" y="0" width="58" height="20" uuid="c57b5888-d7b7-42a0-babf-f5a2ce81f65b"/>
100                 <textFieldExpression><![CDATA[$P{fromDate}]]></textFieldExpression>
101             </textField>
102             <staticText>
103                 <reportElement x="99" y="0" width="10" height="20" uuid="c382e621-5fbe-4e30-8aa1-fb2dbc812e61"/>
104                 <text><![CDATA[to]]></text>
105             </staticText>
106             <textField>
107                 <reportElement x="116" y="0" width="70" height="20" uuid="c7cdc348-898d-4d25-83db-22bbc5fa8784"/>
108                 <textFieldExpression><![CDATA[$P{toDate}]]></textFieldExpression>
109             </textField>
110             <textField>
111                 <reportElement x="90" y="20" width="60" height="20" uuid="965fe446-7d45-4767-8c24-2ccc8cfcd1f2"/>
112                 <textFieldExpression><![CDATA[$P{presentmentDate}]]></textFieldExpression>
113             </textField>
114             <textField>
115                 <reportElement x="70" y="40" width="70" height="20" uuid="7898321a-16f7-4efb-9c98-74d7309ccf14"/>
116                 <textFieldExpression><![CDATA[$P{clearingDate}]]></textFieldExpression>
117             </textField>
118         </band>
119     </title>
120     <pageHeader>
121         <band height="7" splitType="Stretch"/>
122     </pageHeader>
123     <columnHeader>
124         <band height="4" splitType="Stretch"/>
125     </columnHeader>
126     <detail>
127         <band height="22" splitType="Stretch">
128             <subreport>
129                 <reportElement x="0" y="0" width="802" height="22" uuid="be070050-2281-4b30-bc69-662a6974868e"/>
130                 <subreportParameter name="SUBREPORT_DIR">
131                     <subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportParameterExpression>
132                 </subreportParameter>
133                 <subreportParameter name="totalPresented1">
134                     <subreportParameterExpression><![CDATA[$P{totalPresented1}]]></subreportParameterExpression>
135                 </subreportParameter>
136                 <subreportParameter name="totalPending1">
137                     <subreportParameterExpression><![CDATA[$P{totalPending1}]]></subreportParameterExpression>
138                 </subreportParameter>
139                 <subreportParameter name="totalAccepted1">
140                     <subreportParameterExpression><![CDATA[$P{totalAccepted1}]]></subreportParameterExpression>
141                 </subreportParameter>
142                 <subreportParameter name="totalRejected1">
143                     <subreportParameterExpression><![CDATA[$P{totalRejected1}]]></subreportParameterExpression>
144                 </subreportParameter>
145                 <subreportParameter name="totalError1">
146                     <subreportParameterExpression><![CDATA[$P{totalError1}]]></subreportParameterExpression>
147                 </subreportParameter>
148                 <dataSourceExpression><![CDATA[$P{DataSource1}]]></dataSourceExpression>
149                 <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub1.jasper"]]></subreportExpression>
150             </subreport>
151         </band>
152     </detail>
153     <columnFooter>
154         <band height="2" splitType="Stretch"/>
155     </columnFooter>
156     <pageFooter>
157         <band height="2" splitType="Stretch"/>
158     </pageFooter>
159     <summary>
160         <band height="23" splitType="Stretch">
161             <staticText>
162                 <reportElement x="322" y="0" width="158" height="23" uuid="2b3252f4-b55f-451c-9c7f-897b364aef9a"/>
163                 <textElement textAlignment="Center"/>
164                 <text><![CDATA[*** END OF REPORT ***]]></text>
165             </staticText>
166         </band>
167     </summary>
168 </jasperReport>

  子报表1:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!-- Created with Jaspersoft Studio version 6.0.4.final using JasperReports Library version 6.0.4  -->
  3 <!-- 2015-08-21T14:03:19 -->
  4 <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="sub2" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d1debb0f-5c0a-48aa-a93a-187757ae91dd">
  5     <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
  6     <parameter name="totalPresented1" class="java.math.BigDecimal"/>
  7     <parameter name="totalPending1" class="java.math.BigDecimal"/>
  8     <parameter name="totalAccepted1" class="java.math.BigDecimal"/>
  9     <parameter name="totalRejected1" class="java.math.BigDecimal"/>
 10     <parameter name="totalError1" class="java.math.BigDecimal"/>
 11     <field name="channel" class="java.lang.String"/>
 12     <field name="customerGroup" class="java.lang.String"/>
 13     <field name="presented" class="java.math.BigDecimal"/>
 14     <field name="pending" class="java.math.BigDecimal"/>
 15     <field name="accepted" class="java.math.BigDecimal"/>
 16     <field name="rejected" class="java.math.BigDecimal"/>
 17     <field name="error" class="java.math.BigDecimal"/>
 18     <background>
 19         <band splitType="Stretch"/>
 20     </background>
 21     <title>
 22         <band height="20" splitType="Stretch">
 23             <staticText>
 24                 <reportElement x="0" y="0" width="140" height="20" uuid="1690f038-e5f1-4132-b706-c911aae641b7"/>
 25                 <text><![CDATA[Channel]]></text>
 26             </staticText>
 27             <staticText>
 28                 <reportElement x="140" y="0" width="140" height="20" uuid="519c4dda-1400-4f30-8a4e-b65cd1160ae6"/>
 29                 <text><![CDATA[Customer Group]]></text>
 30             </staticText>
 31             <staticText>
 32                 <reportElement x="280" y="0" width="100" height="20" uuid="cba100cf-b80b-4022-aac8-198b2075eed3"/>
 33                 <text><![CDATA[Presented]]></text>
 34             </staticText>
 35             <staticText>
 36                 <reportElement x="380" y="0" width="100" height="20" uuid="a7de3d30-b00c-42f7-979b-54b74ff44826"/>
 37                 <text><![CDATA[Pending]]></text>
 38             </staticText>
 39             <staticText>
 40                 <reportElement x="480" y="0" width="100" height="20" uuid="3b864709-94aa-4c63-8652-f96aa933d76e"/>
 41                 <text><![CDATA[Accepted]]></text>
 42             </staticText>
 43             <staticText>
 44                 <reportElement x="580" y="0" width="100" height="20" uuid="9362560f-6a19-48fc-875c-26a2fd9b98f8"/>
 45                 <text><![CDATA[Rejected]]></text>
 46             </staticText>
 47             <staticText>
 48                 <reportElement x="680" y="0" width="100" height="20" uuid="e2666412-78ef-435e-b8c6-72d44508249b"/>
 49                 <text><![CDATA[Error]]></text>
 50             </staticText>
 51         </band>
 52     </title>
 53     <columnHeader>
 54         <band height="2" splitType="Stretch"/>
 55     </columnHeader>
 56     <detail>
 57         <band height="20" splitType="Stretch">
 58             <textField>
 59                 <reportElement x="0" y="0" width="140" height="20" uuid="85584127-02e5-4da2-b47b-350c8b14e9a4"/>
 60                 <textFieldExpression><![CDATA[$F{channel}]]></textFieldExpression>
 61             </textField>
 62             <textField>
 63                 <reportElement x="140" y="0" width="140" height="20" uuid="5b31982b-0592-4673-a35e-34a7b6a07019"/>
 64                 <textFieldExpression><![CDATA[$F{customerGroup}]]></textFieldExpression>
 65             </textField>
 66             <textField>
 67                 <reportElement x="280" y="0" width="100" height="20" uuid="9f9c54db-08ce-4ffc-b25d-736eda2fc8c8"/>
 68                 <textFieldExpression><![CDATA[$F{presented}]]></textFieldExpression>
 69             </textField>
 70             <textField>
 71                 <reportElement x="380" y="0" width="100" height="20" uuid="c4e844a8-2d48-478c-9265-4b1280dee581"/>
 72                 <textFieldExpression><![CDATA[$F{pending}]]></textFieldExpression>
 73             </textField>
 74             <textField>
 75                 <reportElement x="480" y="0" width="100" height="20" uuid="592114d8-df5c-4e06-ac85-f074093bcd03"/>
 76                 <textFieldExpression><![CDATA[$F{accepted}]]></textFieldExpression>
 77             </textField>
 78             <textField>
 79                 <reportElement x="580" y="0" width="100" height="20" uuid="b4d5d370-c299-4de5-a13d-5661563e6f08"/>
 80                 <textFieldExpression><![CDATA[$F{rejected}]]></textFieldExpression>
 81             </textField>
 82             <textField>
 83                 <reportElement x="680" y="0" width="100" height="20" uuid="66e08821-bf23-4001-9c20-afb657164dbb"/>
 84                 <textFieldExpression><![CDATA[$F{error}]]></textFieldExpression>
 85             </textField>
 86         </band>
 87     </detail>
 88     <summary>
 89         <band height="20" splitType="Stretch">
 90             <staticText>
 91                 <reportElement x="0" y="0" width="140" height="20" uuid="51bc79c0-9dfe-45f7-a177-227563ab8d4c"/>
 92                 <text><![CDATA[Total]]></text>
 93             </staticText>
 94             <textField>
 95                 <reportElement x="280" y="0" width="100" height="20" uuid="b6edaf65-05e4-458f-9672-1e2526e7f462"/>
 96                 <textFieldExpression><![CDATA[$P{totalPresented1}]]></textFieldExpression>
 97             </textField>
 98             <textField>
 99                 <reportElement x="380" y="0" width="100" height="20" uuid="841b4a18-0b16-40f9-8bf4-7cda720720e9"/>
100                 <textFieldExpression><![CDATA[$P{totalPending1}]]></textFieldExpression>
101             </textField>
102             <textField>
103                 <reportElement x="480" y="0" width="100" height="20" uuid="8ebb4d0d-1292-40b0-9153-1baac2cf4fc7"/>
104                 <textFieldExpression><![CDATA[$P{totalAccepted1}]]></textFieldExpression>
105             </textField>
106             <textField>
107                 <reportElement x="580" y="0" width="100" height="20" uuid="698a324f-3f94-4299-919c-05cfda3d2cb7"/>
108                 <textFieldExpression><![CDATA[$P{totalRejected1}]]></textFieldExpression>
109             </textField>
110             <textField>
111                 <reportElement x="680" y="0" width="100" height="20" uuid="28e2c6c6-3eb9-4a93-ad50-6019cdd8c84e"/>
112                 <textFieldExpression><![CDATA[$P{totalError1}]]></textFieldExpression>
113             </textField>
114         </band>
115     </summary>
116 </jasperReport>

  子报表2:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!-- Created with Jaspersoft Studio version 6.0.4.final using JasperReports Library version 6.0.4  -->
  3 <!-- 2015-08-21T14:03:25 -->
  4 <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="sub1" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8392e054-83a8-48b1-b95e-3c40bf5a1143">
  5     <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
  6     <parameter name="totalPresented2" class="java.math.BigDecimal"/>
  7     <parameter name="totalPending2" class="java.math.BigDecimal"/>
  8     <parameter name="totalAccepted2" class="java.math.BigDecimal"/>
  9     <parameter name="totalRejected2" class="java.math.BigDecimal"/>
 10     <parameter name="totalError2" class="java.math.BigDecimal"/>
 11     <field name="productType" class="java.lang.String"/>
 12     <field name="presented" class="java.math.BigDecimal"/>
 13     <field name="pending" class="java.math.BigDecimal"/>
 14     <field name="accepted" class="java.math.BigDecimal"/>
 15     <field name="rejected" class="java.math.BigDecimal"/>
 16     <field name="error" class="java.math.BigDecimal"/>
 17     <background>
 18         <band splitType="Stretch"/>
 19     </background>
 20     <title>
 21         <band height="20" splitType="Stretch">
 22             <staticText>
 23                 <reportElement x="0" y="0" width="140" height="20" uuid="9b246e53-82ea-4726-a86a-d74a59e42797"/>
 24                 <text><![CDATA[Product Type]]></text>
 25             </staticText>
 26             <staticText>
 27                 <reportElement x="140" y="0" width="140" height="20" uuid="1809f9c9-a70c-4998-b582-f36c81518abf"/>
 28                 <text><![CDATA[Presented]]></text>
 29             </staticText>
 30             <staticText>
 31                 <reportElement x="280" y="0" width="100" height="20" uuid="08fea51b-0358-469a-8215-2fa192608884"/>
 32                 <text><![CDATA[Pending]]></text>
 33             </staticText>
 34             <staticText>
 35                 <reportElement x="380" y="0" width="100" height="20" uuid="19a9f495-f801-4019-821d-77b9e26de74e"/>
 36                 <text><![CDATA[Accepted]]></text>
 37             </staticText>
 38             <staticText>
 39                 <reportElement x="480" y="0" width="100" height="20" uuid="aad9f333-5254-4389-a7cc-41455b4a5c92"/>
 40                 <text><![CDATA[Rejected]]></text>
 41             </staticText>
 42             <staticText>
 43                 <reportElement x="580" y="0" width="100" height="20" uuid="27b14062-5dba-412f-81fb-60951d7edd83"/>
 44                 <text><![CDATA[Error]]></text>
 45             </staticText>
 46         </band>
 47     </title>
 48     <columnHeader>
 49         <band height="2" splitType="Stretch"/>
 50     </columnHeader>
 51     <detail>
 52         <band height="20" splitType="Stretch">
 53             <textField>
 54                 <reportElement x="0" y="0" width="140" height="20" uuid="7d71429a-291e-4d2f-a35d-c4a0cc724009"/>
 55                 <textFieldExpression><![CDATA[$F{productType}]]></textFieldExpression>
 56             </textField>
 57             <textField>
 58                 <reportElement x="140" y="0" width="140" height="20" uuid="cf1a29e2-490c-4e5f-be32-e131292daeb5"/>
 59                 <textFieldExpression><![CDATA[$F{presented}]]></textFieldExpression>
 60             </textField>
 61             <textField>
 62                 <reportElement x="280" y="0" width="100" height="20" uuid="cf1a29e2-490c-4e5f-be32-e131292daeb5"/>
 63                 <textFieldExpression><![CDATA[$F{pending}]]></textFieldExpression>
 64             </textField>
 65             <textField>
 66                 <reportElement x="380" y="0" width="100" height="20" uuid="cf1a29e2-490c-4e5f-be32-e131292daeb5"/>
 67                 <textFieldExpression><![CDATA[$F{accepted}]]></textFieldExpression>
 68             </textField>
 69             <textField>
 70                 <reportElement x="480" y="0" width="100" height="20" uuid="cf1a29e2-490c-4e5f-be32-e131292daeb5"/>
 71                 <textFieldExpression><![CDATA[$F{rejected}]]></textFieldExpression>
 72             </textField>
 73             <textField>
 74                 <reportElement x="580" y="0" width="100" height="20" uuid="cf1a29e2-490c-4e5f-be32-e131292daeb5"/>
 75                 <textFieldExpression><![CDATA[$F{error}]]></textFieldExpression>
 76             </textField>
 77         </band>
 78     </detail>
 79     <summary>
 80         <band height="20" splitType="Stretch">
 81             <staticText>
 82                 <reportElement x="0" y="0" width="140" height="20" uuid="df09d91c-970d-4014-bae0-e585058b132d"/>
 83                 <text><![CDATA[Total]]></text>
 84             </staticText>
 85             <textField>
 86                 <reportElement x="140" y="0" width="140" height="20" uuid="ed974398-9a6d-45f8-a512-09a2082b47df"/>
 87                 <textFieldExpression><![CDATA[$P{totalPresented2}]]></textFieldExpression>
 88             </textField>
 89             <textField>
 90                 <reportElement x="280" y="0" width="100" height="20" uuid="4bbcae9e-f867-4e2c-abe2-da1fa26000bb"/>
 91                 <textFieldExpression><![CDATA[$P{totalPending2}]]></textFieldExpression>
 92             </textField>
 93             <textField>
 94                 <reportElement x="380" y="0" width="100" height="20" uuid="15351679-d011-460b-8067-259b3ef326dd"/>
 95                 <textFieldExpression><![CDATA[$P{totalAccepted2}]]></textFieldExpression>
 96             </textField>
 97             <textField>
 98                 <reportElement x="480" y="0" width="100" height="20" uuid="c92a203d-d038-4cad-b084-fc541e127e4b"/>
 99                 <textFieldExpression><![CDATA[$P{totalRejected2}]]></textFieldExpression>
100             </textField>
101             <textField>
102                 <reportElement x="580" y="0" width="100" height="20" uuid="b9155d41-f983-43e2-95a2-11cc8dd0b9eb"/>
103                 <textFieldExpression><![CDATA[$P{totalError2}]]></textFieldExpression>
104             </textField>
105         </band>
106     </summary>
107 </jasperReport>

  生成PDF效果图

结束!!

时间: 2024-08-30 00:21:59

Jasper Report (3)--- 用JavaBean Collection做为数据源的相关文章

Jasper Report (2)--- 用JDBC做为数据源

用JDBC做为jasper report的数据源来连接数据库读取数据 1. 在Eclipse中新建Java项目(过程略),我的Java项目的结构如下所示: 注:这里的有关spring的jar包并不是改项目运行必须的,是我在做用JavaBean做为数据源的时候用到的包,请忽略. 2.连接数据库代码(我连接的是Mysql数据库) 1 package com.report.sample; 2 3 import java.sql.Connection; 4 import java.sql.DriverM

初试Jasper Report

第一步,打开Jaspersoft Studio,界面应该长这样(跟eclispse长的一样) 默认的项目名称是MyReports,当然也可以自己创建,此时我们需要这么干 点击File → New → Other,然后你会看到向导,此时勾选show all wizards(不勾选先看看长什么样子),然后点开java 或者jaspersoft studio两个文件夹,一般选的就是java project 或者jasperReports project,当然了,也可以将你自己的文件import到jas

Jasper Report (1)--- Eclipse集成Ireport插件

环境的搭建(Eclipse中集成Ireport插件) 1. 首先在eclipse的工具栏点击 Help --> Eclipse MarketPlace --> Find 输入“ireport”点击搜索会出现如图所示的界面,按照图示进行安装插件(目前最新的是6.10) 2.安装成功后会提醒重新启动eclipse软件,重新启动后如图所示,验证插件是否安装成功 至此,eclipse集成ipreport插件已经成功,可以进行jasper report的设计了!

用Jasper report来实现交叉报表的增长率计算

来源:     http://community.jaspersoft.com/questions/847490/how-get-annual-growth-rate-crosstab 交叉表的每一列都是动态生成的,进行列间计算时需要动态引用,用Jasper脚本实现此类需求有一定难度,用集算器在数据准备阶段实现则相对简单,下面用例子来说明. 数据库表store存储着多种产品在2014.2015年的销售量,需要用交叉表呈现每种产品每年的销售量,并计算出各产品的年增长率.部分源数据如下: 集算器代码

用Jasper report计算opening balance

来源: http://community.jaspersoft.com/questions/850400/how-create-report-opening-balance-using-jasper-reports . 根据存取金额计算初期余额时需要进行跨行计算,用Jasper表达式可以实现,但步骤复杂有一定难度,用集算器协助Jasper则可以轻松实现,下面用简化的例子来说明. data.csv存储着某账户各期资金存取情况,需要根据该文件计算出初期余额,部分源数据如下: 集算器代码: A1:以逗

报表示例——用Jasper report实现MongoDB join

多样性数据源是报表开发的常见问题,但用JasperReport等报表工具本身难以处理,比如展现两个MongoDB collection连接的结果.虽然JasperReport有virtual data source或table join,但这些功能只在商业版或高端版本出现,在免费版中实现的难度很大.而且这些功能只支持两个数据源的连接,要实现多连接则麻烦得多.另外,这些功能只是图形化界面,无法对连接后的数据进行类似SQL的结构化计算. 集算器具有结构化强计算引擎,支持多样性数据源,集成简单,可以协

用Jasper Report制作Mongodb join Mysql的报表例子

多样性和多数据源问题使用JasperReport等报表工具本身不易处理,比如展现MongoDB和mysql的混合运算.虽然JasperReport/Birt有virtual data source或table join等功能,但只在商业版或高端版本出现,在免费版中实现难度很大,而且功能也有较大局限,无法对连接后的数据进行类似SQL的结构化计算. 集算器具有结构化强计算引擎,支持多样性数据源,集成简单,可以协助报表工具方便地实现此类需求.下面通过一个例子来说明MongoDB join mysql的

ComboBox绑定Dictionary做为数据源

http://www.cnblogs.com/refresh/archive/2012/07/14/2591503.html https://msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource(v=vs.80).aspx ComboBox的DataSource的值可设置为 :实现 IList 接口的对象,如 DataSet 或 Array.默认为 空引用(在 Visual Basic 中为 Nothing). 但

[翻译]在objective c创建自定义collection view样式

创建自定义collection样式 苹果在ios6中新增了一个更加易于创建和管理复杂用户界面的类:collection view.在此之前ios6上面用于展示多项列表的是table view,虽然名称是表格但它展示信息的形式并不是表格形式,而是垂直列表,当然在实际设计展示垂直可滚动的文本型列表时非常有用,但是你会遇到许多垂直可滚动的列表所存在的局限性,比如不能水平滚动,每行只有一项单元格,没有复杂的样式,在ios6之前你想要做到这些事情,只能靠自定义扩展. 在ios6 平台介绍了一个新的类:co