openoffice excel word 转换pdf 支持本地调用和远程调用

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

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

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

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

并且支持本地文件转换和内存文件(不走本地磁盘io)两种转换方式

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

代码如下

  1 package com.cigna.hmc.groupinsurance.utils.excel;
  2 
  3 import java.io.File;
  4 import java.io.InputStream;
  5 import java.io.OutputStream;
  6 import java.net.ConnectException;
  7 
  8 import org.apache.commons.lang.StringUtils;
  9 
 10 import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
 11 import com.artofsolving.jodconverter.DocumentConverter;
 12 import com.artofsolving.jodconverter.DocumentFormatRegistry;
 13 import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
 14 import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
 15 import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
 16 import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
 17 
 18 /**
 19  * 
 20  * @author josnow
 21  * @date 2017年5月9日 下午12:38:39
 22  * @version 1.0.0
 23  * @desc openoffice转换工具
 24  */
 25 public class OpenOfficeUtils {
 26 
 27     public static final String LOCAL_HOST = "localhost";
 28     public static final int LOCAL_PORT = 8100;
 29 
 30     // Format
 31     public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry();
 32 
 33     /**
 34      * 
 35      * @desc
 36      * @auth josnow
 37      * @date 2017年6月9日 下午4:11:04
 38      * @param inputFilePath
 39      *            待转换的文件路径
 40      * @param outputFilePath
 41      *            输出文件路径
 42      */
 43     public static void convert(String inputFilePath, String outputFilePath) throws ConnectException {
 44         convert(inputFilePath, outputFilePath, LOCAL_HOST, LOCAL_PORT);
 45     }
 46 
 47     /**
 48      * 
 49      * @desc
 50      * @auth josnow
 51      * @date 2017年6月9日 下午4:12:29
 52      * @param inputFilePath
 53      *            待转换的文件路径
 54      * @param outputFilePath
 55      *            输出文件路径
 56      * @param connectIp
 57      *            远程调用ip
 58      * @param connectPort
 59      *            远程调用端口
 60      */
 61     public static void convert(String inputFilePath, String outputFilePath, String connectIp, int connectPort)
 62             throws ConnectException {
 63         if (StringUtils.isEmpty(inputFilePath) || StringUtils.isEmpty(outputFilePath)
 64                 || StringUtils.isEmpty(connectIp)) {
 65             throw new IllegalArgumentException("参数异常!!");
 66         }
 67         OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
 68         connection.connect();
 69 
 70         DocumentConverter converter = getConverter(connectIp, connection);
 71 
 72         converter.convert(new File(inputFilePath), new File(outputFilePath));
 73         connection.disconnect();
 74     }
 75 
 76     /**
 77      * 
 78      * @desc
 79      * @auth josnow
 80      * @date 2017年6月9日 下午4:08:26
 81      * @param inputStream
 82      * @param inputFileExtension
 83      *            待转换文件的扩展名,例如: xls,doc
 84      * @param outputStream
 85      * @param outputFileExtension
 86      *            输出文件扩展名,例如:pdf
 87      */
 88     public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
 89             String outputFileExtension) throws ConnectException {
 90         convert(inputStream, inputFileExtension, outputStream, outputFileExtension, LOCAL_HOST, LOCAL_PORT);
 91     }
 92 
 93     /**
 94      * 
 95      * @desc
 96      * @auth josnow
 97      * @date 2017年6月9日 下午4:10:21
 98      * @param inputStream
 99      * @param inputFileExtension
100      *            待转换文件的扩展名,例如: xls,doc
101      * @param outputStream
102      * @param outputFileExtension
103      *            输出文件扩展名,例如:pdf
104      * @param connectIp
105      *            远程调用ip
106      * @param connectPort
107      *            远程调用端口
108      */
109     public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
110             String outputFileExtension, String connectIp, int connectPort) throws ConnectException {
111 
112         if (inputStream == null || StringUtils.isEmpty(inputFileExtension) || outputStream == null
113                 || StringUtils.isEmpty(outputFileExtension) || StringUtils.isEmpty(connectIp)) {
114             throw new IllegalArgumentException("参数异常!!");
115         }
116 
117         OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
118         connection.connect();
119         DocumentConverter converter = getConverter(connectIp, connection);
120 
121         converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream,
122                 formatFactory.getFormatByFileExtension(outputFileExtension));
123         connection.disconnect();
124     }
125 
126     private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {
127         DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp)
128                 || "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection)
129                         : new StreamOpenOfficeDocumentConverter(connection);
130         return converter;
131     }
132 
133 }

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

原文地址:https://www.cnblogs.com/wulm/p/6962199.html