简体   繁体   English

从主报告传递参数时,JasperReports 子报告不显示

[英]JasperReports subreport not displaying when parameters passed from main report

I have one main report where parameters from java code by Map object.我有一个主要报告,其中来自 Map 对象的 java 代码的参数。 In main report including one subreport and trying to pass main report parameter.在主报告中包括一个子报告并试图传递主报告参数。

The main report and subreport working fine separately but main report does not show subreport.主报表和子报表分别工作正常,但主报表不显示子报表。

Invoice.jrxml (main report): Invoice.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="ma" pageWidth="683" pageHeight="899" columnWidth="643" leftMargin="20" rightMargin="20" topMargin="1" bottomMargin="1" isSummaryNewPage="true" isSummaryWithPageHeaderAndFooter="true" uuid="51ff5174-9cb9-4d22-8e11-cd3fe9392bbd">
    <property name="net.sf.jasperreports.awt.ignore.missing.font" value="true"/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <parameter name="filePath" class="java.lang.String"/>
    <parameter name="imagePath" class="java.lang.String"/>
    <parameter name="orderId" class="java.lang.String"/>
    <parameter name="orderNum" class="java.lang.String"/>
    <parameter name="orderDate" class="java.util.Date"/>
    <parameter name="billingAddress" class="java.lang.String"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <pageHeader>
        <band height="121">
            <image>
                <reportElement x="8" y="0" width="314" height="50" uuid="c072a055-ead7-4e63-b87f-3d7163cf2f2b"/>
                <imageExpression><![CDATA[$P{imagePath}]]></imageExpression>
            </image>
            <textField>
                <reportElement x="240" y="37" width="166" height="20" uuid="7619dfad-f0f5-4c80-9f73-625cfb24d1a5"/>
                <textElement textAlignment="Center">
                    <font size="14" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["INVOICE"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="17" y="61" width="68" height="20" uuid="9d49ad56-14a9-4214-a615-3ce706cdcb0f"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["ORDER NO. "]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="85" y="61" width="7" height="20" uuid="8d44e405-3d27-421f-bd07-eeac5d84699c"/>
                <textElement>
                    <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
                </textElement>
                <textFieldExpression><![CDATA[":"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="92" y="61" width="100" height="20" uuid="5eee8a2d-9c15-4c36-887b-9baf8009901e"/>
                <textFieldExpression><![CDATA[$P{orderNum}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="488" y="61" width="79" height="20" uuid="d6eba4a3-4349-4220-ac2a-8e7ac45866d2"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["INVOICE DATE"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="568" y="61" width="7" height="20" uuid="933fae5b-6850-4620-9860-609fb4f3ae0e"/>
                <textElement>
                    <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
                </textElement>
                <textFieldExpression><![CDATA[":"]]></textFieldExpression>
            </textField>
            <textField pattern="dd-MMM-yyyy">
                <reportElement x="575" y="61" width="70" height="20" uuid="94a17726-82c9-4068-a508-faa9669bb867"/>
                <textFieldExpression><![CDATA[new Date()]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="17" y="81" width="68" height="20" uuid="8193c234-a847-4db7-9f77-86cb8b7ea06e"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["PLACED ON "]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="85" y="81" width="7" height="20" uuid="b4226f3c-6c85-4a66-a86f-25f846a0c0ff"/>
                <textElement>
                    <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
                </textElement>
                <textFieldExpression><![CDATA[":"]]></textFieldExpression>
            </textField>
            <textField pattern="dd-MMM-yyyy">
                <reportElement x="92" y="81" width="100" height="20" uuid="23599627-a12c-4409-bf59-690182c93265"/>
                <textFieldExpression><![CDATA[$P{orderDate}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="488" y="81" width="79" height="20" uuid="c5382e3e-b4ae-45ca-8ed3-d78fc180730b"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["INVOICE NO. "]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="568" y="81" width="7" height="20" uuid="4c26ae03-6d88-45c6-83cd-e8144a428813"/>
                <textElement>
                    <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
                </textElement>
                <textFieldExpression><![CDATA[":"]]></textFieldExpression>
            </textField>
            <line>
                <reportElement x="15" y="106" width="619" height="1" uuid="f1d3c700-6cd0-4e6f-9482-ac0de6899d6c"/>
                <graphicElement>
                    <pen lineWidth="0.5"/>
                </graphicElement>
            </line>
        </band>
    </pageHeader>
    <detail>
        <band height="54" splitType="Stretch">
            <subreport isUsingCache="false">
                <reportElement x="0" y="0" width="200" height="54" uuid="0b04fdb2-f4bb-4db2-935a-be96d92fa8db"/>
                <subreportParameter name="billingAddress">
                    <subreportParameterExpression><![CDATA[$P{billingAddress}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportExpression><![CDATA[$P{filePath} + "/BillingVenueSubReport.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
    <pageFooter>
        <band height="67">
            <textField>
                <reportElement x="539" y="47" width="86" height="20" uuid="071f6609-ce04-405d-b993-ad0e89d95919"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="626" y="47" width="25" height="20" uuid="abb1c879-f261-4d0f-ba1b-7f0569d1c947"/>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <textField pattern="EEE,yyy,MMMMM,dd hh:mm aaa">
                <reportElement x="67" y="47" width="168" height="20" uuid="36b23a16-1717-472c-96d7-a6f250137920"/>
                <textFieldExpression><![CDATA[new Date()]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="9" y="3" width="634" height="46" uuid="ef43f479-905b-4e70-8410-3641b2ef7b73"/>
                <textElement textAlignment="Center">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["#72/4, First Floor, Near Gokula Towers, Dr. MS Ramaiah Rd, HMS Layout,Gokula Extension Mathikere, Bengaluru, Karnataka 560054, Service Tax #AAICP5558PSD001, Service Tax Category Supply of Tangible Goods Service, PAN No.AAICP5558P, CIN:U74900KA2016PTC086666 Tel: 080 4114566 Web: www.powersparrow.com    © POWERSPARROW INDIA PRIVATE LIMITED"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="13" y="48" width="61" height="19" uuid="cc0b3cef-159f-4688-8325-3dfac9cb8631"/>
                <textElement>
                    <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
                </textElement>
                <textFieldExpression><![CDATA["Printed On: "]]></textFieldExpression>
            </textField>
            <line>
                <reportElement x="11" y="3" width="624" height="1" uuid="f2aae4f8-1a16-4563-9ecd-38a610a3e51b"/>
                <graphicElement>
                    <pen lineWidth="0.5"/>
                </graphicElement>
            </line>
        </band>
    </pageFooter>
    <lastPageFooter>
        <band height="108">
            <textField>
                <reportElement x="542" y="0" width="101" height="24" uuid="d45e931d-45ef-42ac-b026-79d35954b03f"/>
                <textElement verticalAlignment="Bottom">
                    <font size="12" isItalic="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Thank You !"]]></textFieldExpression>
            </textField>
            <textField pattern="EEE,yyy,MMMMM,dd hh:mm aaa">
                <reportElement x="68" y="88" width="168" height="20" uuid="547654f3-c4ff-45f7-8ad1-9f8d70b5b454"/>
                <textFieldExpression><![CDATA[new Date()]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="10" y="44" width="634" height="46" uuid="7748c51f-0b9e-4d10-b80d-c44bb9c16523"/>
                <textElement textAlignment="Center">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["#72/4, First Floor, Near Gokula Towers, Dr. MS Ramaiah Rd, HMS Layout,Gokula Extension Mathikere, Bengaluru, Karnataka 560054, Service Tax #AAICP5558PSD001, Service Tax Category Supply of Tangible Goods Service, PAN No.AAICP5558P, CIN:U74900KA2016PTC086666 Tel: 080 4114566 Web: www.powersparrow.com    © POWERSPARROW INDIA PRIVATE LIMITED"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="14" y="89" width="61" height="19" uuid="6d2b3804-3f20-4b5b-b7e4-04064df35dac"/>
                <textElement>
                    <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
                </textElement>
                <textFieldExpression><![CDATA["Printed On: "]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="627" y="88" width="25" height="20" uuid="b8c448b9-aebb-4708-ae0a-e169d30131ec"/>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="540" y="88" width="86" height="20" uuid="73bad185-9ff2-42f1-9a05-fc18a23bb48b"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
            </textField>
            <line>
                <reportElement x="12" y="44" width="624" height="1" uuid="d8b89ce1-6581-4ce6-abf0-55fb079286e6"/>
                <graphicElement>
                    <pen lineWidth="0.5"/>
                </graphicElement>
            </line>
            <textField>
                <reportElement x="527" y="24" width="118" height="20" uuid="2a56862b-8300-460b-afb6-367fb031d6fe"/>
                <textElement textAlignment="Center" verticalAlignment="Top">
                    <font isItalic="true"/>
                </textElement>
                <textFieldExpression><![CDATA["For Your Business"]]></textFieldExpression>
            </textField>
        </band>
    </lastPageFooter>
</jasperReport>

billingVenuSubReport.xml (Sub-report): billingVenuSubReport.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="MultipleReport" pageWidth="629" pageHeight="842" columnWidth="629" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="898b61ea-06d8-443b-9332-6c4e1a9cde27">
    <property name="net.sf.jasperreports.awt.ignore.missing.font" value="true"/>
    <parameter name="billingAddress" class="java.lang.String"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="74">
            <rectangle>
                <reportElement stretchType="RelativeToBandHeight" x="18" y="0" width="187" height="73" isRemoveLineWhenBlank="true" uuid="7b23f2bd-0dcb-45a2-95e4-30db17995322"/>
                <graphicElement>
                    <pen lineWidth="0.5"/>
                </graphicElement>
            </rectangle>
            <rectangle>
                <reportElement stretchType="RelativeToBandHeight" x="205" y="0" width="193" height="73" isRemoveLineWhenBlank="true" uuid="b7f856b6-6047-4458-add5-5c3999db8276"/>
                <graphicElement>
                    <pen lineWidth="0.5"/>
                </graphicElement>
            </rectangle>
            <rectangle>
                <reportElement stretchType="RelativeToBandHeight" x="398" y="0" width="228" height="73" isRemoveLineWhenBlank="true" uuid="a38c9204-bd4d-4272-a437-bdaadb824973"/>
                <graphicElement>
                    <pen lineWidth="0.5"/>
                </graphicElement>
            </rectangle>
            <textField>
                <reportElement isPrintRepeatedValues="false" x="21" y="0" width="151" height="14" uuid="3f7fecf6-3f08-458a-b64d-0b660661385e"/>
                <textElement>
                    <font fontName="Times New Roman" isBold="true" pdfFontName="Times-Bold"/>
                </textElement>
                <textFieldExpression><![CDATA["BILLING ADDRESS"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="21" y="13" width="151" height="13" uuid="62f86f45-e16b-4e0b-ae88-658b3bdf5c57"/>
                <textElement>
                    <font fontName="Times New Roman" size="9" pdfFontName="Times-Roman"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{billingAddress}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Java code:爪哇代码:

Map<String,Object> reportParameters = new HashMap<>();
URL jrxmlUrl = this.getClass().getResource("/jrxml");
String filePath= jrxmlUrl.getPath();
String imagePath= filePath+"/logo.jpg";
reportParameters.put("filePath", filePath);
reportParameters.put("imagePath", imagePath);
setCommonParameters(reportDTO, orderInvoiceDetail, reportParameters);
setBillingLocationParameter(orderInvoiceDetail, reportParameters);
String invoiceFileName = "invoice_"+orderId+"_"+orderItemId+".pdf";
JasperPrint jasperPrint = JasperFillManager.fillReport(filePath+"/Invoice.jasper", reportParameters, new JREmptyDataSource());
Path invoiceFilePath = storageService.getFilePath(FileCategoryEnum.INVOICE, orgId, invoiceFileName);
Resource resource = new UrlResource(invoiceFilePath.toUri());
if(resource.exists()) {
   resource.getFile().delete();
}
OutputStream outputStream = new FileOutputStream(invoiceFilePath.toFile());
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
outputStream.flush();
outputStream.close();

Any help is much appreciated.任何帮助深表感谢。

The subreport has no data source and doesn't generate any output (by default).子报表没有数据源并且不生成任何输出(默认情况下)。

There are (at least) two things that you can do.您可以(至少)做两件事。

  1. Pass a JREmptyDataSource to the subreport (like you do for the master report in your Java code):JREmptyDataSource传递给子报表(就像您在 Java 代码中为主报表所做的那样):

     <subreport isUsingCache="false"> <reportElement x="0" y="0" width="200" height="54" uuid="0b04fdb2-f4bb-4db2-935a-be96d92fa8db"/> <subreportParameter name="billingAddress"> <subreportParameterExpression><![CDATA[$P{billingAddress}]]></subreportParameterExpression> </subreportParameter> <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression> <subreportExpression><![CDATA[$P{filePath} + "/BillingVenueSubReport.jasper"]]></subreportExpression> </subreport>
  2. Set whenNoDataType="AllSectionsNoDetail" in the subreport (at the <jasperReport> level).在子报表中设置whenNoDataType="AllSectionsNoDetail" (在<jasperReport>级别)。 That will result in the subreport printing all bands except the detail band when it has no data (which would do in your case because your subreport only uses the title band).这将导致子报表在没有数据时打印除详细信息波段之外的所有波段(在您的情况下会这样做,因为您的子报表仅使用标题波段)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM