编程语言
首页 > 编程语言> > 2019-05-09 EasyWeb下使用JasperReport制作主从表

2019-05-09 EasyWeb下使用JasperReport制作主从表

作者:互联网

【制作思路】

一个报表模板只能放置一个数据源,当有两个及以上时候,需要使用主报表嵌套子报表;主报表使用数据源方式传入数据,子报表使用参数方式传入数据

【开发注意】

1.报表中使用了二维码,所以需要引入依赖,所有与报表相关的依赖如下

 1         <dependency>
 2             <groupId>net.sf.jasperreports</groupId>
 3             <artifactId>jasperreports</artifactId>
 4             <version>6.8.0</version>
 5         </dependency>
 6         
 7         <!-- jasperreport用到的二维码生成 -->
 8         <dependency>
 9             <groupId>net.sf.barcode4j</groupId>
10             <artifactId>barcode4j</artifactId>
11             <version>2.1</version>
12         </dependency>
13         <!-- jasperreport 宋体字符库 解决中文不显示 -->
14         <dependency>
15             <groupId>cn.lesper</groupId>
16             <artifactId>iTextAsian</artifactId>
17             <version>3.0</version>
18         </dependency>

2.需要在报表模板编辑器Parameters中定义存放从表数据的参数detailData

3.在报表模板编辑器中增加Subreport指向子报表模板

4.在Subreport属性中设置Data Source Expression(数据源表达式),通过java类转换成数据源传给子报表

1 new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{detailData})

【关键代码】

1.主报表模板

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
  3 <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="order" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3f7172d1-9779-4750-b9e5-5abc388f7210">
  4     <property name="com.jaspersoft.studio.data.defaultdataadapter" value="demo1"/>
  5     <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
  6     <property name="com.jaspersoft.studio.property.dataset.dialog.DatasetDialog.sash.w1" value="579"/>
  7     <property name="com.jaspersoft.studio.property.dataset.dialog.DatasetDialog.sash.w2" value="410"/>
  8     <parameter name="detailData" class="java.util.List"/>
  9     <parameter name="title" class="java.lang.String"/>
 10     <queryString>
 11         <![CDATA[]]>
 12     </queryString>
 13     <field name="date" class="java.util.Date">
 14         <fieldDescription><![CDATA[date]]></fieldDescription>
 15     </field>
 16     <field name="deptName" class="java.lang.String">
 17         <fieldDescription><![CDATA[deptName]]></fieldDescription>
 18     </field>
 19     <field name="creator" class="java.lang.String">
 20         <fieldDescription><![CDATA[creator]]></fieldDescription>
 21     </field>
 22     <field name="taskCode" class="java.lang.String">
 23         <fieldDescription><![CDATA[taskCode]]></fieldDescription>
 24     </field>
 25     <field name="orderCode" class="java.lang.String">
 26         <fieldDescription><![CDATA[orderCode]]></fieldDescription>
 27     </field>
 28     <field name="id" class="java.lang.String">
 29         <fieldDescription><![CDATA[id]]></fieldDescription>
 30     </field>
 31     <field name="state" class="java.lang.Integer">
 32         <fieldDescription><![CDATA[state]]></fieldDescription>
 33     </field>
 34     <field name="createDate" class="java.util.Date">
 35         <fieldDescription><![CDATA[createDate]]></fieldDescription>
 36     </field>
 37     <background>
 38         <band splitType="Stretch"/>
 39     </background>
 40     <title>
 41         <band height="42" splitType="Stretch">
 42             <textField>
 43                 <reportElement x="180" y="6" width="200" height="30" uuid="1bbd790d-66dd-4152-9ecf-ca82def10e00"/>
 44                 <textElement textAlignment="Center" verticalAlignment="Middle">
 45                     <font fontName="宋体" size="20"/>
 46                 </textElement>
 47                 <textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
 48             </textField>
 49         </band>
 50     </title>
 51     <pageHeader>
 52         <band height="88" splitType="Stretch">
 53             <staticText>
 54                 <reportElement x="0" y="0" width="40" height="20" uuid="523f3327-2264-445f-b837-5dd6c4018f08"/>
 55                 <textElement>
 56                     <font fontName="宋体" size="14"/>
 57                 </textElement>
 58                 <text><![CDATA[日期]]></text>
 59             </staticText>
 60             <staticText>
 61                 <reportElement x="0" y="30" width="40" height="20" uuid="05b201de-67ab-4ffa-b3cd-695f5f25e3c8"/>
 62                 <textElement>
 63                     <font fontName="宋体" size="14"/>
 64                 </textElement>
 65                 <text><![CDATA[部门]]></text>
 66             </staticText>
 67             <staticText>
 68                 <reportElement x="120" y="0" width="60" height="20" uuid="505f2466-8591-4e6a-a06f-84d46a09be8a"/>
 69                 <textElement>
 70                     <font fontName="宋体" size="14"/>
 71                 </textElement>
 72                 <text><![CDATA[任务单号]]></text>
 73             </staticText>
 74             <staticText>
 75                 <reportElement x="300" y="0" width="60" height="20" uuid="275e5db4-29fe-496f-97d6-e1877e69d1aa"/>
 76                 <textElement>
 77                     <font fontName="宋体" size="14"/>
 78                 </textElement>
 79                 <text><![CDATA[派工单号]]></text>
 80             </staticText>
 81             <staticText>
 82                 <reportElement x="120" y="30" width="60" height="20" uuid="6522ec29-267f-4bba-a3b2-c6c97f264648"/>
 83                 <textElement>
 84                     <font fontName="宋体" size="14"/>
 85                 </textElement>
 86                 <text><![CDATA[制单人]]></text>
 87             </staticText>
 88             <staticText>
 89                 <reportElement x="300" y="30" width="60" height="20" uuid="e87bf7b2-88ed-42bf-a384-40a514c4ae72"/>
 90                 <textElement>
 91                     <font fontName="宋体" size="14"/>
 92                 </textElement>
 93                 <text><![CDATA[制单日期]]></text>
 94             </staticText>
 95             <staticText>
 96                 <reportElement x="0" y="60" width="40" height="20" uuid="522db4c7-92db-43da-8c54-85556594bfd7"/>
 97                 <textElement>
 98                     <font fontName="宋体" size="14"/>
 99                 </textElement>
100                 <text><![CDATA[状态]]></text>
101             </staticText>
102             <textField>
103                 <reportElement x="40" y="0" width="70" height="20" uuid="99f3e0ae-0525-42ea-aa03-0c5c80decd5e"/>
104                 <textElement verticalAlignment="Middle">
105                     <font fontName="宋体"/>
106                 </textElement>
107                 <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
108             </textField>
109             <textField>
110                 <reportElement x="40" y="30" width="70" height="20" uuid="40729b5d-b5f5-46a3-bde3-49898de66db2"/>
111                 <textElement verticalAlignment="Middle">
112                     <font fontName="宋体"/>
113                 </textElement>
114                 <textFieldExpression><![CDATA[$F{deptName}]]></textFieldExpression>
115             </textField>
116             <textField>
117                 <reportElement x="180" y="0" width="110" height="20" uuid="02467d83-b5be-4e12-8729-22de35be5fe9"/>
118                 <textElement verticalAlignment="Middle">
119                     <font fontName="宋体"/>
120                 </textElement>
121                 <textFieldExpression><![CDATA[$F{orderCode}]]></textFieldExpression>
122             </textField>
123             <textField>
124                 <reportElement x="180" y="30" width="110" height="20" uuid="b2b3a476-7518-4cae-8011-84da7a2e3df4"/>
125                 <textElement verticalAlignment="Middle">
126                     <font fontName="宋体"/>
127                 </textElement>
128                 <textFieldExpression><![CDATA[$F{creator}]]></textFieldExpression>
129             </textField>
130             <textField>
131                 <reportElement x="360" y="0" width="110" height="20" uuid="e0990b1b-42f4-455a-b8b3-98cc4163bdfd"/>
132                 <textElement verticalAlignment="Middle">
133                     <font fontName="宋体"/>
134                 </textElement>
135                 <textFieldExpression><![CDATA[$F{taskCode}]]></textFieldExpression>
136             </textField>
137             <textField>
138                 <reportElement x="360" y="30" width="110" height="20" uuid="adcd741c-1b9a-4309-a71d-3fe3bd428279"/>
139                 <textElement verticalAlignment="Middle">
140                     <font fontName="宋体"/>
141                 </textElement>
142                 <textFieldExpression><![CDATA[$F{createDate}]]></textFieldExpression>
143             </textField>
144             <textField>
145                 <reportElement x="40" y="60" width="140" height="20" uuid="afbdae73-4cd7-44ab-9f09-fb05f77536b6"/>
146                 <textElement verticalAlignment="Middle">
147                     <font fontName="宋体"/>
148                 </textElement>
149                 <textFieldExpression><![CDATA[$F{state}==0?"保存":($F{state}==1?"下单":"作废")]]></textFieldExpression>
150             </textField>
151             <componentElement>
152                 <reportElement x="460" y="6" width="70" height="70" uuid="67689075-e44b-4c4b-ad2b-d899415a1f4e"/>
153                 <jr:QRCode xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" margin="0">
154                     <jr:codeExpression><![CDATA["master:"+$F{id}]]></jr:codeExpression>
155                 </jr:QRCode>
156             </componentElement>
157             <line>
158                 <reportElement x="-8" y="87" width="570" height="1" uuid="d6a63de9-1c8b-44be-b42e-104952a24eb5"/>
159             </line>
160         </band>
161     </pageHeader>
162     <detail>
163         <band height="100" splitType="Stretch">
164             <subreport>
165                 <reportElement x="-20" y="0" width="595" height="100" uuid="c229f65b-afea-4a9f-abde-195e7321065e"/>
166                 <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{detailData})]]></dataSourceExpression>
167                 <subreportExpression><![CDATA["src/main/resources/jasper/demo1/order-detail.jasper"]]></subreportExpression>
168             </subreport>
169         </band>
170     </detail>
171 </jasperReport>

2.从报表模板

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
 3 <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="order-detail" columnCount="2" printOrder="Horizontal" pageWidth="595" pageHeight="842" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fd47ee62-841a-40bf-8baf-fadfee67070c">
 4     <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
 5     <queryString>
 6         <![CDATA[]]>
 7     </queryString>
 8     <field name="productCode" class="java.lang.String">
 9         <fieldDescription><![CDATA[productCode]]></fieldDescription>
10     </field>
11     <field name="orderNum" class="java.lang.String">
12         <fieldDescription><![CDATA[orderNum]]></fieldDescription>
13     </field>
14     <field name="id" class="java.lang.String">
15         <fieldDescription><![CDATA[id]]></fieldDescription>
16     </field>
17     <field name="productName" class="java.lang.String">
18         <fieldDescription><![CDATA[productName]]></fieldDescription>
19     </field>
20     <background>
21         <band splitType="Stretch"/>
22     </background>
23     <detail>
24         <band height="102" splitType="Stretch">
25             <staticText>
26                 <reportElement x="90" y="10" width="60" height="20" uuid="02e931c4-a903-492f-a915-fb4f38a8b130"/>
27                 <textElement>
28                     <font fontName="宋体" size="14" isBold="true"/>
29                 </textElement>
30                 <text><![CDATA[产品代码]]></text>
31             </staticText>
32             <staticText>
33                 <reportElement x="90" y="40" width="60" height="20" uuid="98efa39e-7d23-4c1e-bcd6-acf0953c5429"/>
34                 <textElement>
35                     <font fontName="宋体" size="14" isBold="true"/>
36                 </textElement>
37                 <text><![CDATA[产品名称]]></text>
38             </staticText>
39             <staticText>
40                 <reportElement x="90" y="70" width="60" height="19" uuid="33c4eae1-f075-440b-8744-4d816f4167ff"/>
41                 <textElement textAlignment="Left">
42                     <font fontName="宋体" size="14" isBold="true"/>
43                 </textElement>
44                 <text><![CDATA[数  量  ]]></text>
45             </staticText>
46             <textField>
47                 <reportElement x="160" y="10" width="110" height="20" uuid="a565adea-bac5-4406-95b5-900def774984"/>
48                 <textElement verticalAlignment="Middle">
49                     <font fontName="宋体"/>
50                 </textElement>
51                 <textFieldExpression><![CDATA[$F{productCode}]]></textFieldExpression>
52             </textField>
53             <textField>
54                 <reportElement x="160" y="40" width="105" height="20" uuid="8222b996-e9b4-4b51-a314-990341b1f5d2"/>
55                 <textElement verticalAlignment="Middle">
56                     <font fontName="宋体"/>
57                 </textElement>
58                 <textFieldExpression><![CDATA[$F{productName}]]></textFieldExpression>
59             </textField>
60             <textField>
61                 <reportElement x="160" y="70" width="105" height="19" uuid="e5985c93-c08a-4b01-a6ea-b54097e057d3"/>
62                 <textElement verticalAlignment="Middle">
63                     <font fontName="宋体"/>
64                 </textElement>
65                 <textFieldExpression><![CDATA[$F{orderNum}]]></textFieldExpression>
66             </textField>
67             <componentElement>
68                 <reportElement x="0" y="10" width="80" height="80" uuid="1293a5a8-ac25-44d0-a17a-d055c6bcbd31"/>
69                 <jr:QRCode xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" margin="0">
70                     <jr:codeExpression><![CDATA["detail:"+$F{id}]]></jr:codeExpression>
71                 </jr:QRCode>
72             </componentElement>
73             <line>
74                 <reportElement x="0" y="101" width="270" height="1" forecolor="#BFB2B2" uuid="1f835a86-88bb-4647-bcb1-290e79355507"/>
75             </line>
76         </band>
77     </detail>
78 </jasperReport>

3.后端Java 代码

 1 @RequestMapping("/report")
 2     public void getReport(String orderID,HttpServletRequest request, HttpServletResponse response) throws Exception {
 3         // 主表数据放入主报表模板渲染数据源
 4         List<Object> list = new ArrayList<Object>();
 5         ProductOrderExample example1 = new ProductOrderExample();
 6         example1.createCriteria().andIdEqualTo(orderID);
 7         list.add(productOrderService.list(example1, 1, 1).getData().get(0));
 8         
 9         
10         HashMap<String, Object> parameters = new HashMap<String, Object>();
11         
12         // 从表数据放入主报表模板参数detailData
13         ProductOrderDetailExample example2 = new ProductOrderDetailExample();
14         example2.createCriteria().andOrderIDEqualTo(orderID);
15         parameters.put("detailData",productOrderDetailService.list(example2, 0, 0).getData());
16         // 设置报表标题参数
17         parameters.put("title","生产派工单");
18         
19         
20         JasperReportUtils.printPdf2HtmlByJavaBean(request, response, "classpath:jasper/demo1/order.jasper",parameters ,list );
21 
22 
23     }

4.上面涉及的printPdf2HtmlByJavaBean封装代码

 1     /**
 2      * 使用JavaBean填充打印PDF到Html中
 3      * @param request
 4      * @param response
 5      * @param jasperPath 例如:classpath:jasper/report2.jasper
 6      * @param list JavaBean数据
 7      * @param parameters 
 8      * @throws Exception
 9      */
10     public static void printPdf2HtmlByJavaBean(HttpServletRequest request, HttpServletResponse response,String jasperPath,Map<String, Object> parameters,List<?> list) throws Exception {
11 
12         ServletOutputStream outputStream = response.getOutputStream();
13 
14         //获取报表的编译文件,后面要将对其进行填充数据
15         String path = URLDecoder.decode(ResourceUtils.getURL(jasperPath).getPath(),"UTF-8");
16         
17         FileInputStream inputStream =new FileInputStream(new File(path));
18 
19         //由于没有数据,故这里使用空参数和空数据源,该方法需要抛出JRException异常
20         JasperRunManager.runReportToPdfStream(inputStream, outputStream, parameters , new JRBeanCollectionDataSource(list));
21 
22         // 输出PDF
23         response.setContentType("application/pdf");
24 
25         // 关闭资源
26         outputStream.flush();
27         outputStream.close();
28         inputStream.close();
29         
30     }

【最终效果】

 

标签:报表,parameters,05,数据源,09,list,JasperReport,new,模板
来源: https://www.cnblogs.com/WongHugh/p/10840831.html