简体   繁体   English

编译并填充Jasper报告-XML数据源

[英]Compile & Fill Jasper Report - XML datasource

I am hoping someone can tell me why the code below is creating an empty pdf file every time? 我希望有人能告诉我为什么下面的代码每次都会创建一个空的pdf文件?

I am trying to compile, fill and run the xmldatasource sample project that ships with JasperReports 4.0.2. 我正在尝试编译,填充和运行JasperReports 4.0.2附带的xmldatasource示例项目。 Although I am attempting to do it Programatically as I want to use a similar (non ant-based) compilation method in my own software. 尽管我尝试以编程方式执行此操作,因为我想在自己的软件中使用类似的(基于非蚂蚁的)编译方法。 The same project that comes with Jasper uses ant scripts to create the report and it works. Jasper随附的同一项目使用ant脚本来创建报告,并且该报告可以工作。 So I know there is no issue with the JRXML files. 因此,我知道JRXML文件没有问题。 However, I cannot seem to fill the report with data correctly. 但是,我似乎无法正确地用数据填充报告。

When I debugged the Jasper engine source to see if my XML source document is being loaded correctly I see that the PARAMETER_XML_DOCUMENT had the value 当我调试Jasper引擎源以查看XML源文档是否正确加载时,我看到PARAMETER_XML_DOCUMENT具有该值

[#document: null] [#document:null]

Do you think this would mean that the XML source file is not being read correctly? 您是否认为这表示XML源文件未正确读取?

The code compiles and runs without errors and all of the relevant files are generated (.jasper, .jrprint and .pdf) but for some reason the rendered PDF document appears to have only one blank page every time. 代码可以编译并运行,没有错误,并且生成了所有相关文件(.jasper,.jrprint和.pdf),但是由于某种原因,渲染的PDF文档每次似乎只有一个空白页。

Any help is greatly appreciated... 任何帮助是极大的赞赏...

public static void main(String[] args) {
        Map map = new RenderReportBean().execute("northwind", "OrdersReport");

    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public Map execute(String sourceFileId, String templateId, Integer[] formats) {
        Map params = new HashMap();
        Document document;
        Map<Integer, Boolean> renderedSuccessList = new HashMap<Integer, Boolean>();
        try {

            document = JRXmlUtils.parse(JRLoader.getLocationInputStream(sourceFileId + ".xml"));

            params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);
            params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd");
            params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##");
            params.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.ENGLISH);
            params.put(JRParameter.REPORT_LOCALE, Locale.US);

            JasperCompileManager.compileReportToFile(templateId + ".jrxml", templateId + ".jasper");

            JasperFillManager.fillReportToFile(templateId + ".jasper", params);

            JasperExportManager.exportReportToPdfFile(templateId + ".jrprint", sourceFileId + ".pdf");


        } catch (Exception e) {
            e.printStackTrace();
            return renderedSuccessList;
        }
        return renderedSuccessList;
    }

OrdersReport.jrxml OrdersReport.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="OrdersReport" pageWidth="500" pageHeight="842" columnWidth="500" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <style name="Sans_Normal" isDefault="true" fontName="DejaVu Sans" fontSize="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
    <style name="Sans_Bold" isDefault="false" fontName="DejaVu Sans" fontSize="8" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
    <style name="Sans_Italic" isDefault="false" fontName="DejaVu Sans" fontSize="8" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false"/>
    <parameter name="CustomerID" class="java.lang.String"/>
    <queryString language="xPath"><![CDATA[/Northwind/Orders[CustomerID='$P{CustomerID}']]]></queryString>
    <field name="Id" class="java.lang.String">
        <fieldDescription><![CDATA[OrderID]]></fieldDescription>
    </field>
    <field name="OrderDate" class="java.util.Date">
        <fieldDescription><![CDATA[OrderDate]]></fieldDescription>
    </field>
    <field name="ShipCity" class="java.lang.String">
        <fieldDescription><![CDATA[ShipCity]]></fieldDescription>
    </field>
    <field name="Freight" class="java.lang.Float">
        <fieldDescription><![CDATA[Freight]]></fieldDescription>
    </field>
    <variable name="TotalFreight" class="java.lang.Float" calculation="Sum">
        <variableExpression><![CDATA[$F{Freight}]]></variableExpression>
    </variable>
    <pageHeader>
        <band height="14">
            <frame>
                <reportElement x="0" y="2" width="356" height="10" forecolor="#ccffff" backcolor="#ccffff" mode="Opaque"/>
                <staticText>
                    <reportElement mode="Opaque" x="0" y="0" width="48" height="10" backcolor="#ccffff" style="Sans_Bold"/>
                    <textElement textAlignment="Right"/>
                    <text><![CDATA[ID]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="54" y="0" width="87" height="10" backcolor="#ccffff" style="Sans_Bold"/>
                    <textElement textAlignment="Center"/>
                    <text><![CDATA[Order Date]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="146" y="0" width="108" height="10" backcolor="#ccffff" style="Sans_Bold"/>
                    <text><![CDATA[Ship City]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="259" y="0" width="92" height="10" backcolor="#ccffff" style="Sans_Bold"/>
                    <textElement textAlignment="Right"/>
                    <text><![CDATA[Freight]]></text>
                </staticText>
            </frame>
        </band>
    </pageHeader>
    <detail>
        <band height="14">
            <textField>
                <reportElement x="0" y="2" width="51" height="10"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{Id}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="yyyy, MMM dd">
                <reportElement positionType="Float" x="54" y="2" width="87" height="10"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.util.Date"><![CDATA[$F{OrderDate}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="146" y="2" width="108" height="10"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{ShipCity}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="¤ #,##0.00">
                <reportElement positionType="Float" x="259" y="2" width="92" height="10"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression class="java.lang.Float"><![CDATA[$F{Freight}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="14">
            <frame>
                <reportElement x="0" y="2" width="356" height="10" forecolor="#33cccc" backcolor="#33cccc" mode="Opaque"/>
                <staticText>
                    <reportElement mode="Opaque" x="160" y="0" width="67" height="10" backcolor="#33cccc" style="Sans_Bold"/>
                    <textElement textAlignment="Right"/>
                    <text><![CDATA[Total :]]></text>
                </staticText>
                <textField>
                    <reportElement mode="Opaque" x="259" y="0" width="92" height="10" backcolor="#33cccc" style="Sans_Bold"/>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression class="java.lang.Float"><![CDATA[$V{TotalFreight}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement mode="Opaque" x="227" y="0" width="27" height="10" backcolor="#33cccc" style="Sans_Bold"/>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression class="java.lang.Integer"><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </summary>
</jasperReport>

CustomersReport.jrxml 客户报告.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="CustomersReport" pageWidth="595" pageHeight="842" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50">
    <style name="Sans_Normal" isDefault="true" fontName="DejaVu Sans" fontSize="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
    <style name="Sans_Bold" isDefault="false" fontName="DejaVu Sans" fontSize="12" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
    <style name="Sans_Italic" isDefault="false" fontName="DejaVu Sans" fontSize="12" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false"/>
    <queryString language="xPath"><![CDATA[/Northwind/Customers]]></queryString>
    <field name="CustomerID" class="java.lang.String">
        <fieldDescription><![CDATA[CustomerID]]></fieldDescription>
    </field>
    <field name="CompanyName" class="java.lang.String">
        <fieldDescription><![CDATA[CompanyName]]></fieldDescription>
    </field>
    <title>
        <band height="50">
            <line>
                <reportElement x="0" y="0" width="515" height="1"/>
                <graphicElement/>
            </line>
            <staticText>
                <reportElement x="0" y="10" width="515" height="30" style="Sans_Normal"/>
                <textElement textAlignment="Center">
                    <font size="22"/>
                </textElement>
                <text><![CDATA[Customer Orders Report]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="21">
            <staticText>
                <reportElement mode="Opaque" x="0" y="5" width="515" height="15" forecolor="#ffffff" backcolor="#333333" style="Sans_Bold"/>
                <text><![CDATA[Customer Order List]]></text>
            </staticText>
        </band>
    </pageHeader>
    <detail>
        <band height="50">
            <textField>
                <reportElement x="5" y="5" width="100" height="15" isPrintWhenDetailOverflows="true" style="Sans_Bold"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{CustomerID}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement isPrintRepeatedValues="false" x="404" y="5" width="100" height="15" isPrintWhenDetailOverflows="true" style="Sans_Bold"/>
                <text><![CDATA[(continued)]]></text>
            </staticText>
            <line>
                <reportElement x="0" y="20" width="515" height="1" isPrintWhenDetailOverflows="true"/>
                <graphicElement/>
            </line>
            <subreport>
                <reportElement isPrintRepeatedValues="false" x="5" y="25" width="507" height="20" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
                <subreportParameter name="XML_DATA_DOCUMENT">
                    <subreportParameterExpression>$P{XML_DATA_DOCUMENT}</subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="XML_DATE_PATTERN">
                    <subreportParameterExpression>$P{XML_DATE_PATTERN}</subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="XML_NUMBER_PATTERN">
                    <subreportParameterExpression>$P{XML_NUMBER_PATTERN}</subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="XML_LOCALE">
                    <subreportParameterExpression>$P{XML_LOCALE}</subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="XML_TIME_ZONE">
                    <subreportParameterExpression>$P{XML_TIME_ZONE}</subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="CustomerID">
                    <subreportParameterExpression>$F{CustomerID}</subreportParameterExpression>
                </subreportParameter>
                <subreportExpression class="java.lang.String"><![CDATA["OrdersReport.jasper"]]></subreportExpression>
            </subreport>
            <textField>
                <reportElement x="109" y="5" width="291" height="15" isPrintWhenDetailOverflows="true" style="Sans_Bold"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{CompanyName}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <pageFooter>
        <band height="40">
            <line>
                <reportElement x="0" y="10" width="515" height="1"/>
                <graphicElement/>
            </line>
            <textField>
                <reportElement x="200" y="20" width="80" height="15"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression class="java.lang.String"><![CDATA["Page " + String.valueOf($V{PAGE_NUMBER}) + " of"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="280" y="20" width="75" height="15"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[" " + String.valueOf($V{PAGE_NUMBER})]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
</jasperReport>

Ok so I found out the issue and it's a bit of an odd one really as the other means of compiling the templates do not call for the same approach. 好的,所以我发现了这个问题,确实有点奇怪,因为其他编译模板的方法不需要相同的方法。

The example above uses nested reports and for some reason the runtime compile option requires you to compile both templates individually instead of just compiling the Super report and Jasper picking up the sub-report bu default - as seems to be the case when compiling from ant scripts. 上面的示例使用嵌套的报告,由于某些原因,运行时编译选项要求您分别编译两个模板,而不是仅编译超级报告和Jasper来选择默认情况下的子报告-就像从ant脚本编译时的情况一样。

So to make the above code work the line: 因此,使上面的代码行如下:

JasperCompileManager.compileReportToFile(templateId + ".jrxml", templateId + ".jasper");

needs to be included for each template (super and sub) that are required for the report. 需要为报告所需的每个模板(上级和下级)包括在内。

The problem is here: document = JRXmlUtils.parse(JRLoader.getLocationInputStream(sourceFileId + ".xml")); 问题在这里:document = JRXmlUtils.parse(JRLoader.getLocationInputStream(sourceFileId +“ .xml”));

The API says: getLocationInputStream API说:getLocationInputStream

public static java.io.InputStream getLocationInputStream(java.lang.String location) throws JRException 公共静态java.io.InputStream getLocationInputStream(java.lang.String location)抛出JRException

Tries to open an input stream for a location.

The method tries to interpret the location as a file name, a resource name or an URL. If any of these succeed, an input stream is created and returned.

Parameters:
    location - the location 
Returns:
    an input stream if the location is an existing file name, a resource name on the classpath or an URL or null otherwise. 
Throws:
    JRException

If the method is not able to return an input stream, it will return null. 如果该方法无法返回输入流,则它将返回null。 Basically the string you are passing as parameter is incorrect. 基本上,您作为参数传递的字符串不正确。 Try passing the absolute path. 尝试通过绝对路径。

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

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