通过JasperReports中的subreportExpression传递一个InputStream

时间:2017-04-05 14:27:14

标签: java jasper-reports subreport

我遇到JasperReports报告的问题,因为我使用的是Spring-boot框架。我需要传递一个InputStream,而不是子报表表达式标记中的绝对路径。

因此,我在这个InputStream中添加了一个参数:

Map<String, Object> parameters = new HashedMap();
parameters.put("STREAM_SUBREPORT",classLoader.getResource(subreport_filename).openStream());

并且在jrxml中具有以下子报告定义

<subreportParameter name="ItemDataSource">
    <subreportParameterExpression><![CDATA[$P{ItemDataSource}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{ItemDataSource}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{STREAM_SUBREPORT}]]></subreportExpression>

我的Java代码是:

JasperDesign jd = null;
try {
    jd = JRXmlLoader.load(classLoader.getResource(REPORT_PATH).openStream());

} catch (IOException e) {
   ...
}
JasperReport report = JasperCompileManager.compileReport(jd);
JasperPrint print = JasperFillManager.fillReport(report, parameters, ds);

我尝试传递的子报告的定义是

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-04-02T23:37:54 -->
<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="submissions_subreport_summary" pageWidth="595" pageHeight="842" whenNoDataType="BlankPage" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="408f425e-96dc-41a9-991e-2208c3c8a120">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.sql.SQLQueryDesigner.sash.w1" value="233"/>
    <property name="com.jaspersoft.studio.data.sql.SQLQueryDesigner.sash.w2" value="756"/>
    <parameter name="ItemDataSourceSummary" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <field name="title" class="java.lang.String"/>
    <field name="details" class="java.lang.String"/>
    <title>
        <band height="15">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <staticText>
                <reportElement x="2" y="0" width="123" height="12" uuid="782e96a2-5db0-458a-9078-63d470b2e155">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
                <box>
                    <bottomPen lineWidth="1.0"/>
                </box>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Subreport Caption]]></text>
            </staticText>
        </band>
    </title>
    <detail>
        <band height="35">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <textField>
                <reportElement x="0" y="0" width="280" height="30" uuid="6f92584d-1738-4d79-b169-2f544152341b"/>
                <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="280" y="0" width="270" height="30" uuid="579b2443-f865-4422-888d-d995ffdfbdc5"/>
                <textFieldExpression><![CDATA[$F{details}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

但是,我通过JasperFillManager方法得到了例外,

net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: $P{STREAM_SUBREPORT}

我尝试使用jrxml

中的跟随演员
<subreportExpression><![CDATA[(java.io.InputStream($P{DIR_SUBREPORT_SUBMISSION}))]]></subreportExpression>

但也有例外

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. java.io cannot be resolved to a type
                value = (java.io.InputStream(((java.lang.String)parameter_STREAM_SUBREPORT.getValue()))); //$JR_EXPR_ID=11$

欢迎任何建议!

0 个答案:

没有答案