2016-01-05 10 views
5

muszę dokonać raport tak:Jak utworzyć wiele tabel w raporcie jaspra używając json jako źródła danych?

Product Name : Product XYZ 

--------------------------------- 
| Item Name | Quantity | Price | 
--------------------------------- 
| Item ABC | 10  | 10 $ | 
| Item BCD | 10  | 1 $ | 

Product Name : Product XYZ2 

--------------------------------- 
| Item Name | Quantity | Price | 
--------------------------------- 
| Item DEF | 15  | 10 $ | 
| Item HIJ | 11  | 1 $ | 

Summary Report 
--------------------------------- 
| Product Name | Total Quantity | 
--------------------------------- 
| Product XYZ | 20    | 
| Product XYZ2 | 26    | 

Pierwszym problemem jest to "produkt" Puszka 1 do wielu.

Drugi problem to muszę „Summary Report” w dolnym

Trzecim problemem jest to surowy raport dane pochodzą z json, nie od zapytania do bazy danych.

Co to ja już spróbować:

  • stworzyć wiele tabeli produktu. Ale to się nie udało, ponieważ jaspis tworzy puste miejsce na nieużywany stół. I tabela podsumowująca na innej stronie z wieloma pustymi miejscami.

  • Podraporty, tak samo jak powyżej.

Czy możesz mi powiedzieć proszę, jak zrobić dynamicznie wiele stolików?

EDIT: próbka JSON

{"produkList": { 
"items":[ 
    { 
     "nameProduct": "Product XYZ", 
     "itemList": [{ 
      "itemName": "XXXXXXX", 
      "quantity": 50, 
      "price": 50 
     }, 
     { 
      "itemName": "YYYYYYY", 
      "quantity": 50, 
      "price": 50 
     }, 
     { 
      "itemName": "ZZZZZZZZ", 
      "quantity": 50, 
      "price": 50 
     }] 
    }, 
    { 
     "nameProduct": "Product XYZ2", 
     "itemList": [{ 
      "itemName": "AAAAAAAA", 
      "quantity": 50, 
      "price": 50 
     }, 
     { 
      "itemName": "BBBBBBB", 
      "quantity": 50, 
      "price": 50 
     }, 
     { 
      "itemName": "CCCCCCC", 
      "quantity": 50, 
      "price": 50 
     }] 
    } 
    ], 
    "summary":[ 
    { 
     "title": "Summary Report", 
     "summaryReportDetailList": [{ 
      "nameProduct": "Product XYZ", 
      "quantity": 150 
     }, 
     { 
      "nameProduct": "Product XYZ2", 
      "quantity": 150 
     }] 
    } 
    ] 
    } 
} 

Odpowiedz

6

Struktura będzie

  1. raport główny z zapytaniem produkList.items które mają się wyświetlać nazwa wyrobów
  2. Podraport w detail zespołu z <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("itemList")]]></dataSourceExpression> aby wyświetlić pozycja tabela
  3. Podraport w summary zespół z zapytaniem produkList.summary.summaryReportDetailList aby wyświetlić tabelę Podsumowanie

To wynik

PDF Result

Poniżej znajdziesz 3 jrxml produkujących ten wynik, musisz dostosować ścieżkę do źródła json i ścieżkę do podraportu, nie zapominaj, że podraport musi być skompilowany w .jasper przed głównym raportem można uruchomić.

Raport Główny

<?xml version="1.0" encoding="UTF-8"?> 
<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="multitable" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1c6dc1c9-67af-43d5-bf1e-9243557c8048"> 
<property name="net.sf.jasperreports.json.source" value="C:/jdd/projects/StackTrace/jasper/multitable.json"/> 
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false"> 
    <defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression> 
</parameter> 
<queryString language="json"> 
    <![CDATA[produkList.items]]> 
</queryString> 
<field name="nameProduct" class="java.lang.String"/> 
<detail> 
    <band height="48" splitType="Stretch"> 
     <textField> 
      <reportElement x="0" y="1" width="555" height="20" uuid="4dfdf5e5-436e-4538-b302-620ea945c6e7"/> 
      <textElement verticalAlignment="Middle"/> 
      <textFieldExpression><![CDATA["Product Name : " + $F{nameProduct}]]></textFieldExpression> 
     </textField> 
     <subreport> 
      <reportElement x="0" y="21" width="555" height="20" uuid="48a7bbe4-b8ce-4a0d-a6e1-6ddd288e5602"/> 
      <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("itemList")]]></dataSourceExpression> 
      <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "multitable_subreport.jasper"]]></subreportExpression> 
     </subreport> 
    </band> 
</detail> 
<summary> 
    <band height="52" splitType="Stretch"> 
     <subreport> 
      <reportElement x="0" y="10" width="555" height="32" uuid="780a1f25-065e-494a-ba1d-86a33b464343"/> 
      <subreportParameter name="net.sf.jasperreports.json.source"> 
       <subreportParameterExpression><![CDATA["C:/jdd/projects/StackTrace/jasper/multitable.json"]]></subreportParameterExpression> 
      </subreportParameter> 
      <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "multitable_summary.jasper"]]></subreportExpression> 
     </subreport> 
    </band> 
</summary> 
</jasperReport> 

Podraport szczegółowo zespołumultitable_subreport.jrxml

<?xml version="1.0" encoding="UTF-8"?> 
<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="multitable_subreport" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="678bf408-397f-4797-bb50-84262b9df76b"> 
<field name="itemName" class="java.lang.String"/> 
<field name="quantity" class="java.lang.Double"/> 
<field name="price" class="java.lang.Double"/> 
<columnHeader> 
    <band height="20" splitType="Stretch"> 
     <staticText> 
      <reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="af654b95-0a60-40fc-80c6-df38cc28061c"/> 
      <box> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement verticalAlignment="Middle"/> 
      <text><![CDATA[itemName]]></text> 
     </staticText> 
     <staticText> 
      <reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="7969463f-0569-4863-bbc3-a87364369a0f"/> 
      <box> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"/> 
      <text><![CDATA[quantity]]></text> 
     </staticText> 
     <staticText> 
      <reportElement mode="Opaque" x="200" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="86942d20-9a68-4409-8907-8abcfef1c82e"/> 
      <box> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"/> 
      <text><![CDATA[price]]></text> 
     </staticText> 
    </band> 
</columnHeader> 
<detail> 
    <band height="20" splitType="Stretch"> 
     <textField> 
      <reportElement x="0" y="0" width="100" height="20" uuid="2b991eb1-6e24-4e74-8ee2-cd90fe07f60a"/> 
      <box> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement verticalAlignment="Middle"/> 
      <textFieldExpression><![CDATA[$F{itemName}]]></textFieldExpression> 
     </textField> 
     <textField pattern="###0.##"> 
      <reportElement x="100" y="0" width="100" height="20" uuid="71929061-2351-477c-8d2d-431c846f8a06"/> 
      <box> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"/> 
      <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression> 
     </textField> 
     <textField pattern="¤ #,##0.00"> 
      <reportElement x="200" y="0" width="100" height="20" uuid="16b99246-d1f5-4c96-90db-34ea5f3141db"/> 
      <box> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"/> 
      <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression> 
     </textField> 
    </band> 
</detail> 
</jasperReport> 

Podraport Podsumowując zespołumultitable_summary.jrxml

<?xml version="1.0" encoding="UTF-8"?> 
<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="multitable_summary" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="2a869c20-1e7d-4baa-92d7-c8d555f1f344"> 
<queryString language="json"> 
    <![CDATA[produkList.summary.summaryReportDetailList]]> 
</queryString> 
<field name="nameProduct" class="java.lang.String"/> 
<field name="quantity" class="java.lang.Double"/> 
<background> 
    <band splitType="Stretch"/> 
</background> 
<title> 
    <band height="23"> 
     <staticText> 
      <reportElement x="0" y="0" width="100" height="20" uuid="a9730039-8418-4e58-a2eb-0babe8d2a380"/> 
      <text><![CDATA[Summary Report]]></text> 
     </staticText> 
    </band> 
</title> 
<columnHeader> 
    <band height="20"> 
     <staticText> 
      <reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="a49b18ef-63c3-42bb-ae81-8d7d72ac9086"/> 
      <box leftPadding="0"> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement verticalAlignment="Middle"/> 
      <text><![CDATA[Product Name]]></text> 
     </staticText> 
     <staticText> 
      <reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="c1c70fed-d887-429f-a3b0-ed645517e41f"/> 
      <box leftPadding="0"> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"/> 
      <text><![CDATA[quantity]]></text> 
     </staticText> 
    </band> 
</columnHeader> 
<detail> 
    <band height="20" splitType="Stretch"> 
     <textField> 
      <reportElement x="0" y="0" width="100" height="20" uuid="401436b3-d9a1-40bd-8a1d-dfa3f162f842"/> 
      <box leftPadding="0"> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement verticalAlignment="Middle"/> 
      <textFieldExpression><![CDATA[$F{nameProduct}]]></textFieldExpression> 
     </textField> 
     <textField pattern="###0"> 
      <reportElement x="100" y="0" width="100" height="20" uuid="4447d3b0-6d3e-4795-8b5f-6a09d620448b"/> 
      <box leftPadding="0"> 
       <pen lineWidth="0.25"/> 
       <topPen lineWidth="0.25"/> 
       <leftPen lineWidth="0.25"/> 
       <bottomPen lineWidth="0.25"/> 
       <rightPen lineWidth="0.25"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"/> 
      <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression> 
     </textField> 
    </band> 
</detail> 
</jasperReport> 
+0

hai Peter, próbuję twój przykład, ale dostaję ten błąd net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Błąd oceny wyrażenie dla tekstu źródłowego: ((netto .sf.jasperreports.engine.data.JsonDataSource) $ {P} REPORT_DATA_SOURCE) subDataSource ("ITEMLIST") Spowodowany przez:. java.lang.ClassCastException: net.sf.jasperreports.engine.JREmptyDataSource nie mogą być oddane do net.sf.jasperreports.engine.data.JsonDataSource – user2571094

+0

Przekazujesz EmptyDataSource podczas procesu wypełniania, nie przekazuj żadnych danych do raportu. (JasperFillManager.fillReport (report, paramMap);) (źródło danych jest zdefiniowane w pliku main.jrxml ... zobacz

Powiązane problemy