2015-11-16 66 views
6

我試圖使用Jasper iReport Designer 5.6.0生成報表,其中的表格包含其單元格內的列表。生成的表看起來是這樣的:使用XML數據源的表元素中的Jasper列表元素

Table with nested List

要做到這一點,我使用下面的XML文件作爲數據源:

<report> 
<table> 
    <persons> 
     <person> 
      <id>111</id> 
      <name>John</name> 
      <addresses> 
       <address>Johan's Street 1</address> 
       <address>Johan's Street 2</address> 
       <address>Johan's Street 3</address> 
      </addresses> 
     </person> 
     <person> 
      <id>222</id> 
      <name>Marko</name> 
      <addresses> 
       <address>Marko's Street 1</address> 
       <address>Marko's Street 2</address> 
      </addresses> 
     </person> 
     <person> 
      <id>333</id> 
      <name>Tito</name> 
      <addresses> 
       <address>Tito's Street 1</address> 
       <address>Tito's Street 2</address> 
       <address>Tito's Street 3</address> 
       <address>Tito's Street 4</address> 
      </addresses> 
     </person> 
    </persons> 
</table> 
</report> 

而且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="TableWithList" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2347c131-1884-430a-b77f-59f08f896c8a"> 
<property name="ireport.zoom" value="1.0"/> 
<property name="ireport.x" value="0"/> 
<property name="ireport.y" value="0"/> 
<subDataset name="PersonsTable" uuid="1470ca7f-50f9-4781-9af7-ed3e04841738"> 
    <queryString language="xPath"> 
     <![CDATA[/report/table/persons/person]]> 
    </queryString> 
    <field name="person" class="java.lang.String"> 
     <fieldDescription><![CDATA[child::text()]]></fieldDescription> 
    </field> 
    <field name="id" class="java.lang.String"> 
     <fieldDescription><![CDATA[id]]></fieldDescription> 
    </field> 
    <field name="name" class="java.lang.String"> 
     <fieldDescription><![CDATA[name]]></fieldDescription> 
    </field> 
</subDataset> 
<subDataset name="AddressesList" uuid="5c62f1af-dfc1-49ca-8615-493009f964f6"> 
    <queryString language="xPath"> 
     <![CDATA[/report/table/persons/person/addresses]]> 
    </queryString> 
    <field name="address" class="java.lang.String"> 
     <fieldDescription><![CDATA[address]]></fieldDescription> 
    </field> 
</subDataset> 
<queryString language="xPath"> 
    <![CDATA[/report]]> 
</queryString> 
<detail> 
    <band height="175" splitType="Stretch"> 
     <componentElement> 
      <reportElement key="table" x="0" y="0" width="308" height="50" uuid="d3ef2b70-5a74-4ebf-85f5-36fbcac91938"/> 
      <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd"> 
       <datasetRun subDataset="PersonsTable" uuid="41f6ef78-fb40-4d6f-9998-e31689321fc8"> 
        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/report/table/persons/person")]]></dataSourceExpression> 
       </datasetRun> 
       <jr:column width="122" uuid="3bbeb837-7663-4af6-b2f4-759be2c88102"> 
        <jr:columnHeader height="30" rowSpan="1"> 
         <staticText> 
          <reportElement x="0" y="0" width="122" height="30" uuid="3bc07e10-17f3-45ea-8df9-69a3968dc689"/> 
          <text><![CDATA[NAMES]]></text> 
         </staticText> 
        </jr:columnHeader> 
        <jr:detailCell height="33" rowSpan="1"> 
         <textField> 
          <reportElement x="0" y="0" width="122" height="33" uuid="9d26eb70-a1bc-443c-8fd1-25bcefc6dd58"/> 
          <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression> 
         </textField> 
        </jr:detailCell> 
       </jr:column> 
       <jr:column width="185" uuid="1f779e6e-316b-4442-8e36-c020b7b50d6b"> 
        <jr:columnHeader height="30" rowSpan="1"> 
         <staticText> 
          <reportElement x="0" y="0" width="185" height="30" uuid="59998a31-67a7-4ebe-bd15-6f6d3ec38231"/> 
          <text><![CDATA[ADDRESSES]]></text> 
         </staticText> 
        </jr:columnHeader> 
        <jr:detailCell height="33" rowSpan="1"> 
         <componentElement> 
          <reportElement x="0" y="0" width="185" height="33" uuid="e6ed252e-e104-489d-bf10-b3ffb0f3ad0e"/> 
          <jr:list printOrder="Vertical"> 
           <datasetRun subDataset="AddressesList" uuid="03bc1640-16e7-4111-b48a-0b77e01baefa"> 
            <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/report/table/persons/person/addresses")]]></dataSourceExpression> 
           </datasetRun> 
           <jr:listContents height="33" width="185"> 
            <textField> 
             <reportElement x="0" y="0" width="185" height="33" uuid="eb98e95f-d395-4404-a77b-1660f1de9fd0"/> 
             <textFieldExpression><![CDATA[$F{address}]]></textFieldExpression> 
            </textField> 
           </jr:listContents> 
          </jr:list> 
         </componentElement> 
        </jr:detailCell> 
       </jr:column> 
      </jr:table> 
     </componentElement> 
    </band> 
</detail> 

這就是我得到的結果報告:

enter image description here

是否有可能有表元素中的列表元素?如果是,我做錯了什麼?

回答

6

你靠近

<subDataset name="PersonsTable">正確

<subDataset name="AddressesList">應該

<subDataset name="AddressesList" uuid="5c62f1af-dfc1-49ca-8615-493009f964f6"> 
    <queryString language="xPath"> 
     <![CDATA[/report/table/persons/person/addresses/address]]> 
    </queryString> 
    <field name="address" class="java.lang.String"> 
     <fieldDescription><![CDATA[child::text()]]></fieldDescription> 
    </field> 
</subDataset> 

您需要到達節點address

既然你的人表已經工作,我們只需要修復的dataSourceExpression<jr:list>

<datasetRun subDataset="AddressesList" uuid="c8e1bd7e-b4d8-4e48-8b06-e8fd59846d69"> 
    <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/person/addresses/address")]]></dataSourceExpression> 
</datasetRun> 

那就是它

Result

+1

我無法對此表示謝意:)我在這個問題上困擾了幾天。我無法找到任何示例或教程來解釋如何在表格中使用列表。比你快速和準確的答案。 – mrm

+0

我的榮幸,謝謝你接受答案 –