首页 > 解决方案 > 使用 XmlDataSource 在 iReports 中创建表

问题描述

我正在尝试使用 XmlDataSource 创建一个表,但它既不打印表结构也不显示数据。我正在使用 iReport 4.7.1。请帮忙。

我所做的是:

  1. 创建了一个新的空白报告
  2. 创建了一个 xml 数据源
  3. 选择 /sendDetail/results 作为记录节点
  4. 创建了一个包含选定字段的表

下面是我正在使用的 .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="testReport2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a19f4e39-052a-483f-834a-5546d7626590">
    <property name="ireport.zoom" value="1.5"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#EFF7FF"/>
        </conditionalStyle>
    </style>
    <style name="table 1">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#EFF7FF"/>
        </conditionalStyle>
    </style>
    <subDataset name="Dataset" uuid="601a324d-5c44-4688-b6d4-cf64b3869ba4">
        <queryString language="xPath">
            <![CDATA[/sendDetail/results]]>
        </queryString>
        <field name="results" class="java.lang.String">
            <fieldDescription><![CDATA[child::text()]]></fieldDescription>
        </field>
        <field name="effDate" class="java.lang.String">
            <fieldDescription><![CDATA[effDate]]></fieldDescription>
        </field>
        <field name="period" class="java.lang.String">
            <fieldDescription><![CDATA[period]]></fieldDescription>
        </field>
        <field name="amount" class="java.lang.String">
            <fieldDescription><![CDATA[amount]]></fieldDescription>
        </field>
        <field name="totalAmount" class="java.lang.String">
            <fieldDescription><![CDATA[totalAmount]]></fieldDescription>
        </field>
        <field name="description" class="java.lang.String">
            <fieldDescription><![CDATA[description]]></fieldDescription>
        </field>
    </subDataset>
    <queryString language="xPath">
        <![CDATA[/sendDetail/results]]>
    </queryString>
    <field name="effDate" class="java.lang.String">
        <fieldDescription><![CDATA[effDate]]></fieldDescription>
    </field>
    <field name="period" class="java.lang.String">
        <fieldDescription><![CDATA[period]]></fieldDescription>
    </field>
    <field name="amount" class="java.lang.String">
        <fieldDescription><![CDATA[amount]]></fieldDescription>
    </field>
    <field name="totalAmount" class="java.lang.String">
        <fieldDescription><![CDATA[totalAmount]]></fieldDescription>
    </field>
    <field name="description" class="java.lang.String">
        <fieldDescription><![CDATA[description]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="65" splitType="Stretch">
            <componentElement>
                <reportElement uuid="9bed609a-a1cf-489d-a7b0-d322f0dfc9db" key="table 1" style="table 1" x="0" y="0" width="555" height="65"/>
                <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="Dataset" uuid="33620359-7c04-4c08-9f6c-a18d9c5eb044">
                        <connectionExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_CONNECTION}).subDataSource("/sendDetail/results")]]></connectionExpression>
                    </datasetRun>
                    <jr:column uuid="9403d18c-6eec-447b-b8d2-c4973350060a" width="90">
                        <jr:columnHeader style="table 1_CH" height="30">
                            <staticText>
                                <reportElement uuid="b86bb6ed-aee3-40b0-976e-bc4c23408910" x="0" y="0" width="90" height="30"/>
                                <textElement/>
                                <text><![CDATA[effDate]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20">
                            <textField>
                                <reportElement uuid="de8b3535-74da-41b4-9f1d-7b66164bb0dc" x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{effDate}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column uuid="ee9a667a-8916-4d87-87b7-e256e90e7682" width="90">
                        <jr:columnHeader style="table 1_CH" height="30">
                            <staticText>
                                <reportElement uuid="3d173f1a-776d-415b-b241-ba4b4416a829" x="0" y="0" width="90" height="30"/>
                                <textElement/>
                                <text><![CDATA[period]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20">
                            <textField>
                                <reportElement uuid="9d6db0e4-6b13-4af3-9060-9057246e4e63" x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{period}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column uuid="d178a594-0faf-4a05-969c-2e17e65e9121" width="90">
                        <jr:columnHeader style="table 1_CH" height="30">
                            <staticText>
                                <reportElement uuid="41a37b97-55be-45cb-8279-354d460ded19" x="0" y="0" width="90" height="30"/>
                                <textElement/>
                                <text><![CDATA[amount]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20">
                            <textField>
                                <reportElement uuid="9e7714bc-3f8b-4d1e-8f77-ab8e9b14617f" x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{amount}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column uuid="e2cdad6a-fd55-4b35-9fc9-c6c93f8d3c0e" width="90">
                        <jr:columnHeader style="table 1_CH" height="30">
                            <staticText>
                                <reportElement uuid="b2d16a10-e73a-404d-8435-445664399482" x="0" y="0" width="90" height="30"/>
                                <textElement/>
                                <text><![CDATA[totalAmount]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20">
                            <textField>
                                <reportElement uuid="ea85d3b8-23e4-4c24-a611-d099b4a6d242" x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{totalAmount}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column uuid="e4a14c01-5159-41e2-b8eb-5fa333c8cc6c" width="90">
                        <jr:columnHeader style="table 1_CH" height="30">
                            <staticText>
                                <reportElement uuid="0beaa2b0-6b2c-46d5-97ff-36a1888bd7b8" x="0" y="0" width="90" height="30"/>
                                <textElement/>
                                <text><![CDATA[description]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20">
                            <textField>
                                <reportElement uuid="9b34870c-6013-47b5-af29-3380fbda1b25" x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

下面是xml文件:

<sendDetail>    
<results>
    <effDate>2015-10-20</effDate>
    <period>DEC 1;2015 TO FEB 29;2016</period>
    <amount>177.48</amount>
    <totalAmount>177.48</totalAmount>
    <description>Premium</description>
</results>
<results>
    <effDate>2015-10-20</effDate>
    <period>DEC 1;2015 TO FEB 29;2016</period>
    <amount>-177.48</amount>
    <totalAmount>0</totalAmount>
    <description>Premium Cancellation</description>
</results>
<results>
    <effDate>2015-07-20</effDate>
    <period>SEP 1;2015 TO NOV 30;2015</period>
    <amount>-177.48</amount>
    <totalAmount>0</totalAmount>
    <description>Premium Cancellation</description>
</results>
<results>
    <effDate>2015-07-20</effDate>
    <period>SEP 1;2015 TO NOV 30;2015</period>
    <amount>177.48</amount>
    <totalAmount>177.48</totalAmount>
    <description>Premium</description>
</results>
</sendDetail>

标签: jasper-reports

解决方案


你需要:

  1. 将您的主要数据集更改queryString为:

    <queryString language="xPath">
        <![CDATA[/]]>
    </queryString>
    
  2. 将表的datasetRun声明替换为:

    <datasetRun subDataset="Dataset" uuid="33620359-7c04-4c08-9f6c-a18d9c5eb044">
        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/sendDetail/results")]]></dataSourceExpression>
    </datasetRun>
    

推荐阅读