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转换office为PDF, 内附有jar包,java实现,OpenOffice下载地址,及OpenOffice在Linux下的安装实现。
JodConverter一个Java的OpenDocument 文件转换器,可以进行许多文件格式的转换,它利用OpenOffice所提供的转换介面来进行转换工作,它能进行以下的转换工作: 一、Microsoft Office格式转换为OpenDocument,以及...
java使用OpenOffice实现的Excel转pfd,Excel中存在图片也可以完美的完成转换
java实现附件预览(openoffice+PDF.js),将office文档,通过openoffice工具转换为PDF文件,使用PDF.js进行前端展示 是对openoffice+swftools+flexpaper的升级版,减少一次swf文件转换,及flexpaper只能预览十页内容...
下载一个Openoffice软件下载到电脑然后安装,具体如何安装请百度一下。分析一下代码的功能;Entry.java这个类的原理是先通过OfficeToPDF.java把文档转成pdf,然后再通过Pdf2Jpg.java这个类把PDF转成JPG或者PNG,想转成...
PS:我下载了2天,第一天下载到一半中断了,第二天下载快完成了 又自动重新下载,10K左右的速度,下载100多M的文件,总共几个小时,从Apache官网下载的。全网找了很久都没找到最新版。 贡献出来,大家方便下载。这个...
OpenOffice SDK
word、excel、txt文件,浏览器预览功能实现工具类,OpenOfficeUtil,openoffice插件!
java调用openOffice转换office为pdf的jar包
java OpenOffice wordExcel转换PDF.zip 带jar包
最全JAVA实现Office在线预览调研,OpenOffice,LibreOffice,POI等已有技术归纳整理,附带链接。
Apache OpenOffice API 不仅可以与 Apache OpenOffice Basic 一起使用,而且还可以与其他编程语言(如 Java 和 C++)一起使用。这是通过一种称为通用网络对象 (Universal Network Objects, UNO) 的技术实现的,该...
OpenOffice.org提供了多语言的开发API,对于JAVA支持也比较好。资源文件中包括了OpenOffice.org 3.0版本开发的所需要的JAR包文件,同时还有本文总结的关于Eclipse环境下OpenOffice.org开发环境搭建过程,以及说明...
java对openoffice操作,对word等文件转换成PDF,以及画图
我从官网下载openoffice4.1.0版的,资源包含有openoffice相关jar包以及java调用文件和部署文档
该手册提供了对OpenOffice. org Basic的编程办法,介绍了编程语言、运行库、API等相关知识,涉及的软件包括Document、Spreadsheet、Presentation、Charts、Base等。
关于OpenOffice 宏编程的书籍。是英文版的,喜欢的可以下载看看
OPenOffice 实现用Java实现文件转换, 已封装,可直接使用
OpenOffice 实现JPG.DOC转PDF转SWF在线预览(Java版),如果有问题可以联系我
zip包内讲的非常详细。此处就不多讲解,为了凑够50个字符。