1. 程式人生 > >java實現office檔案線上預覽

java實現office檔案線上預覽

需要下載的軟體:

基本思路:

通過jodconverte在java程式碼中呼叫OpenOffice把Office檔案轉換為html檔案來實現線上預覽。

demo程式碼:

package com.pds.framework.utils;

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;

import java.io.*;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 利用jodconverter(基於OpenOffice服務)將檔案(*.doc、*.docx、*.xls、*.ppt)轉化為html格式或者pdf格式,
 * 使用前請檢查OpenOffice服務是否已經開啟, OpenOffice程序名稱:soffice.exe | soffice.bin
 * Created by Wewon on 2017/12/29.
 */
public class Doc2HtmlUtil {
    private static Doc2HtmlUtil doc2HtmlUtil;

    /**
     * 獲取Doc2HtmlUtil例項
     */
    public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
        if (doc2HtmlUtil == null) {
            doc2HtmlUtil = new Doc2HtmlUtil();
        }
        return doc2HtmlUtil;
    }
    /**
     * 轉換檔案成html
     *
     * @param fromFileInputStream:
     * @throws IOException
     */
    public String file2Html(InputStream fromFileInputStream, String toFilePath, String type) throws IOException {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timesuffix = sdf.format(date);
        String docFileName = null;
        String htmFileName = null;
        if("doc".equals(type)){
            docFileName = "doc_" + timesuffix + ".doc";
            htmFileName = "doc_" + timesuffix + ".html";
        }else if("docx".equals(type)){
            docFileName = "docx_" + timesuffix + ".docx";
            htmFileName = "docx_" + timesuffix + ".html";
        }else if("xls".equals(type)){
            docFileName = "xls_" + timesuffix + ".xls";
            htmFileName = "xls_" + timesuffix + ".html";
        }else if("ppt".equals(type)){
            docFileName = "ppt_" + timesuffix + ".ppt";
            htmFileName = "ppt_" + timesuffix + ".html";
        }else{
            return null;
        }

        File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
        File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
        if (htmlOutputFile.exists())
            htmlOutputFile.delete();
        htmlOutputFile.createNewFile();
        if (docInputFile.exists())
            docInputFile.delete();
        docInputFile.createNewFile();
        /**
         * 由fromFileInputStream構建輸入檔案
         */
        try {
            OutputStream os = new FileOutputStream(docInputFile);
            int bytesRead = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }

            os.close();
            fromFileInputStream.close();
        } catch (IOException e) {
        }

        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        try {
            connection.connect();
        } catch (ConnectException e) {
            System.err.println("檔案轉換出錯,請檢查OpenOffice服務是否啟動。");
        }
        // convert
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(docInputFile, htmlOutputFile);
        connection.disconnect();
        // 轉換完之後刪除word檔案
        docInputFile.delete();
        return htmFileName;
    }


    public static void main(String[] args) throws IOException {
        Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance();
        File file = null;
        FileInputStream fileInputStream = null;

        file = new File("C:/poi-test/exportExcel.xls");
        fileInputStream = new FileInputStream(file);
        coc2HtmlUtil.file2Html(fileInputStream, "C:/poi-test/openOffice", "xls");
    }

    }


遇到的困難:

1、報錯java.lang.NoClassDefFoundError:com.sun.star.lang.XEventListener

這是由於使用jodconverter-2.2.2時,包引用不全的話會出現這個問題,解決辦法就是到官方網站下載jodconverter-2.2.2.zip,然後把lib資料夾下除了 commons-cli、jodconverter-cli(這兩個包跟命令列有關)外所有jar包都拷進去,就可以了。

2、在中文環境下生成的html檔案是GB2312編碼,如果你的專案是UTF8編碼,需要把html檔案轉為UTF8編碼,否則亂碼

編碼轉換實用類(轉換之後是直接覆蓋原檔案不是追加):

package com.pds.framework.utils;

import java.io.*;

/**
 * Created by Wewon on 2018/1/2.
 */
public class ReadWriteFileWithEncode {
    public static void write(String path, String content, String encoding)
            throws IOException {
        File file = new File(path);
        file.delete();
        file.createNewFile();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file), encoding));
        writer.write(content);
        writer.close();
    }

    public static String read(String path, String encoding) throws IOException {
        String content = "";
        File file = new File(path);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), encoding));
        String line = null;
        while ((line = reader.readLine()) != null) {
            content += line + "\n";
        }
        reader.close();
        return content;
    }

    public static void main(String[] args) throws IOException {
        String path = "C:/pds/pds-scm/web/openOffice/xls_20171229170859.html";
        String encoding = "utf-8";
        ReadWriteFileWithEncode.write(path, ReadWriteFileWithEncode.read(path, "GB2312"), encoding);
    }
}


參考資料: