六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 92|回复: 0

OpenOffice学习笔记一连接OppenOffice

[复制链接]

升级  46%

31

主题

31

主题

31

主题

秀才

Rank: 2

积分
119
 楼主| 发表于 2013-1-15 02:19:20 | 显示全部楼层 |阅读模式
其实网上已经有不少关于.Openoffice使用的文章的.我这里只不过是画蛇添足多此一举.但是.因为长期不添足.现在说都不会话了.还是写点吧.希望对你有帮助.我最讨厌废话连篇了.所以不说废话了.还有我的表达能力有限.希望大家能凑合看.不要有不清楚的地方.可以留言讨论.也可以给我发邮件..(本人邮箱垃圾邮件堆满了,不一定能看到.)现在开始讲了.还有,没有用中文标点.这属于个人习惯吧.算了废话又来了开始讲了哦.

首先需要安装Openoffice主程序.
去Openoffice主站下载.
然后安装Openofficejdk.

从jdk的example文件夹中找到了学习步骤...就按照他的走吧
下载是摘自他的源码
/************************************************************************* * *  The Contents of this file are made available subject to the terms of *  the BSD license. *   *  Copyright 2000, 2010 Oracle and/or its affiliates. *  All rights reserved. * *  Redistribution and use in source and binary forms, with or without *  modification, are permitted provided that the following conditions *  are met: *  1. Redistributions of source code must retain the above copyright *     notice, this list of conditions and the following disclaimer. *  2. Redistributions in binary form must reproduce the above copyright *     notice, this list of conditions and the following disclaimer in the *     documentation and/or other materials provided with the distribution. *  3. Neither the name of Sun Microsystems, Inc. nor the names of its *     contributors may be used to endorse or promote products derived *     from this software without specific prior written permission. * *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *      *************************************************************************/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 );        }        catch (java.lang.Exception e){            e.printStackTrace();        }        finally {            System.exit(0);        }    }}

你运行吧肯定报错..绝对报错.
因为他的默认路径是找不到的..估计环境变量中得设置..
于是参考了一个哥们的笔记
http://www.xue5.com/Office/OpenOffice/441298_4.html


下载了个bootstrapconnector.jar


代码修改成如下:

package ansj.sun.os;import ooo.connector.BootstrapSocketConnector;import com.sun.star.lang.XMultiComponentFactory;import com.sun.star.uno.XComponentContext;public class ConnOpenOffice { public static void main(String[] args) {        try {            // 连接OpenOffice        String oooExecFolder = "C:/Program Files/OpenOffice.org 3/program/" ;            XComponentContext xContext = BootstrapSocketConnector.bootstrap(oooExecFolder) ;            System.out.println("Connected to a running office ...");            XMultiComponentFactory xMCF =  xContext.getServiceManager();            String available = (xMCF != null ? "available" : "not available");            System.out.println( "remote ServiceManager is " + available );        }        catch (java.lang.Exception e){            e.printStackTrace();        }        finally {            System.exit(0);        }    }}

运行..发生错误如下...
com.sun.star.comp.helper.BootstrapExceptionat ooo.connector.BootstrapConnector.connect(BootstrapConnector.java:129)at ooo.connector.BootstrapSocketConnector.connect(BootstrapSocketConnector.java:68)at ooo.connector.BootstrapSocketConnector.connect(BootstrapSocketConnector.java:45)at ooo.connector.BootstrapSocketConnector.bootstrap(BootstrapSocketConnector.java:82)at ansj.sun.os.ConnOpenOffice.main(ConnOpenOffice.java:13)

于是你打开任务管理器看看是否有
office.bin 和 office.exe 这两个进程,如果没有那就是因为Openoffice的服务没有启动.
你需要cmd到Openoffice主程序的安装目录下
cd C:\Program Files\OpenOffice.org 3\program
运行如下命令
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

如果没有保存哦.你再看任务管理器的进程是不是多出了office.bin 和 office.exe

现在重新运行程序

Connected to a running office ...remote ServiceManager is available

出现如下..呵呵成功了...
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表