2016-12-29 384 views
1

我有一个C#应用程序,它使用Interop API执行与MS Office的邮件合并。 我现在试图让它支持开放式办公。 我想使用OpenOffice SDK: http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/MailMerge.html#Command使用OpenOffice SDK创建数据源时出现异常

看起来并不晶莹剔透我现在....

我总算得到邮件合并代码工作。 事情是我们需要在实际执行MailMerge之前创建一个“DataSource”,并且遇到困难。

我可以在Java这里得到一个样本: https://wiki.openoffice.org/wiki/Documentation/DevGuide/Database/The_DataSource_Service

我需要将其转换为C#。

我的困难是,Java使用这个对象来执行其强制转换:

XStorable store = (XStorable)UnoRuntime.queryInterface(XStorable.class, xDs); 

没有什么等同于C#。

我转换的代码是这样的:

public static void CreateDataSource(string dataSourceProvidedFilePath, string dataSourceSavedFilePath) 
    { 
       XComponentContext oStrap = uno.util.Bootstrap.bootstrap(); 
     XMultiServiceFactory _rMSF = (XMultiServiceFactory)oStrap.getServiceManager(); 

     // the XSingleServiceFactory of the database context creates new generic 
     // com.sun.star.sdb.DataSources (!) 
     // retrieve the database context at the global service manager and get its 
     // XSingleServiceFactory interface 
     XSingleServiceFactory xFac = (XSingleServiceFactory) _rMSF.createInstance("com.sun.star.sdb.DatabaseContext"); 
      //(XSingleServiceFactory)UnoRuntime.queryInterface(XSingleServiceFactory.class, _rMSF.createInstance("com.sun.star.sdb.DatabaseContext")); 

     // instantiate an empty data source at the XSingleServiceFactory 
     // interface of the DatabaseContext 
     Object xDs = xFac.createInstance(); 

     // register it with the database context 
     XNamingService xServ = (XNamingService)xFac; 
      //(XNamingService)UnoRuntime.queryInterface(XNamingService.class, xFac); 

     XStorable store = (XStorable) xDs; 
      //(XStorable)UnoRuntime.queryInterface(XStorable.class, xDs); 

     XModel model =(XModel) xDs; 
      //(XModel)UnoRuntime.queryInterface(XModel.class, xDs); 

     //on détermine le fichier ou sera sauvegardée la data source 
     string dataSourcePathURL = Path.Combine(Path.GetDirectoryName(dataSourceProvidedFilePath), dataSourceSavedFilePath + ".odb").ConvertToOpenOfficeURL(); 
     store.storeAsURL(/*"file:///c:/test.odb"*/dataSourcePathURL,model.getArgs()); 
     xServ.registerObject("NewDataSourceName", xDs); 

     // setting the necessary data source properties 
     XPropertySet xDsProps = (XPropertySet)xDs; 
      //(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xDs); 
     // Adabas D URL 
     xDsProps.setPropertyValue("URL", new uno.Any("sdbc:adabas::MYDB1")); 

     // force password dialog 
     //xDsProps.setPropertyValue("IsPasswordRequired", new Boolean(true)); 

     // suggest dsadmin as user name 
     xDsProps.setPropertyValue("User", new uno.Any("dsadmin")); 
     store.store(); 
    } 

一些石膏工作得很好:

XNamingService xServ = (XNamingService)xFac; 
      //(XNamingService)UnoRuntime.queryInterface(XNamingService.class, xFac); 

但一些其他类型转换抛出一个异常:

XStorable店=(XStorable)XDS; //(XStorable)UnoRuntime.queryInterface(XStorable.class,xDs);

- >

Unable to cast transparent proxy to type 'unoidl.com.sun.star.frame.XStorable'. 

有没有一种方法能有这个代码正确转换为C#?

否则,您是否知道任何其他资源显示如何在Java中创建Open Office DataSource?

Thx

回答

0

首先,我尝试使用C#并遇到了与您所描述的相同的错误。

然后我尝试了使用Java的示例,并以XStorable的空值结束。所以我认为你的问题不是由于C#引起的,而是因为某些原因,空的数据源没有得到正确的创建。

Create a libreoffice text-based datasource and set settings with java,海报似乎已经成功,所以我不知道当我尝试它时出了什么问题。

此代码打印数据源确实对我有用:https://wiki.openoffice.org/wiki/Documentation/DevGuide/Database/Data_Sources_in_OpenOffice.org_API

相关问题