使用openffice 实现word、excel、ppt转pdf或者html文件

  1. 运行环境:win10、eclipse mars、java8 所需相关jar包下载地址
  2. openoffice下载地址 转换之前请先安装openoffice,然后在软件安装目录的progrom目录下:使用命令启动相关服务: soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard
  3. 相关代码
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();
    }
}

转载于:https://my.oschina.net/u/262693/blog/728777

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值