2016-12-29 78 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

相關問題