我有一個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