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. Jbpm工作流(一)

    了解一下什么是Jbpm及特点. jBPM,全称是Java Business Process Management,是一种基于J2EE的轻量级工作流管理系统.jBPM是公开源代码项目,它使用要遵循 Ap ...

  2. linux 下查看wwn号

    PC server主机与FC存储进行连接时,一般需要加装HBA卡,两者之间衔接的一个重要参数就是wwn号.redhat或suse下查看wwn号的方法如下.一.SuSE Linux 9查看 /proc/ ...

  3. Day6_time模块

    TIME模块: print(time.time()) #指的是从1970年到现在的秒数 print(time.strftime('%Y-%m-%d %X'))  #指定特定的格式输出时间 print( ...

  4. IT桔子沙龙之本地生活服务O2O探路者笔记整理

    这个沙龙活动侧重于创业融资与投资角度 主持人:真格基金投资分析师顾女士;E-mail:grace@zhenfund.com;新浪微博:@顾三小姐 赞助商七牛云的服务宣传:静态资源托管:上传下载全网加速 ...

  5. 前端CDN公共库整理

    转自: 灰狼博客, 地址: http://itlobo.com/articles/2016.html 现在web应用都在使用js类库,这些类库小的几十K,大的几百K,而国内网络访问速度大家都知道不是那 ...

  6. Java永久代去哪儿了

    http://www.infoq.com/cn/articles/Java-PERMGEN-Removed 在Java虚拟机(以下简称JVM)中,类包含其对应的元数据,比如类的层级信息,方法数据和方法 ...

  7. 今年暑假不AC - HZNU寒假集训

    今年暑假不AC "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@#$%^&a ...

  8. R语言学习 第十篇:操作符

    运算符是R语言中最基础的存在,熟悉运算符的使用,是熟练使用R处理数据的基础,操作符,顾名思义,是对数据进行运算的符号,R有自己的一套操作符,实现变量的赋值,引用,运算等功能. 一,赋值符号 为变量赋值 ...

  9. leetCode刷题(找到两个数组拼接后的中间数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  10. 【原创】整合Spring4+Hibernate4+Struts2时NullPointerException问题解决

    1.开场白 相信SSH初学者肯定遇到过这个问题,但是又是百思不得其解,明白了之后就恍然大悟. 2.问题描述 程序实现过程是UserAction中调用UserService,UserService的实现 ...