开始Java,C++之OpenOffice编程 (Start OpenOffice Programming In Java or Cpp ) 之一

阅读更多

Java/CXX OpenOffice编程... 1

1. JDK. 1

2. JAVA/CXX IDE. 1

3. OOo SDK. 1

4. Setting IDE. 2

5. 一个Demo程序... 2

6. Eclipse下运行Demo. 3

7. 取得远端服务器上下文... 3

8. Java具体操作ods文件... 4

9. CXXOOO编程... 7

10. 文本 表格 图画的相同机制... 9

11. 创建文本、表格和图形... 10

11.1 创建文件的一个好方法... 10

11.2 Writer文件中的文本、表格和图形... 10

11.3 Calc中的文本、表格和图形... 12

OpenOffice.org Developer's Guide

Java/CXX OpenOffice编程

according to the presentation of this guide, we need to get JDK, JAVA/CXX IDE and OpenOffice.org Software Development Kit (SDK).

1. JDK

download it from www.sun.com please.

2. JAVA/CXX IDE

Eclipse and VC(.Net) are the perfect choice.

3. OOo SDK

this is the point I need to show detail info about it.

http://download.openoffice.org/sdk/index.htmlfor downloading 3.1.1 OOo SDK now (20100108).

http://download.openoffice.org/2.4.0/sdk.htmlfor downloading 2.4.0 OOo SDK now.

http://download.openoffice.org/3.0.0/sdk.htmlfor downloading 3.0.0 OOo SDK now.

4. Setting IDE

if you are interesting of CXX for programming with OOo SDK and so on, here is the good drink for you:

http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK

http://wiki.services.openoffice.org/wiki/SDKCppLanguage

if you are good at JAVA, here is the guide for you too:

http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/FirstSteps/Configuration

5. 一个Demo程序

OOO通过UNO实现了语言无关和平台无关的机能。UNO自己定义了自己的数据类型,接口定义方式,服务,结构,数组(序列),模块,异常,Singleton,继承,实现等,还有自己的通信协议。用这些可以实现进程间通信,服务管理,组件管理,属性集合管理,事件模型,异常处理,UNO对象生命期管理,对象识别等等。在我眼里,就是个新的语言,它有自己的标准和实现。而最大的好处,也是它出生的目的就是做到编写的东西与平台无关,可以和JavaCXX等语言绑定起来使用、交互。

那么Java是如何绑定上的呢,也就是说如何和OOOUNO对象,组件,进程等交互的呢,下面代码足以让人直观一下。

首先Bootstrap.bootstrap()取得一个组件上下文XcomponentContext,这个上下文其实就是对office进程的一个“引用”(旨在表意,词不太准),通过这个上下文可以取得一个服务管理器(此时得说说服务,服务管理器这东西了:OOO安装后,肯定有个可执行的office,这就是所谓的服务器,office进程里提供了特定的服务,当然,也有个服务管理器。你写程序,就是要和office交互,来操作odtodsodp,这些文件对应MSdocxlsppt。你的程序就是客户端了。OOO采用这种CS模式有好处,好处不在这说,咱说怎么用。所以你要操作OOO文件就得取得一些特定的服务,取得服务后,才能操作具体的组件啊,模块啊什么的。)通过服务管理器可以取得一个叫Desktop的服务,这个服务可以取得组件装载器的一个实例(XComponentLoader ),有了这个XComponentLoader 实例,我们就可以操作具体的组件了。先看看怎么实现这几步。

import com.sun.star.frame.XComponentLoader;

import com.sun.star.uno.UnoRuntime;

public class FirstUnoContact {

public static void main(String[] args) {

try {

// get the remote office component context

com.sun.star.uno.XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();

System.out.println("Connected to a running office ...");

com.sun.star.lang.XMultiComponentFactory xMCF = xContext.getServiceManager();

String available = (xMCF != null ? "available" : "not available");

System.out.println("remote ServiceManager is " + available);

Object desktop = xMCF.createInstanceWithContext(

"com.sun.star.frame.Desktop", xContext);

XComponentLoader xComponentLoader = (XComponentLoader)

UnoRuntime.queryInterface(XComponentLoader.class, desktop);

System.out.println("OK Done");

} catch (java.lang.Exception e) {

e.printStackTrace();

} finally {

System.exit(0);

}

}

}

6. Eclipse下运行Demo

下面说怎么跑这个小程序,我用的是Eclipse来跑的。OOO推荐Netbeans来跑,因为Sun公司给其做了插件,可以很方便。无所谓啦,陪陪类路径,俺还是有耐心的,具体步骤如下。

1. create a new java project.

2. Window-> Preferences->Java->BuildePath->UserLibraries->New create a new library with name, as ooo

add jar... (as on windows platform)

choose C:\Program Files\OpenOffice.org 3\URE\java

choose C:\Program Files\OpenOffice.org 3\Basis\program\classes

3. add class path for your new Java project.

right click project name in PackageExplorer View -> Properties -> Java Build Path -> choose Libraries tab

-> Add External Class Folder... -> choose C:\Program Files\OpenOffice.org 3\program -> OK

4. just run the code up as java application and you will see output msg.

7. 取得远端服务器上下文

默认情况时取得本机的office服务器的上下文,上面说了,OOOUNO有自己的网络通信协议,可以通过指定IP地址和端口来取得对远端office的引用,哎,这点有点意思,这样可以,比如,在Applet中来操作远端的office。当然,写成JSPAjax等都是可以的,这种灵活性就是服务带来的好处。下面的代码展示了如何取得远端office服务器的上下文。

import com.sun.star.uno.XComponentContext;

import com.sun.star.comp.helper.Bootstrap;

import com.sun.star.lang.XMultiComponentFactory;

import com.sun.star.bridge.UnoUrlResolver;

import com.sun.star.bridge.XUnoUrlResolver;

import com.sun.star.beans.XPropertySet

import com.sun.star.uno.UnoRuntime;

XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null);

// create a connector, so that it can contact the office

XUnoUrlResolver urlResolver = UnoUrlResolver.create(xcomponentcontext);

Object initialObject = urlResolver.resolve(

"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");

XMultiComponentFactory xOfficeFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(

XMultiComponentFactory.class, initialObject);

// retrieve the component context as property (it is not yet exported from the office)

// Query for the XPropertySet interface.

XPropertySet xProperySet = (XPropertySet) UnoRuntime.queryInterface(

XPropertySet.class, xOfficeFactory);

// Get the default context from the office server.

Object oDefaultContext = xProperySet.getPropertyValue("DefaultContext");

// Query for the interface XComponentContext.

XComponentContext xOfficeComponentContext = (XComponentContext) UnoRuntime.queryInterface(

XComponentContext.class, oDefaultContext);

// now create the desktop service

// NOTE: use the office component context here!

Object oDesktop = xOfficeFactory.createInstanceWithContext(

"com.sun.star.frame.Desktop", xOfficeComponentContext);

8. Java具体操作ods文件

到此,说了如何取得服务器的引用,接下来,咱们详细说说具体操作ods的方法,其实取得对服务器的引用后,接下的的代码和Java操作Exceljxl代码差不多了。OOO的程序其实可以远比jxl来得复杂,因为有了UNO,可以做很多事情。好,下面看看Java操作ods的方法。

import com.sun.star.beans.PropertyValue;

import com.sun.star.beans.XPropertySet;

import com.sun.star.uno.XComponentContext;

import com.sun.star.comp.helper.Bootstrap;

import com.sun.star.container.XEnumeration;

import com.sun.star.container.XEnumerationAccess;

import com.sun.star.frame.XComponentLoader;

import com.sun.star.frame.XController;

import com.sun.star.frame.XModel;

import com.sun.star.lang.XComponent;

import com.sun.star.lang.XMultiComponentFactory;

import com.sun.star.sheet.XCellAddressable;

import com.sun.star.sheet.XCellRangesQuery;

import com.sun.star.sheet.XSheetCellRanges;

import com.sun.star.sheet.XSpreadsheet;

import com.sun.star.sheet.XSpreadsheetDocument;

import com.sun.star.sheet.XSpreadsheetView;

import com.sun.star.sheet.XSpreadsheets;

import com.sun.star.table.XCell;

import com.sun.star.uno.UnoRuntime;

public class FirstLoadComponent {

/** Creates a new instance of FirstLoadComponent */

public FirstLoadComponent() {

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

try {

// get the remote office component context

XComponentContext xRemoteContext = Bootstrap.bootstrap();

if (xRemoteContext == null) {

System.err.println("ERROR: Could not bootstrap default Office.");

}

XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager();

Object desktop = xRemoteServiceManager.createInstanceWithContext(

"com.sun.star.frame.Desktop", xRemoteContext);

XComponentLoader xComponentLoader = (XComponentLoader)

UnoRuntime.queryInterface(XComponentLoader.class, desktop);

PropertyValue[] loadProps = new PropertyValue[0];

XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps);

XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)

UnoRuntime.queryInterface(XSpreadsheetDocument.class,

xSpreadsheetComponent);

XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();

xSpreadsheets.insertNewByName("MySheet", (short)0);

com.sun.star.uno.Type elemType = xSpreadsheets.getElementType();

System.out.println(elemType.getTypeName());

font-size: 9pt; font-family: Fixedsy

你可能感兴趣的:(Java,编程,C#,C++,C)