1

我对 jasperstudio 6.3.0 非常熟悉,并且能够毫无问题地从 java 运行其他报告。效果很好。但是,我无法运行包含子报表的报表。我在这里 stackoverflow 上找到了解决方案,但仍然无法使其工作。在我的解决方案中,我将报表加载到表中进行存储,并从数据库中提取子报表和主报表并编译 jrxml。一切都很好,然后

net.sf.jasperreports.engine.JRException:在以下位置找不到资源:subInvoiceSummary.jasper。runReport 中的错误:找不到资源:subInvoiceSummary.jasper。在 net.sf.jasperreports.repo.RepositoryUtil.getReport(RepositoryUtil.java:112) 在 net.sf.jasperreports.engine.fill.JRFillSubreport 在 net.sf.jasperreports.repo.RepositoryUtil.getResourceFromLocation(RepositoryUtil.java:153) .loadReport(JRFillSubreport.java:398) 在 net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(JRFillSubreport.java:365) 在 net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:427) ) 在 net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:381) 在 net.sf.jasperreports 的 net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:341)。 engine.fill.JRFillBand。

我尝试了很多排列但无济于事。我还尝试提取 jasperreports 的代码来跟踪上面显示的堆栈,但是有太多 .jar 依赖项和依赖项版本,我无法在 2 天后将它编译干净(如果有人有指向一个简单的方法来使这项工作,请让我知道)。这是代码片段:

Map<String, Object> rptParms = new HashMap<String, Object>();

List<daRecOnDemandSubReport> subReports = report.getOnDemandSubReport();
Iterator<daRecOnDemandSubReport> subReportList = subReports.iterator();
while (subReportList.hasNext()) {
daRecOnDemandSubReport subReport = subReportList.next();
ByteArrayInputStream x = getSubReportDataStream(dbConnROIPro, report.getODReportID());
JasperReport compiledSubReport = JasperCompileManager.compileReport(x);
String fileName = subReport.getODSubReportFileName().replace(".jrxml", ".jasper");
rptParms.put(fileName, compiledSubReport);
}

ByteArrayInputStream x = getReportDataStream(dbConnROIPro, report.getODReportID());

JasperReport cRpt = JasperCompileManager.compileReport(x);

rptParms = getReportParameters(dbConnGTrack,report.getODReportID(), rptParms, report.getOnDemandParm(), programID);

JasperPrint rpt = JasperFillManager.fillReport(cRpt,rptParms,dbConnGTrack.getDbConn());

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
JRXlsxExporter XLSXexporter = new JRXlsxExporter();
   XLSXexporter.setExporterInput(new SimpleExporterInput(rpt));
   XLSXexporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outStream)); 
   XLSXexporter.exportReport();

提前感谢您的任何帮助

4

1 回答 1

0

我有同样的问题,从来没有找到一个简单的方法来实现这一点。Jasper 开发人员自己说子报告在他们的论坛上没有很好地实现。

我用我的这段代码解决了这个问题。它也远非完美,但它确实有效。

public byte[] generateReport(ExportType exportType, String resourceName, Connection connection, Map<String, Object> parameters) throws JRException, SQLException {
    JasperReport jasperReport = loadReport(resourceName);

    LOGGER.debug("Loading subreports if any...");
    Map<String, JasperReport> subReports = loadSubReports(resourceName, jasperReport);
    parameters.putAll(subReports);

    LOGGER.debug("Filling report with JDBC and {}", parameters);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);

    LOGGER.debug("Exporting report with {}", exportType.getClass().getSimpleName());
    byte[] pdf = exportType.export(jasperPrint);

    return pdf;
}

/**
 * Converts a system resource into a JasperReport instance.
 * If the extension is ".jasper", it tries to load a pre-compiled report.
 * If the extension is ".jrxml", it tries to compile the report.
 */
private JasperReport loadReport(String resourceName) throws JRException {
    JasperReport jasperReport = null;
    InputStream inputStream = this.getClass().getResourceAsStream(resourceName);

    if (inputStream != null) {
        if (resourceName.toLowerCase().endsWith(JASPER)) {
            LOGGER.debug("Loading pre-compiled report: {}", resourceName);
            jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
        } else {
            LOGGER.debug("Compiling XML report: {}", resourceName);
            jasperReport = JasperCompileManager.compileReport(inputStream);
        }
    } else {
        LOGGER.warn("Unable to open resource: {}", resourceName);
    }
    return jasperReport;
}

/**
 * Parses a Report for SubReport parameters.
 * A Parameter has to be a <code>java.lang.Object</code> or a <code>net.sf.jasperreports.engine.JasperReport</code> to be considered a SubReport Input
 * An Object parameter has the advantage to allow Jasper Report's IDE to accept a Filename String resource.
 * If the parameter is an Object, the Filename declared as <b>default value</b> is used to load the SubReport.
 * If the parameter is a JasperReport, the parameter's name is used instead.
 * 
 * The default value, or the parameter's name, have to use a ".jasper" or ".jrxml" extension.
 * The SubReport resource's path has to be located relatively to the parent Report.
 */
private Map<String, JasperReport> loadSubReports(String masterResource, JasperReport jasperReport) throws JRException {
    Map<String, JasperReport> subReportExpressions = new HashMap<>();
    String masterPath = FilenameUtils.getFullPath(masterResource);

    for (JRParameter jrParameter : jasperReport.getParameters()) {
        String subReportName = getSubReportName(jrParameter);
        if (subReportName != null) {
            String name = jrParameter.getName();
            JasperReport subReport = loadReport(masterPath + subReportName);
            subReportExpressions.put(name, subReport);
        }
    }
    return subReportExpressions;
}


/**
 * Analyses the parameter to find a SubReport resource name respecting the defined rules
 * 
 * @return The filename of the resource, or null
 */
private String getSubReportName(JRParameter jrParameter) {
    String ret = null;
    Class<?> valueClass = jrParameter.getValueClass();
    boolean isObject = (valueClass == Object.class);
    boolean isJasperReport = (valueClass == JasperReport.class);

    // If the parameter is an Object,
    // we take the SubReport name from the default value (a String)
    if (isObject) {
        String defaultValue = getDefaultValue(jrParameter);
        if (defaultValue != null && checkExtension(defaultValue)) {
            ret = defaultValue;
        }
    }

    // If the parameter is a JasperReport, or an Object without a correct defaultValue,
    // we take the SubReport name from the parameter's name
    if (isJasperReport || (ret == null && isObject)) {
        String name = jrParameter.getName();
        if (checkExtension(name)) {
            ret = name;
        }
    }
    return ret;
}


private boolean checkExtension(String defaultValue) {
    defaultValue = defaultValue.toLowerCase();
    return defaultValue.endsWith(JRXML) || defaultValue.endsWith(JASPER);
}

private String getDefaultValue(JRParameter jrParameter) {
    String ret = null;
    JRExpression defaultValueExpression = jrParameter.getDefaultValueExpression();
    if (defaultValueExpression != null) {
        ret = defaultValueExpression.getText();
        if (ret != null && ret.length() > 2 && ret.startsWith(QUOTE) && ret.endsWith(QUOTE)) {
            // Removing quotes from the String
            ret = ret.substring(1, ret.length() - 1);
        }
    }
    return ret;
}

希望这可以帮助。如果有人有更好的解决方案,我会喜欢你的问题。

于 2017-02-21T23:19:55.090 回答