标签:pack sem oid code final .net 自动 int 支持
OpenOffice.org 是一套跨平台的办公室软件套件,能在Windows、Linux、MacOS X (X11)和 Solaris 等操作系统上执行。它与各个主要的办公室软件套件兼容。OpenOffice.org 是自由软件,任何人都可以免费下载、使用及推广它。
目前我已经测试过excel转换pdf已测试成功,word暂时没去测试,理论上是可以转换。因为原理都是调用openoffice的转换pdf功能。
openoffice会自动判断源文件类型和目标文件类型。
工具类支持本地转换和远程调用转换。
并且支持本地文件转换和内存文件(文件已在内存中)两种转换方式
使用时,需要在被调用的机器上安装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.DocumentConverter; 11 import com.artofsolving.jodconverter.DocumentFormat; 12 import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; 13 import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; 14 import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; 15 import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; 16 17 /** 18 * 19 * @author josnow 20 * @date 2017年5月9日 下午12:38:39 21 * @version 1.0.0 22 * @desc openoffice转换工具 23 */ 24 public class OpenOfficeUtils { 25 26 public static final String LOCAL_HOST = "localhost"; 27 public static final int LOCAL_PORT = 8100; 28 29 public static void convert(String inputFileName, String outputFileName) throws ConnectException { 30 convert(inputFileName, outputFileName, LOCAL_HOST, LOCAL_PORT); 31 } 32 33 public static void convert(String inputFileName, String outputFileName, String connectIp, int connectPort) 34 throws ConnectException { 35 if (StringUtils.isEmpty(inputFileName) || StringUtils.isEmpty(outputFileName) 36 || StringUtils.isEmpty(connectIp)) { 37 throw new IllegalArgumentException("参数异常!!"); 38 } 39 OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort); 40 connection.connect(); 41 42 DocumentConverter converter = getConverter(connectIp, connection); 43 44 converter.convert(new File(inputFileName), new File(outputFileName)); 45 connection.disconnect(); 46 } 47 48 public static void convert(InputStream inputStream, DocumentFormat inputFileFormat, OutputStream outputStream, 49 DocumentFormat outputFileFormat) throws ConnectException { 50 convert(inputStream, inputFileFormat, outputStream, outputFileFormat, LOCAL_HOST, LOCAL_PORT); 51 } 52 53 public static void convert(InputStream inputStream, DocumentFormat inputFileFormat, OutputStream outputStream, 54 DocumentFormat outputFileFormat, String connectIp, int connectPort) throws ConnectException { 55 56 if (inputStream == null || inputFileFormat == null || outputStream == null || outputFileFormat == null 57 || StringUtils.isEmpty(connectIp)) { 58 throw new IllegalArgumentException("参数异常!!"); 59 } 60 61 OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort); 62 connection.connect(); 63 DocumentConverter converter = getConverter(connectIp, connection); 64 65 converter.convert(inputStream, inputFileFormat, outputStream, outputFileFormat); 66 connection.disconnect(); 67 } 68 69 private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) { 70 DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp) 71 || "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection) 72 : new StreamOpenOfficeDocumentConverter(connection); 73 return converter; 74 } 75 76 }
转载请标明来源:http://www.cnblogs.com/wulm/p/6962199.html
标签:pack sem oid code final .net 自动 int 支持
原文地址:http://www.cnblogs.com/wulm/p/6962199.html