1. 程式人生 > 其它 >JAVA 實現Excel 轉換PDF(開源) 且效果較好的版本

JAVA 實現Excel 轉換PDF(開源) 且效果較好的版本

JAVA 實現Excel 轉換PDF(開源) 且效果較好的版本 
首先 下載libreoffice 並配置環境變數 
地址-> https://zh-cn.libreoffice.org/
JAVA程式碼如下

檢視程式碼

package cn.com.toyota.sales.dist.scd.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;


/******************************************************** <BR>
 * Class名  Excle2PDF                                     <BR>
 * 機能概要: Excel 另存為 PDF                              <BR>
 * 呼叫libreoffice另存  需要安裝libreoffice                 <BR>
 ********************************************************/
public class Excle2PDF {

    private static final Logger logger = LoggerFactory.getLogger(Excle2PDF.class);

    /**
     * 另存為PDF
     * @param filePath  檔案全路徑
     * @param outPath   輸出路徑
     * @return
     */
    public static void save2PDF (String filePath , String outPath)  {
        long start = System.currentTimeMillis();
        logger.info("start:{} ms", start);
        String command = "";
        String osName = System.getProperty("os.name");
        if (osName.contains("Windows")) {
            command = "soffice --headless --convert-to pdf "+ "d:" + filePath + " --outdir " + "d:" + outPath;
            exec(command,filePath);
        }else {
            command = "libreoffice7.2 --headless --invisible  --convert-to pdf " +filePath + " --outdir "  +outPath;
            exec(command,filePath);
//            filePath.replace("excel", "pdf");
//            filePath.replace(".xlsx", ".pdf");
//            File file = new File( filePath);
//            while (!file.exists()) {
//                System.out.println("檔案出錯 正在重新生成 = " + filePath);
//                exec(command, filePath);
//            }
        }
        long end = System.currentTimeMillis();
        logger.info("end:{} ms", end);
        logger.info("用時:{} ms", end - start);
    }



    public static boolean exec(String command, String filePath) {
        Process process;// Process可以控制該子程序的執行或獲取該子程序的資訊
        try {
            process = Runtime.getRuntime().exec(command);// exec()方法指示Java虛擬機器建立一個子程序執行指定的可執行程式,並返回與該子程序對應的Process物件例項。
            // 下面兩個可以獲取輸入輸出流
            InputStream errorStream = process.getErrorStream();
            InputStream inputStream = process.getInputStream();
        } catch (IOException e) {
            logger.error(" exec {} error", command, e);
            return false;
        }

        int exitStatus = -1;
        try {
            exitStatus = process.waitFor();// 等待子程序完成再往下執行,返回值是子執行緒執行完畢的返回值,返回0表示正常結束
        } catch (InterruptedException e) {
            logger.error("InterruptedException  exec {}", command, e);
            return false;
        }

        if (exitStatus != 0) {
            logger.error("exec cmd exitStatus  error{} " +command, exitStatus);
            logger.error("生成錯誤的pdf " +filePath);
            process.destroy(); // 銷燬子程序
            process = null;
        } else {
            logger.info("exec cmd exitStatus sucess{} "+command, exitStatus);
        }

        process.destroy(); // 銷燬子程序
        process = null;

        return true;
    }
}