碧玉报告空白页

时间:2018-07-12 14:45:41

标签: java jasper-reports

下面的我的jrxml和Java代码。我正在尝试使用一些文本和值创建报告。但是,将生成空白报告。我正在粘贴地图以将报告填充为参数。我必须添加4-5条静态行,然后添加一个动态变量。我在详细的乐队中添加了这些东西。什么地方不对

    <?xml version="1.0" encoding="UTF-8"  ?>
    <!-- Created with iReport - A designer for JasperReports -->
    <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
    <jasperReport
             name="Untitled_report_2"
             columnCount="1"
             printOrder="Vertical"
             orientation="Portrait"
             pageWidth="595"
             pageHeight="842"
             columnWidth="535"
             columnSpacing="0"
             leftMargin="30"
             rightMargin="30"
             topMargin="20"
             bottomMargin="20"
             whenNoDataType="NoPages"
             isTitleNewPage="false"
             isSummaryNewPage="false">
        <property name="ireport.scriptlethandling" value="0" />
        <property name="ireport.encoding" value="UTF-8" />
        <import value="java.util.*" />
        <import value="net.sf.jasperreports.engine.*" />
        <import value="net.sf.jasperreports.engine.data.*" />


        <field name="Field" class="java.lang.String"/>

            <background>
                <band height="0"  isSplitAllowed="true" >
                </band>
            </background>
            <title>
                <band height="50"  isSplitAllowed="true" >
                </band>
            </title>
            <pageHeader>
                <band height="50"  isSplitAllowed="true" >
                </band>
            </pageHeader>
            <columnHeader>
                <band height="30"  isSplitAllowed="true" >
                </band>
            </columnHeader>
            <detail>
                <band height="100"  isSplitAllowed="true" >
                    <staticText>
                        <reportElement
                            x="20"
                            y="10"
                            width="180"
                            height="30"
                            key="staticText-1"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <text><![CDATA[4-5 lines text]]></text>
                    </staticText>
                    <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                        <reportElement
                            x="20"
                            y="40"
                            width="60"
                            height="20"
                            key="textField-1"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <textFieldExpression   class="java.lang.String"><![CDATA[$F{Field}]]></textFieldExpression>
                    </textField>
                    <staticText>
                        <reportElement
                            x="20"
                            y="70"
                            width="160"
                            height="30"
                            key="staticText-2"/>
                        <box></box>
                        <textElement>
                            <font/>
                        </textElement>
                    <text><![CDATA[4-5 lines text]]></text>
                    </staticText>
                </band>
            </detail>
            <columnFooter>
                <band height="30"  isSplitAllowed="true" >
                </band>
            </columnFooter>
            <pageFooter>
                <band height="50"  isSplitAllowed="true" >
                </band>
            </pageFooter>
            <lastPageFooter>
                <band height="50"  isSplitAllowed="true" >
                </band>
            </lastPageFooter>
            <summary>
                <band height="50"  isSplitAllowed="true" >
                </band>
            </summary>
    </jasperReport>

Java代码:

    InputStream fileInput = getApplicationContext().getResource("/WEB-INF/reports/myjrxml.jrxml").getInputStream();
    JasperReport jasperReport = JasperCompileManager.compileReport(fileInput);
    HashMap map = new HashMap();
    map.put("Field", "test");
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map);
    byte[] output1 = JasperExportManager.exportReportToPdf(jasperPrint); 
    String filename = "d:/test.pdf";
    FileOutputStream fo = new FileOutputStream(filename);
    fo.write(output1);
    fo.close();

1 个答案:

答案 0 :(得分:4)

您正在将报告数据与参数混淆。我看不到您有任何报告数据,这就是为什么您得到空报告的原因。此外,您要填充名为“字段”的参数而不是实际报告数据。因此,您要么需要在报告中创建一个名为“字段”的参数,然后在详细信息区域中引用它,要么需要将报告数据传递到报告中,例如

JRBeanCollectionDataSource data = new JRBeanCollectionDataSource(dataSet);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, data);

其中dataSet是一个集合,例如List<?>。在列表中,您将拥有一个对象,其中一个属性为Field