Jasper报表空白页面

5

以下是我的jrxml和java代码。我试图创建一份包含一些文本和值的报告。但是生成了一个空白报告。我将map作为参数传递以填充报告。我必须添加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();
3个回答

4
你把报告数据和参数混淆了。我没有看到你有任何报告数据,这就是为什么你得到了一个空报告的原因。此外,你填充了一个名为“Field”的参数而不是实际的报告数据。所以你需要在报告中创建一个名为“Field”的参数,然后在详细信息区块中引用它,或者你需要将报告数据传递到你的报告中,类似于:
JRBeanCollectionDataSource data = new JRBeanCollectionDataSource(dataSet);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, data);

在这里,dataSet是一个集合,比如List<?>。在这个List里面,你会有一个对象,其中一个属性是Field


3

我曾经也遇到过空白页面(pdf),即使是最简单的报告也是如此。
直到我发送了空数据源(new JREmptyDataSource())!
这解决了我的问题:

Map<String, Object> param = new HashMap<>();
param.put("SOME_PARAM","something");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());

默认情况下,当报表中没有数据源信息时,JR不会生成任何页面。

这里有详细解释。


对我有用,谢谢。 - Alison Moura

0
我在从主要的Jasper报告中调用子报告时,遇到了空白页面的问题好几次。请确保从分组页眉调用子报告,而不要从分组页脚调用。这个解决方案对我来说非常有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接