輸出資料到OpenOffice Scalc

摘要:輸出資料到OpenOffice Scalc

兩種方法(參考 http://www.opendocument4all.com/download/OpenOffice.net.pdf)

1) 透過 OpenOffice 提供的API

須注意安裝的SDK版本不可比安裝的OpenOffice軟體版本新

安裝SDK後,將dll加入參考

SDK 3.0 dll 檔案位於(%InstallPath%/sdk/cli/)

 

using unoidl.com.sun.star.container;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.uno;

XComponentContext localContext = uno.util.Bootstrap.bootstrap();
XMultiServiceFactory multiServiceFactory = (XMultiServiceFactory) localContext.getServiceManager();
XComponentLoader componentLoader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
XSpreadsheetDocument workbook = (XSpreadsheetDocument)componentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, new unoidl.com.sun.star.beans.PropertyValue[0]);
XSpreadsheet sheet = getSpreadsheet(workbook,0);
sheet.getCellByPosition(0, 0).setFormula("TEST");

2) Reflection

 

using System.Reflection;

Type tServiceManager = Type.GetTypeFromProgID("com.sun.star.ServiceManager", true);
Object oServiceManager = Activator.CreateInstance(tServiceManager);
Object oDesktop = oServiceManager.GetType().InvokeMember("createinstance", BindingFlags.InvokeMethod, null, oServiceManager, new object[] { "com.sun.star.frame.Desktop" });
object oComponent = oDesktop.GetType().InvokeMember("loadComponentFromUrl", BindingFlags.InvokeMethod, null, oDesktop, new object[] { "private:factory/scalc" ,"_blank",0,new Object[] { }});
object sheets = oComponent.GetType().InvokeMember("getSheets", BindingFlags.InvokeMethod, null, oComponent, null);
object sheet = sheets.GetType().InvokeMember( "getByIndex", BindingFlags.InvokeMethod,null,sheets, new object[]{0});
object cell = sheet.GetType().InvokeMember( "getCellByPosition", BindingFlags.InvokeMethod,null,sheet, new object[] { 0, 0 });
cell.GetType().InvokeMember("setFormula", BindingFlags.InvokeMethod,null,cell ,new object[]{"TEST"});

開發環境

VS 2008 C# Express

OpenOffice 3.1

OpenOffice SDK 3.0.0