2016-09-08 7 views
2

주 보고서의 제목 영역에있는 하위 보고서에 데이터를 채우는 데 문제가 있습니다. 데이터는 기본 보고서의 세부 밴드의 하위 보고서에 올바르게 채워집니다. JSON 쿼리에서 잘못 될 수있는 부분을 찾으려고합니다. 어떤 도움을 주셔서 감사합니다.Title band에 포함되어있을 때 JSON 데이터 소스의 항목 하나만 표시하는 Jasper 하위 보고서

enter image description here

JSON 샘플 데이터 소스 (expenses.json)

{ 
    "expenses": { 
    "date": "8 Sep 2016", 
    "accounts": [ 
     { 
     "title": "XYZ Corp (111)", 
     "accountName": "XYZ Corp", 
     "accountNumber": "111", 
     "transactions": [ 
      { 
      "date": "21 Jun 2016", 
      "name": "gas", 
      "price": "17.50" 
      }, 
      { 
      "date": "12 May 2016", 
      "name": "shopping", 
      "price": "111.99" 
      }, 
      { 
      "date": "30 Apr 2016", 
      "name": "movies", 
      "price": "7.50" 
      } 
     ] 
     }, 
     { 
     "title": "ABC Corp (222)", 
     "accountName": "ABC Corp", 
     "accountNumber": "222", 
     "transactions": [ 
      { 
      "date": "2 Jun 2016", 
      "name": "coffee", 
      "price": "3.25" 
      }, 
      { 
      "date": "21 May 2016", 
      "name": "tea", 
      "price": "1.90" 
      }, 
      { 
      "date": "20 Apr 2016", 
      "name": "juice", 
      "price": "4.50" 
      } 
     ] 
     } 
    ] 
    } 
} 

주 보고서 (main.jrxml)

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.3.0 --> 
<!-- 2016-09-08T14:41:09 --> 
<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="expenses_main" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8cf0f1cb-ccad-41ba-aa8a-44abd597de54"> 
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="expenses json data"/> 
    <queryString language="json"> 
     <![CDATA[expenses.accounts]]> 
    </queryString> 
    <field name="title" class="java.lang.String"> 
     <fieldDescription><![CDATA[title]]></fieldDescription> 
    </field> 
    <field name="date" class="java.lang.String"> 
     <fieldDescription><![CDATA[expenses.date]]></fieldDescription> 
    </field> 
    <background> 
     <band splitType="Stretch"/> 
    </background> 
    <title> 
     <band height="71" splitType="Stretch"> 
      <staticText> 
       <reportElement x="2" y="0" width="201" height="16" uuid="2e947a01-8b6b-4169-beaa-6b2dac0d4141"/> 
       <text><![CDATA[Expense Report]]></text> 
      </staticText> 
      <subreport> 
       <reportElement x="2" y="44" width="550" height="22" uuid="1d364987-9047-4660-bd58-8a2bcfc8493d"/> 
       <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()]]></dataSourceExpression> 
       <subreportExpression><![CDATA["/Users/arul/JaspersoftWorkspaceV2/reports/sub1.jasper"]]></subreportExpression> 
      </subreport> 
      <textField> 
       <reportElement x="442" y="0" width="100" height="16" uuid="1cfbe11b-4f85-4bad-9280-d8712b80a65c"/> 
       <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression> 
      </textField> 
      <staticText> 
       <reportElement x="410" y="1" width="31" height="15" uuid="ee159651-5d7b-4e0e-a52a-cb71338891cc"/> 
       <text><![CDATA[Date: ]]></text> 
      </staticText> 
     </band> 
    </title> 
    <detail> 
     <band height="54" splitType="Stretch"> 
      <textField> 
       <reportElement x="2" y="10" width="550" height="20" uuid="3b6e1d4b-2a58-4684-b334-396807db1169"/> 
       <textFieldExpression><![CDATA["Transactions for " + $F{title}]]></textFieldExpression> 
      </textField> 
      <subreport> 
       <reportElement x="2" y="30" width="545" height="20" uuid="10fde2aa-2b0b-4c1c-90d5-410f2fb9369b"/> 
       <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("transactions")]]></dataSourceExpression> 
       <subreportExpression><![CDATA["/Users/arul/JaspersoftWorkspaceV2/reports/sub2.jasper"]]></subreportExpression> 
      </subreport> 
     </band> 
    </detail> 
</jasperReport> 

Subreport1 (sub1.jrxml)

0 Subreport2 (sub2.jrxml)

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.3.0 --> 
<!-- 2016-09-08T14:43:55 --> 
<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="expenses_subreport" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="2e39bfaf-e964-4b5d-847e-6517de5718d4"> 
    <field name="date" class="java.lang.String"/> 
    <field name="name" class="java.lang.String"/> 
    <field name="price" class="java.lang.String"/> 
    <background> 
     <band splitType="Stretch"/> 
    </background> 
    <columnHeader> 
     <band height="20" splitType="Stretch"> 
      <staticText> 
       <reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#DBD7D7" uuid="8e073b78-91de-4819-886a-2527d5634067"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textElement> 
        <font isBold="true"/> 
       </textElement> 
       <text><![CDATA[Date]]></text> 
      </staticText> 
      <staticText> 
       <reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#DBD7D7" uuid="e7e7d858-05eb-4fda-9ae6-d998b4a51fd8"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textElement> 
        <font isBold="true"/> 
       </textElement> 
       <text><![CDATA[Name]]></text> 
      </staticText> 
      <staticText> 
       <reportElement mode="Opaque" x="200" y="0" width="100" height="20" backcolor="#DBD7D7" uuid="fef220d6-4f85-4fd7-8017-5b64aff64666"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textElement> 
        <font isBold="true"/> 
       </textElement> 
       <text><![CDATA[Price]]></text> 
      </staticText> 
     </band> 
    </columnHeader> 
    <detail> 
     <band height="20" splitType="Stretch"> 
      <textField> 
       <reportElement x="0" y="0" width="100" height="20" uuid="fd021427-d26c-4c9c-8c71-c9f136f11a66"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression> 
      </textField> 
      <textField> 
       <reportElement x="100" y="0" width="100" height="20" uuid="b6cd9815-ba45-49dd-be46-60a6a835d3fd"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression> 
      </textField> 
      <textField> 
       <reportElement x="200" y="0" width="100" height="20" uuid="cf3e397e-5546-4490-a88c-7211f124475e"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression> 
      </textField> 
     </band> 
    </detail> 
</jasperReport> 

PDF 재스퍼를 사용하여 생성하는

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.3.0 --> 
<!-- 2016-09-08T14:43:35 --> 
<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="expenses_summary" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="d8ef4ad1-3a01-4394-a78c-17f9d95f98de"> 
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="expenses json data"/> 
    <queryString language="JSON"> 
     <![CDATA[expenses.accounts]]> 
    </queryString> 
    <field name="accountName" class="java.lang.String"> 
     <fieldDescription><![CDATA[accountName]]></fieldDescription> 
    </field> 
    <field name="accountNumber" class="java.lang.String"> 
     <fieldDescription><![CDATA[accountNumber]]></fieldDescription> 
    </field> 
    <background> 
     <band splitType="Stretch"/> 
    </background> 
    <title> 
     <band height="21"> 
      <staticText> 
       <reportElement x="0" y="0" width="100" height="20" uuid="258b2395-7fd1-4df0-b000-e2bfd2620fab"/> 
       <textElement> 
        <font isBold="true"/> 
       </textElement> 
       <text><![CDATA[Account Summary]]></text> 
      </staticText> 
     </band> 
    </title> 
    <columnHeader> 
     <band height="21" splitType="Stretch"> 
      <staticText> 
       <reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#DBD7D7" uuid="b04ff630-de8e-4a9b-9ea0-d402d4734ae7"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <text><![CDATA[Account Name]]></text> 
      </staticText> 
      <staticText> 
       <reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#DBD7D7" uuid="6f0d406e-8d99-4364-888c-38bc6c0fa8c6"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <text><![CDATA[Account Number]]></text> 
      </staticText> 
     </band> 
    </columnHeader> 
    <detail> 
     <band height="21" splitType="Stretch"> 
      <textField> 
       <reportElement x="0" y="0" width="100" height="20" uuid="c140795a-02a5-4334-9c5f-dc50e8a8ff6f"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textFieldExpression><![CDATA[$F{accountName}]]></textFieldExpression> 
      </textField> 
      <textField> 
       <reportElement x="100" y="0" width="100" height="20" uuid="ac7a5340-4c86-45d2-a932-b7b0e347a421"/> 
       <box> 
        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       </box> 
       <textFieldExpression><![CDATA[$F{accountNumber}]]></textFieldExpression> 
      </textField> 
     </band> 
    </detail> 
</jasperReport> 

은 6.3.0 자바 API를보고합니다. 귀하는 귀하의 초기 JSON 쿼리

<queryString language="json"> 
     <![CDATA[expenses.accounts]]> 
</queryString> 

Date 깊은에가는

import net.sf.jasperreports.engine.JasperExportManager; 
import net.sf.jasperreports.engine.JasperFillManager; 
import net.sf.jasperreports.engine.JasperPrint; 
import net.sf.jasperreports.engine.JasperReport; 
import net.sf.jasperreports.engine.query.JsonQueryExecuterFactory; 
import net.sf.jasperreports.engine.util.JRLoader; 

import java.io.FileInputStream; 
import java.util.HashMap; 
import java.util.Map; 

public class JasperTest { 
    public static void main(String[] args) throws Exception { 
    JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile("/Users/arul/JaspersoftWorkspaceV2/reports/main.jasper"); 
    Map<String, Object> jasperParameters = new HashMap<String, Object>(); 
    jasperParameters.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, new FileInputStream("/Users/arul/JaspersoftWorkspaceV2/data/expenses.json")); 
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameters); 
    JasperExportManager.exportReportToPdfFile(jasperPrint, "report.pdf"); 
    } 
} 

답변

1

이 범위를 벗어납니다.

이 솔루션은

<queryString language="json"> 
     <![CDATA[expenses]]> 
</queryString> 

하위 보고서 1 (sub1.jasper가) 지금 (subDataSource("accounts") 표현은 이제 1 개 세부 밴드 기록이있을 것이다 그러나

<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("accounts")]]></dataSourceExpression> 

으로 호출 비용 범위에 머물 것입니다 비용),이 문제를 해결하기 위해 다른 하위 보고서를 소개 할 수 있습니다.

대신

직접 거래에 데이터 소스와 하위 보고서 2 ( sub2.jasper)이 제목을 표시합니다 subDataSource("accounts")을 계정에 다시 반복 된 보고서 1_2 ( sub1_2.jasper)를 소개 하위 보고서 2를 호출 포함 .subDataSource("transactions")


주 보고서 (main.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="expenses_main" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8cf0f1cb-ccad-41ba-aa8a-44abd597de54"> 
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Json"/> 
    <queryString language="json"> 
     <![CDATA[expenses]]> 
    </queryString> 
    <field name="date" class="java.lang.String"> 
     <fieldDescription><![CDATA[date]]></fieldDescription> 
    </field> 
    <background> 
     <band splitType="Stretch"/> 
    </background> 
    <title> 
     <band height="71" splitType="Stretch"> 
      <staticText> 
       <reportElement x="2" y="0" width="201" height="16" uuid="2e947a01-8b6b-4169-beaa-6b2dac0d4141"/> 
       <text><![CDATA[Expense Report]]></text> 
      </staticText> 
      <subreport> 
       <reportElement x="2" y="44" width="550" height="22" uuid="1d364987-9047-4660-bd58-8a2bcfc8493d"/> 
       <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("accounts")]]></dataSourceExpression> 
       <subreportExpression><![CDATA["sub1.jasper"]]></subreportExpression> 
      </subreport> 
      <textField> 
       <reportElement x="442" y="0" width="100" height="16" uuid="1cfbe11b-4f85-4bad-9280-d8712b80a65c"/> 
       <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression> 
      </textField> 
      <staticText> 
       <reportElement x="410" y="1" width="31" height="15" uuid="ee159651-5d7b-4e0e-a52a-cb71338891cc"/> 
       <text><![CDATA[Date: ]]></text> 
      </staticText> 
     </band> 
    </title> 
    <detail> 
     <band height="36" splitType="Stretch"> 
      <subreport isUsingCache="false"> 
       <reportElement x="2" y="10" width="545" height="20" uuid="10fde2aa-2b0b-4c1c-90d5-410f2fb9369b"/> 
       <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("accounts")]]></dataSourceExpression> 
       <subreportExpression><![CDATA["sub1_2.jasper"]]></subreportExpression> 
      </subreport> 
     </band> 
    </detail> 
</jasperReport> 

하위 보고서 1_2 (sub1_2.jasper)

<?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="expenses_subreport" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="2e39bfaf-e964-4b5d-847e-6517de5718d4"> 
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Json"/> 
    <field name="title" class="java.lang.String"/> 
    <background> 
     <band splitType="Stretch"/> 
    </background> 
    <detail> 
     <band height="40" splitType="Stretch"> 
      <property name="com.jaspersoft.studio.unit.height" value="pixel"/> 
      <textField> 
       <reportElement x="2" y="1" width="550" height="20" uuid="934d546e-aeeb-4760-b100-cbefed73c420"/> 
       <textFieldExpression><![CDATA["Transactions for " + $F{title}]]></textFieldExpression> 
      </textField> 
      <subreport isUsingCache="false"> 
       <reportElement x="5" y="16" width="545" height="20" uuid="1b8b0487-9dfa-4b82-bfb1-13009cb74ece"/> 
       <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("transactions")]]></dataSourceExpression> 
       <subreportExpression><![CDATA["sub2.jasper"]]></subreportExpression> 
      </subreport> 
     </band> 
    </detail> 
</jasperReport> 

하위 보고서 1, 2 (sub1.jrxmlsub2.jrxml)

번호가 변경


결과

output

+1

Peter, 고맙습니다. –

+1

@ArulDhesiaseelan 메모로 jasper 보고서 예제 가격으로 올바른 클래스를 BigDecimal 또는 Double (포맷터를 json으로보아야 함)로 사용했지만,이 방법으로 패턴을 적용하고 다른 형식으로 내보낼 때 좋은 결과를 얻을 수 있습니다. –