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. [ASP.NET MVC4高级编程] 学习记录(一)

    理论: 先有GUI在发展,当用户按下某个键,某个进程会监听到这个动作,这个进程就是控制器.这就是MVC模式. 后来有了事件驱动编程,响应动作的是按钮本身,而不是控制器. 再后来webForm中,事件驱 ...

  2. Day4_生成器_三元表达式_列表解析_生成器表达式

    生成器:在函数内部包含yield关键,那么该函数执行的结果就是生成器. 生成器就是迭代器. def func(): print('first') yield 111111 print('second' ...

  3. nginx 反向代理,支持跨域,前后分离

    前端开发往往涉及到跨域问题,其中解决方案很多: 1.jsonp 需要目标服务器配合一个callback函数. 2.window.name+iframe 需要目标服务器响应window.name. 3. ...

  4. java内存溢出(二)

    一.有哪些内存溢出异常? OutOfMemoryError: 当堆.栈(多线程场景).方法区(永久区).本地内存(元数据.直接内存),数据容量达到最大时产生 StackOverFlowError: 线 ...

  5. Mysql 快速指南

    Mysql 快速指南 本文的示例在 Mysql 5.7 下都可以测试通过. 知识点 概念 数据库(database):保存有组织的数据的容器(通常是一个文件或一组文件). 数据表(table):某种特 ...

  6. 一天搞懂深度学习-训练深度神经网络(DNN)的要点

    前言 这是<一天搞懂深度学习>的第二部分 一.选择合适的损失函数 典型的损失函数有平方误差损失函数和交叉熵损失函数. 交叉熵损失函数: 选择不同的损失函数会有不同的训练效果 二.mini- ...

  7. C# Dispose模式详细分析

    C#Dispose模式 目的: 为了及时释放宝贵的非托管资源和托管资源,并且保证资源在被gc回收的时候可以正确释放资源,同时兼顾执行效率 必须遵循的事实: 1 托管资源释放: 由另一线程的gc进行释放 ...

  8. C#高级编程笔记之第二章:核心C#

    变量的初始化和作用域 C#的预定义数据类型 流控制 枚举 名称空间 预处理命令 C#编程的推荐规则和约定 变量的初始化和作用域 初始化 C#有两个方法可以一确保变量在使用前进行了初始化: 变量是字段, ...

  9. mac终端命令及pycharm常用快捷键记录

    mac终端命令: 1.root权限 $sudo su - 2.定位到指定文件夹位置 $cd /Users/计算机名称/Desktop     (定位到桌面) 3.新建文件夹 $mkdir 文件夹名称 ...

  10. python 匿名函数与三元运算

    匿名函数 匿名函数就是不需要显示式的指定函数名 首先看一行代码: def calc(x,y): return x*y print(calc(2,3)) # 换成匿名函数 calc = lambda x ...