Crystal报表从XML创建报表并将其导出到PDF,而无需使用原始XML

时间:2019-02-08 14:50:39

标签: java crystal-reports-xi

我有一个Java Web应用程序,可以导出pdf文件。

我必须使用Crystal Reports 11。我已经可以导出pdf了,问题在于它只能在本地使用,因为.rpt文件在我的计算机中引用了XML文件。

因此,当我要导出报告并且.rpt文件找不到该文件时,即使我给它提供了一个新的数据集来处理它,仍然会引发未发现的异常。我尝试以编程方式更改文件的连接,但始终会引发与连接有关的异常。

public InputStream export() throws ReportSDKException, IOException, IllegalArgumentException, IllegalAccessException {
        ReportClientDocument reportClientDoc = new ReportClientDocument();
        reportClientDoc.setLocale(Locale.forLanguageTag(localeTag));
        reportClientDoc.open(reportPath, OpenReportOptions._discardSavedData);
        DatabaseController databaseController = reportClientDoc.getDatabaseController();

        IConnectionInfo oldConn = databaseController.getConnectionInfos(null).get(0);
        IConnectionInfo newConn = resolveConnection(reportClientDoc).get(0);

        int replaceParams = DBOptions._ignoreCurrentTableQualifiers | DBOptions._doNotVerifyDB;
        databaseController.replaceConnection(oldConn, newConn, null,replaceParams);

        reportClientDoc.getDatabaseController().setDataSource(this.dataset);
        ParameterFieldController parameterController = reportClientDoc.getDataDefController()
                .getParameterFieldController();
        for (Param<Double> p : doubleParams) {
            parameterController.setCurrentValue(p.subReportName, p.fieldName, p.value);
        }

        for (Param<Object> p : objectParams) {
            parameterController.setCurrentValue(p.subReportName, p.fieldName, p.value);
        }

        return reportClientDoc.getPrintOutputController().export(this.format);
    }

private ConnectionInfos resolveConnection(ReportClientDocument reportClientDoc) throws ReportSDKException {
        IConnectionInfo oldConnection = new ConnectionInfo();
        DatabaseController dbController = reportClientDoc.getDatabaseController();
        oldConnection = dbController.getConnectionInfos(null).getConnectionInfo(0);

        String xsdPath = Paths.get(this.xsdPath).toAbsolutePath().toString();

        final String SERVER_NAME = dummyXmlPath + " " + xsdPath;
        final String DATABASE_DLL = oldConnection.getAttributes().getStringValue("Database DLL");
        final String LOCAL_SCHEMA_FILE = xsdPath;
        final String SERVER_TYPE = "XML";
        final String PREQESERVERNAME = SERVER_NAME;
        final String PREQESERVERTYPE = "XML";
        final String LOCAL_XML_FILE = dummyXmlPath;

        PropertyBag newAttributes = new PropertyBag();
        newAttributes.put("Server Name", SERVER_NAME);
        newAttributes.put("Database DLL", DATABASE_DLL);
        newAttributes.put("Local Schema File", LOCAL_SCHEMA_FILE);
        newAttributes.put("PreQEServerName", PREQESERVERNAME);
        newAttributes.put("PreQEServerType", PREQESERVERTYPE);
        newAttributes.put("Server Type", SERVER_TYPE);
        newAttributes.put("Local XML File", LOCAL_XML_FILE);

        IConnectionInfo newConnection = (IConnectionInfo) oldConnection.clone(true);
        newConnection.setAttributes(newAttributes);
        newConnection.setKind(oldConnection.getKind());

        ConnectionInfos connectionInfos = new ConnectionInfos();
        connectionInfos.add(newConnection);
        return connectionInfos;
    }

1 个答案:

答案 0 :(得分:0)

我能够通过将子报表数据添加到子报表中来解决此问题。

String