OpenOffice.org 是一套跨平台的办公室软件套件,能在Windows、Linux、MacOS X (X11)和 Solaris 等操作系统上执行。它与各个主要的办公室软件套件兼容。OpenOffice.org 是自由软件,任何人都可以免费下载、使用及推广它。

目前我已经测试过excel转换pdf已测试成功,word暂时没去测试,理论上是可以转换。因为原理都是调用openoffice的转换pdf功能。

openoffice会自动判断源文件类型和目标文件类型。

工具类支持本地转换和远程调用转换。!!! 建议使用本地转换,本地转换比远程调用性能更高,远程调用网络延迟100ms以上,主要消耗在(网络延迟、文件传递速度)

并且支持本地文件转换和内存文件(文件已在内存中)两种转换方式

使用时,需要在被调用的机器上安装openoffice,java中导入openoffice相关jar包 jodconverter-2.2.1.jar 即可

代码如下

 package com.cigna.hmc.groupinsurance.utils.excel;

 import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException; import org.apache.commons.lang.StringUtils; import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormatRegistry;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; /**
*
* @author josnow
* @date 2017年5月9日 下午12:38:39
* @version 1.0.0
* @desc openoffice转换工具
*/
public class OpenOfficeUtils { public static final String LOCAL_HOST = "localhost";
public static final int LOCAL_PORT = 8100; // Format
public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry(); /**
*
* @desc
* @auth josnow
* @date 2017年6月9日 下午4:11:04
* @param inputFilePath
* 待转换的文件路径
* @param outputFilePath
* 输出文件路径
*/
public static void convert(String inputFilePath, String outputFilePath) throws ConnectException {
convert(inputFilePath, outputFilePath, LOCAL_HOST, LOCAL_PORT);
} /**
*
* @desc
* @auth josnow
* @date 2017年6月9日 下午4:12:29
* @param inputFilePath
* 待转换的文件路径
* @param outputFilePath
* 输出文件路径
* @param connectIp
* 远程调用ip
* @param connectPort
* 远程调用端口
*/
public static void convert(String inputFilePath, String outputFilePath, String connectIp, int connectPort)
throws ConnectException {
if (StringUtils.isEmpty(inputFilePath) || StringUtils.isEmpty(outputFilePath)
|| StringUtils.isEmpty(connectIp)) {
throw new IllegalArgumentException("参数异常!!");
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
connection.connect(); DocumentConverter converter = getConverter(connectIp, connection); converter.convert(new File(inputFilePath), new File(outputFilePath));
connection.disconnect();
} /**
*
* @desc
* @auth josnow
* @date 2017年6月9日 下午4:08:26
* @param inputStream
* @param inputFileExtension
* 待转换文件的扩展名,例如: xls,doc
* @param outputStream
* @param outputFileExtension
* 输出文件扩展名,例如:pdf
*/
public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
String outputFileExtension) throws ConnectException {
convert(inputStream, inputFileExtension, outputStream, outputFileExtension, LOCAL_HOST, LOCAL_PORT);
} /**
*
* @desc
* @auth josnow
* @date 2017年6月9日 下午4:10:21
* @param inputStream
* @param inputFileExtension
* 待转换文件的扩展名,例如: xls,doc
* @param outputStream
* @param outputFileExtension
* 输出文件扩展名,例如:pdf
* @param connectIp
* 远程调用ip
* @param connectPort
* 远程调用端口
*/
public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
String outputFileExtension, String connectIp, int connectPort) throws ConnectException { if (inputStream == null || StringUtils.isEmpty(inputFileExtension) || outputStream == null
|| StringUtils.isEmpty(outputFileExtension) || StringUtils.isEmpty(connectIp)) {
throw new IllegalArgumentException("参数异常!!");
} OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
connection.connect();
DocumentConverter converter = getConverter(connectIp, connection); converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream,
formatFactory.getFormatByFileExtension(outputFileExtension));
connection.disconnect();
} private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {
DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp)
|| "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection)
: new StreamOpenOfficeDocumentConverter(connection);
return converter;
} }

转载请标明来源:http://www.cnblogs.com/wulm/p/6962199.html

openoffice excel word 转换pdf 支持本地调用和远程调用的更多相关文章

  1. Aspose 强大的服务器端 excel word ppt pdf 处理工具

    Aspose 强大的服务器端 excel word ppt pdf 处理工具 http://www.aspose.com/java/word-component.aspx

  2. SharePoint 2013 Word 转换PDF服务介绍及示例

    前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有 ...

  3. SharePoint Word 转换PDF服务介绍及示例

    前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有 ...

  4. Jacob工具类使用文件互转服务 word转html html转excel word转pdf excel转pdf ppt转pdf

    前提条件  必须安装MS office 1.jdk使用jdk1.8 2.jacob.dll放在..\jdk1.8\jre\bin目录下 3.eclipse的jre版本要和jdk一致,window-&g ...

  5. Java用OpenOffice将word转换为PDF

    一.      软件安装以及jar包下载 官网的下载地址如下(英文): OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http: ...

  6. excel,word,ppt,pdf,swf 文件互相转换

    转自:  http://www.cnblogs.com/wolf-sun/p/3569960.html 引言 之前项目需要,查找了office文档在线预览的解决方案,顺便记录一下,方便以后查询. 方案 ...

  7. PHP 实现Word,excel等转换pdf

    近期做一个项目,须要将用户上传的word,excel文档转成PDF文档保存并打印.在网上找了非常多资料.并不全面,所以自己写了一份比較全面的教程来分享. 以下是操作步骤: 1.        安装免费 ...

  8. .net调用word转换pdf出现80080005错误的解决办法

    检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80080005. 1:在服务器上安装offi ...

  9. #c word转换PDF

    需要引用Microsoft.Office.Interop.Word,版本是07之上的. 这个版本会判断文件是否被占用. using Microsoft.Office.Interop.Word; usi ...

随机推荐

  1. 没人看系列----css 随笔

    目录 没人看系列----css 随笔 没人看系列----html随笔 前言 没什么要说的就是自己总结,学习用的如果想学点什么东西,请绕行. CSS (Cascading Style Sheets)层叠 ...

  2. ruby簡單的代碼行統計工具

    看代码 # encoding: utf-8 class CodeLineStat attr_reader :code_lines def initialize @code_lines = 0 end ...

  3. RPi:QT+wiringPi demo程序

    一个项目里面要用到这玩意儿,网上查了几篇文章凑出来最后还是不行,自己灵机一动就成了. 今天再次搜索的时候,发现另一篇文章已经讲明白了,真是欲哭无泪 程序大部分参考的是之前学qt的摸索出来的,其实只要在 ...

  4. linux环境安装svn并进行多个源码库区分管理

    关于svn的文档有很多大部分已Windows为例子,因公司没有Windows服务器经过一天的曲折终于初步安装了解了svn.下面一些经验希望能帮助新手 本文采用的yum安装(简单快速没必要源码) 1.y ...

  5. 如何用plugman编辑和添加cordova插件

    1.安装工具 进入nodejs, 安装工具plugman,管理插件,输入命令npm install -g plugman 等待下载安装 2.使用plugman命令生成插件框架 cmd 进入用于生成插件 ...

  6. C#System.Text.RegularExpressions.Regex使用(一) .

    需要引入命名空间 using System.Text.RegularExpressions;(若不引入,则写Regex时要写成 System.Text.RegularExpressions.Regex ...

  7. Pycharm快捷键记录

    这里只记录自己用过的,记录而已 会慢慢添加进来,没有考虑分类和顺序,后期足够多了会整理 参考文章: 1. pycharm的一些快捷键 2. pycharm快捷键及一些常用设置 Ctrl+C  直接复制 ...

  8. Python:怎样用线程将任务并行化?

    如果待处理任务满足: 可拆分,即任务可以被拆分为多个子任务,或任务是多个相同的任务的集合: 任务不是CPU密集型的,如任务涉及到较多IO操作(如文件读取和网络数据处理) 则使用多线程将任务并行运行,能 ...

  9. 字符串转json以及获取域名的参数

    本例将通过location.search来进行字符串拼接成json以及查询域名参数的value console.log(request('id')); function request(obj){ v ...

  10. post 和 get 的区别,直指本质

    在我们初入java编程之路的时候,面试往往会有一个面试题:get和post的区别是什么?那么你真的知道他们的区别吗?接下来抽丝剥茧,让我们看看get和post到底什么东西,首先从本质的角度看get和p ...