Java 在Jasper report中,如何使用XML数据源的子报表创建报表pdf

Java 在Jasper report中,如何使用XML数据源的子报表创建报表pdf,java,xml,jasper-reports,datasource,subreport,Java,Xml,Jasper Reports,Datasource,Subreport,我的报告包含两个子报告,其中XML为: 它是根据这个链接 查看您的stackoverflow,我修改了我的主报告和报告的java构建。如何传递XML数据源?我仍然得到空白报告。在Java代码中,您将两个子报告添加为参数subReport\u 1和subReport\u 2,但主报告仅声明一个名为“subReport\u DIR”的参数 我做了一些与您的Java代码类似的事情(总结): 您必须在主jrxml中将子报表声明为如下参数: <parameter name="Subreport_1"

我的报告包含两个子报告,其中XML为: 它是根据这个链接


查看您的stackoverflow,我修改了我的主报告和报告的java构建。如何传递XML数据源?我仍然得到空白报告。

在Java代码中,您将两个子报告添加为参数
subReport\u 1
subReport\u 2
,但主报告仅声明一个名为“subReport\u DIR”的参数

我做了一些与您的Java代码类似的事情(总结):

您必须在主jrxml中将子报表声明为如下参数:

<parameter name="Subreport_1" class="java.lang.Object"/>
<parameter name="Subreport_2" class="java.lang.Object"/>

...

<subreport runToBottom="false">
    <reportElement uuid="ce3bf2a2-9131-4d75-800b-25d4a71791c3" 
         positionType="Float"    isPrintRepeatedValues="false" 
         x="0" y="179" width="468" height="28" isRemoveLineWhenBlank="true"/>
    <subreportParameter name="XML_DATA_DOCUMENT">
        <subreportParameterExpression><![CDATA[$P{XML_DATA_DOCUMENT}]]></subreportParameterExpression>
    </subreportParameter>
    <subreportExpression><![CDATA[$P{Subreport_1}]]></subreportExpression>
</subreport>

...
并且可以删除以下参数,因为它似乎未在代码中使用:

<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\Users\\bferrao\\Desktop\\jasper\\"]]></defaultValueExpression>
</parameter>


我在代码的您部分添加了注释,但通常我会说它是正确的。我有另一个逻辑,因为我可以有任意数量的子报告。我只是用某种模式编译文件夹中的所有文件,然后将它们添加到参数映射中。然后我编译主程序并使用FillReportToStream,因为我想在另一个Java部件中使用它。稍后我将检查数据源是如何在子报告中传输的,也许您的问题也在那里。@bosco1好的,我现在没有代码,但稍后我将尝试研究它并更新我的答案。同时,如果有帮助,您可以查看本文:查看您的stackoverflow,我已经修改了我的主报告和报告的java构建。如何传递XML数据源?我仍然收到一份空白报告。对于查询字符串语言,我有
xPath
而不是
xpath2
,这很好,请告诉我如何继续。我得到了一个空白的报告。看着你的stackoverflow,我修改了我的主报告和报告的java构建。如何传递XML数据源?我仍然收到空白报告。仅供参考,一般来说,最好不要修改初始问题,因为更改后的答案可能完全错误。如果您想要更新,最好在这里(或在答案上)进行注释,而不是更改初始代码。否则(但对于较短的问题),您可以添加“更新:”部分。
<?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="TransactionReport_subreport1" pageWidth="802" pageHeight="555" orientation="Landscape" columnWidth="802" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="588e5172-b1db-40c6-927f-aca7a0b2876c">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="90"/>
    <queryString language="xpath2">
        <![CDATA[/Report/TransactionTypeRowSet/Row]]>
    </queryString>
    <field name="runDate" class="java.lang.String">
        <fieldDescription><![CDATA[ancestor::Report/@runDate]]></fieldDescription>
    </field>
    <field name="TransactionType" class="java.lang.String">
        <fieldDescription><![CDATA[TransactionType]]></fieldDescription>
    </field>
    <field name="TransactionNo" class="java.lang.String">
        <fieldDescription><![CDATA[TransactionNo]]></fieldDescription>
    </field>
    <field name="TotalAmount" class="java.lang.String">
        <fieldDescription><![CDATA[TotalAmount]]></fieldDescription>
    </field>
    <field name="TotalFee" class="java.lang.String">
        <fieldDescription><![CDATA[TotalFee]]></fieldDescription>
    </field>
    <field name="TotalCommission" class="java.lang.String">
        <fieldDescription><![CDATA[TotalCommission]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch">
            <textField>
                <reportElement x="128" y="0" width="100" height="20" uuid="aa03e739-6e8c-4566-99ea-2d15b27479f3"/>
                <textFieldExpression><![CDATA[$F{runDate}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="10" y="0" width="45" height="20" uuid="59bf27b5-c260-4aa8-9347-f314619b73b4"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Run Date]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="22" splitType="Stretch">
            <staticText>
                <reportElement x="10" y="2" width="100" height="20" uuid="a636f8fb-9dc5-4d5a-be72-faf2e569f1ce"/>
                <text><![CDATA[Transaction Type]]></text>
            </staticText>
            <staticText>
                <reportElement x="128" y="2" width="100" height="20" uuid="066d175f-783d-46a9-bb9b-15c6740aab94"/>
                <text><![CDATA[Transaction No.]]></text>
            </staticText>
            <staticText>
                <reportElement x="244" y="2" width="100" height="20" uuid="20d8dbc0-d94a-4aa2-8f20-c130d5d72937"/>
                <text><![CDATA[Total Amount]]></text>
            </staticText>
            <staticText>
                <reportElement x="358" y="2" width="100" height="20" uuid="1f47dc2d-5849-4440-8861-1523473b7ef7"/>
                <text><![CDATA[Total Fee]]></text>
            </staticText>
            <staticText>
                <reportElement x="470" y="2" width="100" height="20" uuid="48309245-2d2a-43b4-bc10-7f91e1e64612"/>
                <text><![CDATA[Total Commission]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="10" y="0" width="100" height="20" uuid="44951d02-8cdb-4c98-b7d5-e58c6e46e807"/>
                <textFieldExpression><![CDATA[$F{TransactionType}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="128" y="0" width="100" height="20" uuid="abaeec97-a833-4731-9272-4f52972a168a"/>
                <textFieldExpression><![CDATA[$F{TransactionNo}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="244" y="0" width="100" height="20" uuid="ba6461a1-0042-49ce-8cc5-fef146fd2602"/>
                <textFieldExpression><![CDATA[$F{TotalAmount}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="358" y="0" width="100" height="20" uuid="e28079e5-ad93-4c96-a030-de8da6d045e3"/>
                <textFieldExpression><![CDATA[$F{TotalFee}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="470" y="0" width="100" height="20" uuid="4d9482db-e1dc-4e80-8547-87446e7bad0c"/>
                <textFieldExpression><![CDATA[$F{TotalCommission}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>
<?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="TransactionReport_subreport2" pageWidth="1500" pageHeight="555" orientation="Landscape" columnWidth="1500" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="f22b3fa9-da70-48b3-a92b-7d9d5ce3c988">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString language="xpath2">
        <![CDATA[/Report/TransactionRowSet/Row]]>
    </queryString>
    <field name="runDate" class="java.lang.String">
        <fieldDescription><![CDATA[ancestor::Report/@runDate]]></fieldDescription>
    </field>
    <field name="TransactionId" class="java.lang.String">
        <fieldDescription><![CDATA[TransactionId]]></fieldDescription>
    </field>
    <field name="Date" class="java.lang.String">
        <fieldDescription><![CDATA[Date]]></fieldDescription>
    </field>
    <field name="Time" class="java.lang.String">
        <fieldDescription><![CDATA[Time]]></fieldDescription>
    </field>
    <field name="TransactionType" class="java.lang.String">
        <fieldDescription><![CDATA[TransactionType]]></fieldDescription>
    </field>
    <field name="TransactionStatus" class="java.lang.String">
        <fieldDescription><![CDATA[TransactionStatus]]></fieldDescription>
    </field>
    <field name="TransactionAmount" class="java.lang.String">
        <fieldDescription><![CDATA[TransactionAmount]]></fieldDescription>
    </field>
    <field name="TransactionFee" class="java.lang.String">
        <fieldDescription><![CDATA[TransactionFee]]></fieldDescription>
    </field>
    <field name="TransactionCommission" class="java.lang.String">
        <fieldDescription><![CDATA[TransactionCommission]]></fieldDescription>
    </field>
    <field name="WalletId" class="java.lang.String">
        <fieldDescription><![CDATA[WalletId]]></fieldDescription>
    </field>
    <field name="UserType" class="java.lang.String">
        <fieldDescription><![CDATA[UserType]]></fieldDescription>
    </field>
    <field name="AccountType" class="java.lang.String">
        <fieldDescription><![CDATA[AccountType]]></fieldDescription>
    </field>
    <field name="MobileNo" class="java.lang.String">
        <fieldDescription><![CDATA[MobileNo]]></fieldDescription>
    </field>
    <field name="Amount" class="java.lang.String">
        <fieldDescription><![CDATA[Amount]]></fieldDescription>
    </field>
    <field name="Type" class="java.lang.String">
        <fieldDescription><![CDATA[Type]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch">
            <textField>
                <reportElement x="110" y="0" width="100" height="20" uuid="4290617b-8c2a-4530-b0e5-106dd9c35135"/>
                <textFieldExpression><![CDATA[$F{runDate}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="5" y="0" width="45" height="20" uuid="eb5b3fbe-99a8-4142-b70c-121250521ccd"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Run Date]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="22" splitType="Stretch">
            <staticText>
                <reportElement x="5" y="2" width="100" height="20" uuid="041974c0-9b9b-47a1-86b3-3dc3ce64f70f"/>
                <text><![CDATA[Transaction Id]]></text>
            </staticText>
            <staticText>
                <reportElement x="110" y="2" width="100" height="20" uuid="5c7f1106-d65e-4cca-9f82-92d7adb02a7d"/>
                <text><![CDATA[Date]]></text>
            </staticText>
            <staticText>
                <reportElement x="215" y="2" width="100" height="20" uuid="29432958-c1b2-4164-a6d2-61cda5131091"/>
                <text><![CDATA[Time]]></text>
            </staticText>
            <staticText>
                <reportElement x="320" y="2" width="100" height="20" uuid="b84c859b-44e2-4c58-ac50-30b6db62b752"/>
                <text><![CDATA[Transaction Type]]></text>
            </staticText>
            <staticText>
                <reportElement x="425" y="2" width="100" height="20" uuid="e77d633a-d760-4f5c-8d88-95fb91afab21"/>
                <text><![CDATA[Transaction Status]]></text>
            </staticText>
            <staticText>
                <reportElement x="530" y="2" width="100" height="20" uuid="0b4a24b8-2d64-42a2-9875-fd617ddf663e"/>
                <text><![CDATA[Transaction Amount]]></text>
            </staticText>
            <staticText>
                <reportElement x="634" y="2" width="100" height="20" uuid="c6610ab4-f4b0-445a-8f6c-d8e123ca9562"/>
                <text><![CDATA[Fee]]></text>
            </staticText>
            <staticText>
                <reportElement x="737" y="2" width="100" height="20" uuid="2446940d-ecda-47ff-90e1-1889898b8857"/>
                <text><![CDATA[Commission]]></text>
            </staticText>
            <staticText>
                <reportElement x="840" y="2" width="100" height="20" uuid="902717d0-bd96-44dd-9195-ceca8d2ca466"/>
                <text><![CDATA[Wallet Id]]></text>
            </staticText>
            <staticText>
                <reportElement x="943" y="2" width="100" height="20" uuid="246f09b5-f738-479b-819d-d8648f8e13a0"/>
                <text><![CDATA[User Type]]></text>
            </staticText>
            <staticText>
                <reportElement x="1047" y="2" width="100" height="20" uuid="68970e38-f49c-4a9b-8bf8-486298474fff"/>
                <text><![CDATA[Account Type]]></text>
            </staticText>
            <staticText>
                <reportElement x="1152" y="2" width="100" height="20" uuid="31714d60-87d2-41bd-bd74-60ea96d9adcf"/>
                <text><![CDATA[MobileNo]]></text>
            </staticText>
            <staticText>
                <reportElement x="1256" y="2" width="100" height="20" uuid="609bf515-758a-4cc6-b23a-ed603f036f12"/>
                <text><![CDATA[Amount]]></text>
            </staticText>
            <staticText>
                <reportElement x="1360" y="2" width="100" height="20" uuid="b8afcb53-8306-4569-8597-0f3213a09872"/>
                <text><![CDATA[Type]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="5" y="0" width="100" height="20" uuid="29895b19-71b2-43ec-aa18-c5d23d3cece4"/>
                <textFieldExpression><![CDATA[$F{TransactionId}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="110" y="0" width="100" height="20" uuid="1c42caa6-a106-45ff-96ec-65b4873c016d"/>
                <textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="215" y="0" width="100" height="20" uuid="6d85cc83-3830-44af-ac6d-517bb107d3ac"/>
                <textFieldExpression><![CDATA[$F{Time}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="320" y="0" width="100" height="20" uuid="aee3e08d-744d-4578-89da-c5296237510e"/>
                <textFieldExpression><![CDATA[$F{TransactionType}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="425" y="0" width="100" height="20" uuid="20ca50d3-c882-43f2-b3c0-334e7b7c3206"/>
                <textFieldExpression><![CDATA[$F{TransactionStatus}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="530" y="0" width="100" height="20" uuid="3e0418dc-e327-4d94-ac4a-831e6cfdbcde"/>
                <textFieldExpression><![CDATA[$F{TransactionAmount}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="634" y="0" width="100" height="20" uuid="ccec42b7-84e7-43ea-af56-3ee5af5b72cf"/>
                <textFieldExpression><![CDATA[$F{TransactionFee}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="737" y="0" width="100" height="20" uuid="a18fc12e-467e-4ecc-b983-58d5eebb3bf3"/>
                <textFieldExpression><![CDATA[$F{TransactionCommission}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="840" y="0" width="100" height="20" uuid="67a5658d-56ee-4f56-ac28-d5954febf852"/>
                <textFieldExpression><![CDATA[$F{WalletId}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="943" y="0" width="100" height="20" uuid="9c032b11-4fc6-4b1b-89c4-83d508db9475"/>
                <textFieldExpression><![CDATA[$F{UserType}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="1047" y="0" width="100" height="20" uuid="c8a4c818-6eb7-4186-b996-75b0fd9400b2"/>
                <textFieldExpression><![CDATA[$F{AccountType}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="1152" y="0" width="100" height="20" uuid="dd8671c2-13d1-4b7a-829d-ded93701fc13"/>
                <textFieldExpression><![CDATA[$F{MobileNo}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="1256" y="0" width="100" height="20" uuid="48fc8c4f-014d-493c-8b75-63b60dfc7ddc"/>
                <textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="1360" y="0" width="100" height="20" uuid="006ea1f0-30a3-49b8-8c98-e5702146e9c6"/>
                <textFieldExpression><![CDATA[$F{Type}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>
JasperPrint print = new JasperPrint();
JRPdfExporter exporter = new JRPdfExporter();
JasperDesign design, design1;
JasperReport report, report1;
JasperReport jasperReport;
JasperDesign jd1;


jd1 = JRXmlLoader.load("C:/Users/bferrao/Desktop/jasper/TransactionReport_subreport1.jrxml");
jasperReport = JasperCompileManager.compileReport(jd1);

Map parameters = new HashMap();
parameters.put("Subreport_1", jasperReport);

design = JRXmlLoader.load("C:/Users/bferrao/Desktop/jasper/TransactionReport_subreport2.jrxml");
report = JasperCompileManager.compileReport(design);
parameters.put("Subreport_2", report);

Document document = JRXmlUtils.parse(new File("C:/Users/bferrao/Desktop/jasper/TransactionReport.xml"));
parameters.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);

JRXmlDataSource jrxmlds = new JRXmlDataSource(new File("C:/Users/bferrao/Desktop/jasper/TransactionReport.xml"), "/Report");
design1 = JRXmlLoader.load("C:/Users/bferrao/Desktop/jasper/TransactionReport.jrxml");
report1 = JasperCompileManager.compileReport(design1);
JasperFillManager.fillReportToFile(report1, "C:/Users/bferrao/Desktop/jasper/master_report.jrprint", parameters, jrxmlds);

exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "D:\\temp\\Report3.pdf");
exporter.exportReport();
   // first compile the sub reports
   jasperReport = JasperCompileManager.compileReport(jd1);
   jasperReport = JasperCompileManager.compileReport(jd1);
   report = JasperCompileManager.compileReport(design);

   // then add the compiled reports to your parameters
   Map parameters = new HashMap();
   parameters.put("Subreport_1", jasperReport);
   parameters.put("Subreport_2", report);

   // and finally send the sub-reports to the main report as parameters
   JasperFillManager.fillReportToFile(report1, "D:/temp/master_report.jrprint", parameters, jrxmlds);
<parameter name="Subreport_1" class="java.lang.Object"/>
<parameter name="Subreport_2" class="java.lang.Object"/>

...

<subreport runToBottom="false">
    <reportElement uuid="ce3bf2a2-9131-4d75-800b-25d4a71791c3" 
         positionType="Float"    isPrintRepeatedValues="false" 
         x="0" y="179" width="468" height="28" isRemoveLineWhenBlank="true"/>
    <subreportParameter name="XML_DATA_DOCUMENT">
        <subreportParameterExpression><![CDATA[$P{XML_DATA_DOCUMENT}]]></subreportParameterExpression>
    </subreportParameter>
    <subreportExpression><![CDATA[$P{Subreport_1}]]></subreportExpression>
</subreport>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\Users\\bferrao\\Desktop\\jasper\\"]]></defaultValueExpression>
</parameter>