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