• Jasper+ireport实现子报表使用json数据源


    目前的需求是要在同一个pdf文件里面打印两张表格,使用json数据源并且是不同的数据源。效果图如下↓

    这里两张表都是json数据源,主报表采用的是动态数据源,子报表时读取文件里面的数据源。

    主报表xml文件如下↓

      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <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="JsonData" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="204d2c12-6e17-4f5a-88d5-797be11591a1">
      3     <property name="ireport.zoom" value="1.0"/>
      4     <property name="ireport.x" value="0"/>
      5     <property name="ireport.y" value="0"/>
      6     <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
      7         <defaultValueExpression><![CDATA["F:\reportTest\"]]></defaultValueExpression>
      8     </parameter>
      9     <queryString language="json">
     10         <![CDATA[sales.item]]>
     11     </queryString>
     12     <field name="equipment" class="java.lang.String"/>
     13     <field name="project" class="java.lang.String"/>
     14     <field name="count" class="java.lang.String"/>
     15     <title>
     16         <band height="79" splitType="Stretch">
     17             <staticText>
     18                 <reportElement x="137" y="0" width="200" height="20" uuid="dfc1b3b3-97f7-4168-8509-1b611bea72c3"/>
     19                 <textElement textAlignment="Center">
     20                     <font isBold="true"/>
     21                 </textElement>
     22                 <text><![CDATA[子报表使用json数据源测试]]></text>
     23             </staticText>
     24         </band>
     25     </title>
     26     <columnHeader>
     27         <band height="20">
     28             <staticText>
     29                 <reportElement x="75" y="0" width="100" height="20" uuid="1c9641be-6be2-486b-91e9-b61394a54cb7"/>
     30                 <box>
     31                     <topPen lineWidth="1.0"/>
     32                     <leftPen lineWidth="1.0"/>
     33                     <bottomPen lineWidth="1.0"/>
     34                     <rightPen lineWidth="1.0"/>
     35                 </box>
     36                 <text><![CDATA[equipment]]></text>
     37             </staticText>
     38             <staticText>
     39                 <reportElement x="175" y="0" width="100" height="20" uuid="61761013-f83f-4847-a308-ebb19beceacd"/>
     40                 <box>
     41                     <topPen lineWidth="1.0"/>
     42                     <leftPen lineWidth="1.0"/>
     43                     <bottomPen lineWidth="1.0"/>
     44                     <rightPen lineWidth="1.0"/>
     45                 </box>
     46                 <text><![CDATA[project]]></text>
     47             </staticText>
     48             <staticText>
     49                 <reportElement x="275" y="0" width="100" height="20" uuid="0620aa0a-e5b6-4c89-940e-cb464d774026"/>
     50                 <box>
     51                     <topPen lineWidth="1.0"/>
     52                     <leftPen lineWidth="1.0"/>
     53                     <bottomPen lineWidth="1.0"/>
     54                     <rightPen lineWidth="1.0"/>
     55                 </box>
     56                 <text><![CDATA[count]]></text>
     57             </staticText>
     58         </band>
     59     </columnHeader>
     60     <detail>
     61         <band height="20">
     62             <textField>
     63                 <reportElement x="75" y="0" width="100" height="20" uuid="24ed5267-5dd7-449c-ab3c-ccb5ceb627a2"/>
     64                 <box>
     65                     <topPen lineWidth="0.75"/>
     66                     <leftPen lineWidth="0.75"/>
     67                     <bottomPen lineWidth="0.75"/>
     68                     <rightPen lineWidth="0.75"/>
     69                 </box>
     70                 <textFieldExpression><![CDATA[$F{equipment}]]></textFieldExpression>
     71             </textField>
     72             <textField>
     73                 <reportElement x="175" y="0" width="100" height="20" uuid="8d50db0b-645c-4ec8-8fb3-579e2bab2b13"/>
     74                 <box>
     75                     <topPen lineWidth="0.75"/>
     76                     <leftPen lineWidth="0.75"/>
     77                     <bottomPen lineWidth="0.75"/>
     78                     <rightPen lineWidth="0.75"/>
     79                 </box>
     80                 <textFieldExpression><![CDATA[$F{project}]]></textFieldExpression>
     81             </textField>
     82             <textField>
     83                 <reportElement x="275" y="0" width="100" height="20" uuid="dee5fca0-ff07-4166-b81a-1035c94518ca"/>
     84                 <box>
     85                     <topPen lineWidth="0.75"/>
     86                     <leftPen lineWidth="0.75"/>
     87                     <bottomPen lineWidth="0.75"/>
     88                     <rightPen lineWidth="0.75"/>
     89                 </box>
     90                 <textFieldExpression><![CDATA[$F{count}]]></textFieldExpression>
     91             </textField>
     92         </band>
     93     </detail>
     94     <summary>
     95         <band height="148" splitType="Stretch">
     96             <subreport>
     97                 <reportElement x="0" y="0" width="555" height="148" uuid="7a8dcc6f-51eb-449b-8887-296672171b47"/>
     98                 <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "JsonData_subreport1.jasper"]]></subreportExpression>
     99             </subreport>
    100         </band>
    101     </summary>
    102 </jasperReport>
    JsonData.jrxml

    子报表xml文件如下↓

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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="JsonData_subreport1" pageWidth="555" pageHeight="802" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="e3ad0c84-019d-458e-b9b7-ef798faab90e">
     3     <property name="ireport.zoom" value="1.0"/>
     4     <property name="ireport.x" value="0"/>
     5     <property name="ireport.y" value="0"/>
     6     <property name="net.sf.jasperreports.json.source" value="F:UsersDesktoplibjson.json"/>
     7     <queryString language="json">
     8         <![CDATA[item2]]>
     9     </queryString>
    10     <field name="item" class="java.lang.String"/>
    11     <field name="quantity" class="java.lang.String"/>
    12     <columnHeader>
    13         <band height="50">
    14             <staticText>
    15                 <reportElement x="73" y="30" width="100" height="20" uuid="964539ea-f556-426c-b571-5bc12ccacad0"/>
    16                 <box>
    17                     <topPen lineWidth="0.75"/>
    18                     <leftPen lineWidth="0.75"/>
    19                     <bottomPen lineWidth="0.75"/>
    20                     <rightPen lineWidth="0.75"/>
    21                 </box>
    22                 <text><![CDATA[item]]></text>
    23             </staticText>
    24             <staticText>
    25                 <reportElement x="173" y="30" width="100" height="20" uuid="6578bf25-ff51-41c8-939f-5c40236770dd"/>
    26                 <box>
    27                     <topPen lineWidth="0.75"/>
    28                     <leftPen lineWidth="0.75"/>
    29                     <bottomPen lineWidth="0.75"/>
    30                     <rightPen lineWidth="0.75"/>
    31                 </box>
    32                 <text><![CDATA[quantity]]></text>
    33             </staticText>
    34         </band>
    35     </columnHeader>
    36     <detail>
    37         <band height="20">
    38             <textField>
    39                 <reportElement x="73" y="0" width="100" height="20" uuid="72cae18f-36f8-42b4-b5a6-8dd35ab6e5a4"/>
    40                 <box>
    41                     <topPen lineWidth="1.0"/>
    42                     <leftPen lineWidth="1.0"/>
    43                     <bottomPen lineWidth="1.0"/>
    44                     <rightPen lineWidth="1.0"/>
    45                 </box>
    46                 <textElement>
    47                     <font fontName="微软雅黑"/>
    48                 </textElement>
    49                 <textFieldExpression><![CDATA[$F{item}]]></textFieldExpression>
    50             </textField>
    51             <textField>
    52                 <reportElement x="173" y="0" width="100" height="20" uuid="063a6d61-e009-495a-acd5-9dae33f9379d"/>
    53                 <box>
    54                     <topPen lineWidth="1.0"/>
    55                     <leftPen lineWidth="1.0"/>
    56                     <bottomPen lineWidth="1.0"/>
    57                     <rightPen lineWidth="1.0"/>
    58                 </box>
    59                 <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
    60             </textField>
    61         </band>
    62     </detail>
    63 </jasperReport>
    JsonData_subreport1.jrxml

    Java代码如下↓

     1 package a.test.xmlfile;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.ByteArrayInputStream;
     5 import java.io.FileInputStream;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 import java.io.InputStreamReader;
     9 import java.util.HashMap;
    10 import java.util.Locale;
    11 
    12 import net.sf.jasperreports.engine.JRParameter;
    13 import net.sf.jasperreports.engine.JasperCompileManager;
    14 import net.sf.jasperreports.engine.JasperFillManager;
    15 import net.sf.jasperreports.engine.JasperPrint;
    16 import net.sf.jasperreports.engine.JasperReport;
    17 import net.sf.jasperreports.engine.design.JasperDesign;
    18 import net.sf.jasperreports.engine.query.JsonQueryExecuterFactory;
    19 import net.sf.jasperreports.engine.xml.JRXmlLoader;
    20 import net.sf.jasperreports.view.JasperViewer;
    21 
    22 public class SubJsonDataTest {
    23     public static String ReadFile(String Path) {
    24         BufferedReader reader = null;
    25         String laststr = "";
    26         try {
    27             FileInputStream fileInputStream = new FileInputStream(Path);
    28             InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
    29             reader = new BufferedReader(inputStreamReader);
    30             String tempString = null;
    31             while ((tempString = reader.readLine()) != null) {
    32                 laststr += tempString;
    33             }
    34             reader.close();
    35         } catch (IOException e) {
    36             e.printStackTrace();
    37         } finally {
    38             if (reader != null) {
    39                 try {
    40                     reader.close();
    41                 } catch (IOException e) {
    42                     e.printStackTrace();
    43                 }
    44             }
    45         }
    46         return laststr;
    47     }
    48 
    49     public static void main(String[] args) {
    50         //这里注意更改路径
    51         String s = ReadFile("F:\Users\Desktop\lib\tianjin.json");
    52         JasperDesign design;
    53         try {
    54             // 注意更改路径
    55             design = JRXmlLoader.load("F:\reportTest\JsonData.jrxml");
    56             HashMap<String, Object> paramsMap = new HashMap<String, Object>();
    57             // 第二种配置数据源的方式 InputStream
    58             InputStream is = new ByteArrayInputStream(s.getBytes("UTF-8"));
    59             paramsMap.put("JSON_INPUT_STREAM", is);
    60             // 配置其他参数
    61             paramsMap.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.ENGLISH);
    62             paramsMap.put(JRParameter.REPORT_LOCALE, Locale.US);
    63             // 加载模板
    64             JasperReport report = JasperCompileManager.compileReport(design);
    65             // 填充数据
    66             JasperPrint print = JasperFillManager.fillReport(report, paramsMap);
    67             // 预览显示
    68             JasperViewer.viewReport(print);
    69 
    70         } catch (Exception e) {
    71             // TODO Auto-generated catch block
    72             e.printStackTrace();
    73         }
    74     }
    75 
    76 }
    SubJsonDataTest

    主报表数据源如下↓

     1  { "sales": {
     2     "item": [
     3        {
     4         
     5         "equipment": "equipment1",
     6         "project": "project1",
     7         "count": "1"
     8       },
     9       {
    10         
    11         "equipment": "equipment2",
    12         "project": "project2",
    13         "count": "1"
    14       },
    15       {
    16         
    17         "equipment": "equipment3",
    18         "project": "project3",
    19         "count": "1"
    20       },
    21       {
    22         
    23         "equipment": "equipment4",
    24         "project": "project4",
    25         "count": "1"
    26       },
    27       {
    28         
    29         "equipment": "equipment5",
    30         "project": "project5",
    31         "count": "1"
    32       }
    33     ]
    34   }}
    tianjin.json

    子报表数据源如下↓

     1 {
     2     "item2": [
     3         {
     4             "id": "1",
     5             "item": "Book",
     6             "quantity": "15",
     7             "unitprice": "10"
     8         },
     9         {
    10             "id": "2",
    11             "item": "PDA",
    12             "quantity": "5",
    13             "unitprice": "100"
    14         },
    15         {
    16             "id": "2",
    17             "item": "测试",
    18             "quantity": "5",
    19             "unitprice": "100"
    20         }
    21     ],
    22     "field": "test"
    23 }
    json.json

    友情提示一,子报表存放的路径要和下图划线的地方一致

    本文版权归__馋猫和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

    友情提示二,如果报错如下图所示,说明虽然有了子报表的xml文件,但是没有Jasper文件,此时只需要在ireport里面预览一下子报表,就会生成Jasper文件。

  • 相关阅读:
    k3sk8s集群-node节点设置不可调度或者删除node节点
    CentOS8防暴力破解fail2ban
    网站挂马情况处理
    linux给普通用户赋予sudo权限免密码
    Mockito (二十四)
    Mockito (二十三)
    Mockito (二十二)
    Mockito (二十一)
    Mockito (二十)
    Mockito (十九)
  • 原文地址:https://www.cnblogs.com/chanmao--/p/6896071.html
Copyright © 2020-2023  润新知