首页 文章

生成报告时使用NULL值(使用Jasper报告)

提问于
浏览
1

我对Jasper Report和XML数据源有一个奇怪的问题 . 我有两个在iReport中运行良好的报告(预览显示所有正确的数据),但是当我使用JasperRunManager.runReportToPdfFile运行报告时,值显示为null . 有趣的是,两个报告中的一个有子报告,在子报告中,一切都正确显示!

我正在使用XPath这样的查询:

<queryString language="xPath">
    <![CDATA[/rosterArray/list/studentRoster]]>
</queryString>

我只有一个字段:

<field name="studentName" class="java.lang.String">
    <fieldDescription><![CDATA[studentName]]></fieldDescription>
</field>

并显示它:

<detail>
    <band height="30" splitType="Stretch">
        <textField>
            <reportElement x="10" y="6" width="188" height="20"/>
            <textElement/>
            <textFieldExpression class="java.lang.String"><![CDATA[$F{studentName}]]></textFieldExpression>
        </textField>
    </band>
</detail>

XML看起来像这样:

<rosterArray>
   <list>
      <studentRoster>
         <studentName>Robert, Pascal</studentName>
      </studentRoster>
    </list>
  </rosterArray>

以及创建生成的报告的代码:

File xmlFileName = new File(xmlDSFileName());
JRXmlDataSource xmlDS = new JRXmlDataSource(xmlFileName);
xmlDS.setDatePattern("yyyy-mm-dd HH:mm:ss.S z");

File destFile = File.createTempFile(compiledReportName, ".pdf");  
String inputFileName = PathUtilities.pathToReport(compiledReportName);

JasperRunManager.runReportToPdfFile(inputFileName, destFile.getPath(), parameters, dataSource);

1 回答

  • 0

    创建JRXmlDataSource对象时,必须添加querystring标记中定义的xpath选择表达式 . 在你的情况下,它应该是:

    JRXmlDataSource xmlDS = new JRXmlDataSource(xmlFileName, "/rosterArray/list/studentRoster");
    

    问候 .

相关问题