2011年2月9日 星期三

Automate OpenOffice from VB.NET

參考網址
Automate OpenOffice from VB.NET
Though from user's point of view OO Calc is very similar to MS Excel, these two products have completelly different API object models. OO offers its own UNO API which is not compatible with MS Office object model at least directly.

There are several ways to program OpenOffice Calc from dot net languages like VB.NET or C#:

1) Use .net UNO bindings or late binding to UNO

2) Use compatibility layer libraries making OO Calc object model to look similar to MS Excel object model. There are several options:

2.1) Etersoft UnoOffice DLL implements a subset of MS Excel COM interface using OO Calc. So it translates calls to MS Excel object model to OO Calc UNO. This library has support even for OO v3 and many properties and methods are already implemented.

You can download an installation package (MSI file) at: http://updates.etersoft.ru/pub/Etersoft/UniOffice@Etersoft/0.4/Windows/

It installs unioffice_excel.dll and unioffice_excel.tlb to C:\WINDOWS\system32\ You can see a list of implemented methods in any COM object browser, for example in VBA by adding a reference to unioffice_excel.tlb. Unioffice_excel.dll contains only implementation code without any metadata. It is even possible to run many different applications expecting MS Excel installed and they will begin to work with OO Calc. Also you can use MS Excel primary interop assembly instead of unioffice_excel.tlb. In such case you may need to cast typed instances of objects like Sheet or Range to an IUnknown using CObj() function in VB.NET, I expect it is related with a lack of duality in some MS Excel interfaces.

A sample VB.NET code:

Dim EA As New Excel.Application
EA.Visible = True
Dim WB As Excel.Workbook = EA.Workbooks.Open("C:\Download\Test.xls")
Dim S As String = WB.ActiveSheet().Cells(1, 1).Value
Debug.Print(S)
EA.Quit()

沒有留言:

張貼留言