- 运行环境:win10、eclipse mars、java8 所需相关jar包下载地址
- openoffice下载地址 转换之前请先安装openoffice,然后在软件安装目录的progrom目录下:使用命令启动相关服务: soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard
- 相关代码
package com.sun.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
* @description 文件转换工具类
* @author sunshanjin
*
*/
public class Doc2HtmlUtils {
private static Doc2HtmlUtils doc2HtmlUtils;
/**
* 获取Doc2HtmlUtil实例
*/
public static synchronized Doc2HtmlUtils getDoc2HtmlUtilsInstance() {
if (doc2HtmlUtils == null) {
doc2HtmlUtils = new Doc2HtmlUtils();
}
return doc2HtmlUtils;
}
/**
* @description dox,docx,xls,ppt转html
* @param file
* @return 生成的html文件名称 xxx.html
* @throws IOException
*/
public String file2html(File file) throws IOException {
if (!file.exists()) {
throw new FileNotFoundException("没有找到该文件:" + file.getPath());
}
String filename = file.getName();
File htmlFile = null;
if (filename.endsWith("docx")) {
htmlFile = new File(file.getParent() + File.separator + filename.replace(".docx", ".html"));
}else if (filename.endsWith("doc")) {
htmlFile = new File(file.getParent() + File.separator + filename.replace(".doc", ".html"));
}else if (filename.endsWith("xls")) {
htmlFile = new File(file.getParent() + File.separator + filename.replace(".xls", ".html"));
}else if (filename.endsWith("ppt")) {
htmlFile = new File(file.getParent() + File.separator + filename.replace(".ppt", ".html"));
}
if (!htmlFile.exists()) {
htmlFile.createNewFile();
}else{
return htmlFile.getName();
}
this.convent(file, htmlFile);
//返回生成的html文件名
return htmlFile.getName();
}
/**
* @description dox,docx,xls,ppt转pdf
* @param file
* @return 生成的pdf文件名称xxx.pdf
* @throws IOException
*/
public String file2pdf(File file) throws IOException {
if (!file.exists()) {
throw new FileNotFoundException("没有找到该文件:" + file.getPath());
}
String filename = file.getName();
File pdfFile = null;
if (filename.endsWith("docx")) {
pdfFile = new File(file.getParent() + File.separator + filename.replace(".docx", ".pdf"));
}else if (filename.endsWith("doc")) {
pdfFile = new File(file.getParent() + File.separator + filename.replace(".doc", ".pdf"));
}else if (filename.endsWith("xls")) {
pdfFile = new File(file.getParent() + File.separator + filename.replace(".xls", ".pdf"));
}else if(filename.endsWith("xlsx")){
pdfFile = new File(file.getParent() + File.separator + filename.replace(".xlsx", ".pdf"));
}else if (filename.endsWith("ppt")) {
pdfFile = new File(file.getParent() + File.separator + filename.replace(".ppt", ".pdf"));
}
if (!pdfFile.exists()) {
pdfFile.createNewFile();
}else{
return pdfFile.getName();
}
this.convent(file, pdfFile);
//返回生成的html文件名
return pdfFile.getName();
}
/**
* 转换文件
* @param sourceFile 源文件
* @param distinctFile 目标文件
*/
public void convent(File sourceFile, File distinctFile){
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
System.err.println("无法完成文件转换,请检查OpenOffice服务是否启动!");
}
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(sourceFile, distinctFile);
connection.disconnect();
}
}