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. CXX的OOO编程... 7
10. 文本 表格 图画的相同机制... 9
11. 创建文本、表格和图形... 10
11.1 创建文件的一个好方法... 10
11.2 Writer文件中的文本、表格和图形... 10
11.3 Calc中的文本、表格和图形... 12
OpenOffice.org Developer's Guide
according to the presentation of this guide, we need to get JDK, JAVA/CXX IDE and OpenOffice.org Software Development Kit (SDK).
download it from www.sun.com please.
Eclipse and VC(.Net) are the perfect choice.
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.
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对象生命期管理,对象识别等等。在我眼里,就是个新的语言,它有自己的标准和实现。而最大的好处,也是它出生的目的就是做到编写的东西与平台无关,可以和Java,CXX等语言绑定起来使用、交互。
那么Java是如何绑定上的呢,也就是说如何和OOO的UNO对象,组件,进程等交互的呢,下面代码足以让人直观一下。
首先Bootstrap.bootstrap()取得一个组件上下文XcomponentContext,这个上下文其实就是对office进程的一个“引用”(旨在表意,词不太准),通过这个上下文可以取得一个服务管理器(此时得说说服务,服务管理器这东西了:OOO安装后,肯定有个可执行的office,这就是所谓的服务器,office进程里提供了特定的服务,当然,也有个服务管理器。你写程序,就是要和office交互,来操作odt,ods和odp,这些文件对应MS的doc,xls和ppt。你的程序就是客户端了。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);
}
}
}
下面说怎么跑这个小程序,我用的是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服务器的上下文,上面说了,OOO的UNO有自己的网络通信协议,可以通过指定IP地址和端口来取得对远端office的引用,哎,这点有点意思,这样可以,比如,在Applet中来操作远端的office。当然,写成JSP,Ajax等都是可以的,这种灵活性就是服务带来的好处。下面的代码展示了如何取得远端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);
到此,说了如何取得服务器的引用,接下来,咱们详细说说具体操作ods的方法,其实取得对服务器的引用后,接下的的代码和Java操作Excel的jxl代码差不多了。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作为一种广泛应用的编程语言,提供了多种库和工具来处理这样的需求。本话题主要关注如何使用OpenOffice API在Java环境中将Excel文件转换为PDF格式,同时确保图片等复杂元素能够完美保留。 OpenOffice是一个开源...
Java使用OpenOffice转换Office文档为PDF是一种常见的技术需求,尤其在企业级应用中,为了保持一致性和跨平台兼容性,可能会需要将Word、Excel或PowerPoint文档转换为PDF格式。以下将详细介绍如何在Java环境中利用...
JodConverter一个Java的OpenDocument 文件转换器,可以进行许多文件格式的转换,它利用OpenOffice所提供的转换介面来进行转换工作,它能进行以下的转换工作: 一、Microsoft Office格式转换为OpenDocument,以及...
Java作为一种广泛应用的编程语言,提供了各种库和工具来处理这样的需求。本篇将重点讲解如何利用OpenOffice API在Java环境中将Word文档转换为PDF文件。 OpenOffice是一个开源的办公套件,它提供了一个称为UNO...
在Java编程环境中,OpenOffice API 提供了一个强大的工具集,使得开发者可以利用OpenOffice的处理能力,例如将Word文档转换为PDF格式,以及进行绘图操作。为了实现这些功能,你需要引入特定的JAR(Java Archive)...
1. unoil-3.0.1.jar:Uno接口库,它是OpenOffice与Java之间通信的桥梁,提供了一种方式让Java应用程序能够控制OpenOffice的进程。 2. xstream-1.3.1.jar:XStream是一个用于Java对象到XML和XML到Java对象序列化的库...
Java OpenOffice库是一种用于在Java应用程序中处理Office文档的强大工具,尤其在进行word和Excel到PDF的转换时。OpenOffice提供了API,使得开发者能够通过编程方式处理这些转换任务,而无需依赖用户界面或手动操作。...
Java在线预览PDF技术主要涉及两个关键点:Java编程语言和OpenOffice库。OpenOffice是一个开源的办公软件套件,它包含了一组API,能够处理各种文档格式,包括PDF。在Java环境中,我们可以利用OpenOffice的API来实现...
在Java编程中,有时我们需要与操作系统进行交互,例如在服务器端执行系统命令,这通常通过Java的`Runtime.getRuntime().exec()`或`ProcessBuilder`类来实现。在这个特定的问题"Java调用Linux CentOS系统安装的...
下载一个Openoffice软件下载到电脑然后安装,具体如何安装请百度一下。分析一下代码的功能;Entry.java这个类的原理是先通过OfficeToPDF.java把文档转成pdf,然后再通过Pdf2Jpg.java这个类把PDF转成JPG或者PNG,想转成...
java实现附件预览(openoffice+PDF.js),将office文档,通过openoffice工具转换为PDF文件,使用PDF.js进行前端展示 是对openoffice+swftools+flexpaper的升级版,减少一次swf文件转换,及flexpaper只能预览十页内容...
**OpenOffice API**是编程中最核心的部分之一,它提供了访问**OpenOffice.org**各种组件的方法。以下是一些主要组件及其相关功能的简要介绍: 1. **文档管理** - **文本文档**:支持创建、编辑和格式化文本文档。 ...
OpenOffice SDK
1. **unoil-5.4.2.jar**:这是OpenOffice UNO(Universal Network Objects)接口库的一部分,提供了对OpenOffice编程接口的访问,使得Java应用程序能够调用OpenOffice的功能,如打开、编辑和保存文档。 2. **ridl-...
word、excel、txt文件,浏览器预览功能实现工具类,OpenOfficeUtil,openoffice插件!
4. **Java UNO Bridge**: Java UNO Bridge是OpenOffice提供的一个组件,它使得Java应用程序可以使用UNO(Universal Network Objects)接口与OpenOffice的内部对象进行通信,从而实现文件转换等功能。 5. **转换过程...
PS:我下载了2天,第一天下载到一半中断了,第二天下载快完成了 又自动重新下载,10K左右的速度,下载100多M的文件,总共几个小时,从Apache官网下载的。全网找了很久都没找到最新版。 贡献出来,大家方便下载。这个...
JODConverter是基于OpenOffice的Java库,允许我们通过编程方式调用OpenOffice的转换功能。我们需要在Java项目中引入JODConverter的依赖库,然后创建一个OfficeManager实例,设置OpenOffice的路径和服务端口,接着就...
在Java编程环境下,我们可以利用OpenOffice的API来实现文件转换功能,将文档转换为PDF或SWF格式。这在很多场景下非常有用,比如自动化文档处理、报表生成或在线预览。 首先,我们要了解OpenOffice提供的Java接口,...
Java作为一种广泛使用的编程语言,提供了多种方式来处理这种需求。本篇文章将详细介绍如何利用OpenOffice库在Java环境中将Word、Excel、PowerPoint等Microsoft Office文档转换为PDF格式。 首先,OpenOffice是一款...