关于OpenOffice 4.1.6的使用总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ywyngq/article/details/89218729

注意:此片短文仅用于个人的日常技术札记,在网上学习的,分享给大家,贡献给在一线开发程序的java从业人员~

使用excel文档转成IMG文件时,用到了open office,中间一直存在文件找不到的问题:java.io.IOException: Cannot run program "C:\Program": CreateProcess error=2

这一部将在程序中以线程启动的形式实现,不需要额外做调用系统的cmd窗口启动服务。

程序中将实现将该服务启动并在完成所有操作之后关闭服务。也就是说,每次执行改程序都会经过服务启动与服务关闭的过程。如果你觉得这样子对于系统性能有影响,你也可以单独地启动该openoffice服务。

三、  程序代码

package com.sheke.wenku;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import com.artofsolving.jodconverter.DocumentConverter;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class Office2PDF {

    /**

     * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为

     * http://www.openoffice.org/

     *

     * <pre>

     * 方法示例:

     * String sourcePath = "F:\\office\\source.doc";

     * String destFile = "F:\\pdf\\dest.pdf";

     * Converter.office2PDF(sourcePath, destFile);

     * </pre>

     *

     * @param sourceFile

     *            源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,

     *            .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc

     * @param destFile

     *            目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf

     * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,

     *         则表示操作成功; 返回1, 则表示转换失败

     */

    public static int office2PDF(String sourceFile, String destFile) {

        try {

            File inputFile = new File(sourceFile);

            if (!inputFile.exists()) {

                return -1;// 找不到源文件, 则返回-1

            }

            // 如果目标路径不存在, 则新建该路径

            File outputFile = new File(destFile);

            if (!outputFile.getParentFile().exists()) {

                outputFile.getParentFile().mkdirs();

            }

            // 启动OpenOffice的服务

            String command = "C:/OpenOffice 4/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";

            Process pro = Runtime.getRuntime().exec(command);

            // connect to an OpenOffice.org instance running on port 8100

            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);

            connection.connect();

            // convert

            DocumentConverter converter = new OpenOfficeDocumentConverter(

                    connection);

            converter.convert(inputFile, outputFile);

            // close the connection

            connection.disconnect();

            // 关闭OpenOffice服务的进程

            pro.destroy();

            return 0;

        } catch (FileNotFoundException e) {

            e.printStackTrace();

            return -1;

        } catch (IOException e) {

            e.printStackTrace();

        }

        return 1;

    }

注意:command字符串中的oppenoffice程序文件找不到:java.io.IOException: Cannot run program "E:\OpenOffice\openoffice4\program\s(解决办法:把路径中的空格去掉,从新安装一遍)

网络连接问题:java.net.ConnectException: connection failed: socket,host=127.0.0.1,port=8100,tcpNoDelay=1: java.net.ConnectException: Connection refused: connect

未完待续......

猜你喜欢

转载自blog.csdn.net/ywyngq/article/details/89218729