Excel轉Pdf —— jacob

這需求,我是真的吐了,找了太多東西了,poi直接想都不想,直接放棄poi
documents4j導出pdf很簡單,但是沒找到其他pdf相關設置的參數,算了
最後還是jacob能解決所有問題 —— 縮放,橫向,大小
該方式僅限windows下,底層就是通過jni調用office的com組件

1、配置

		<!-- excel to pdf -->
        <!-- https://mvnrepository.com/artifact/com.hynnet/jacob -->
     	<dependency>
            <groupId>com.hynnet</groupId>
            <artifactId>jacob</artifactId>
            <version>1.18</version>
        </dependency>

對應的dll文件https://sourceforge.net/projects/jacob-project/files/jacob-project/,下載的文件中有對應dll,

將jacob-1.14.3-x64.dll文件拷貝如以下目錄

C:\Windows\System32
${java_home}/jdk/bin
${java_home}/jre/bin

  • 這裏有個坑,如果你是web應用,需要在META-INF目錄下放入JacobVersion.properties文件,這個文件在jar包中${mvn_repository}/com/hynnet/jacob/1.18/jacob-1.18.jar!/META-INF/JacobVersion.properties
  • 再則,導入包時,先看看項目有沒有以前版本的jacob,我這個就是因爲項目本身有一個了,但是是1.14.3版本的,真不知道是不是遠古人,還在用這麼老的版本,所以找了很久的問題

2、使用

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.io.File;

	/**
     * excel轉pdf
     * @param inFilePath 讀取excel路徑
     * @param outFilePath 輸出pdf路徑
     * @param pageSize 頁面大小
     *        A3 297mm x 420mm : 8
     *        A4 210mm x 297mm : 10
     *        A5 148mm x 210mm : 11
     * @param zoom excel內容縮放百分比
     * @param orientation 是否橫向輸出
     */
    public static void excel2Pdf(String inFilePath, String outFilePath, Integer pageSize, Integer zoom, Boolean orientation) {
        ActiveXComponent ax = null;
        Dispatch excel = null;
        try {
            ComThread.InitSTA();
            ax = new ActiveXComponent("Excel.Application");
            ax.setProperty("Visible", new Variant(false));
            // 禁用宏
            ax.setProperty("AutomationSecurity", new Variant(3));
            Dispatch excels = ax.getProperty("Workbooks").toDispatch();
            Object[] obj = new Object[]{
                    inFilePath,
                    new Variant(false),
                    new Variant(false)
            };
            excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();
            // 獲取第一個sheet
            Dispatch currentSheet = Dispatch.get(excel,
                    "ActiveSheet").toDispatch();
            // 獲取頁面設置
            Dispatch pageSetup = Dispatch.get(currentSheet, "PageSetup")
                    .toDispatch();
            // 橫向輸出
            if (orientation) {
                Dispatch.put(pageSetup, "Orientation", new Variant(2));
            }
            // 頁面大小
            Dispatch.put(pageSetup, "PaperSize", pageSize);
            // 內容縮放
            Dispatch.put(pageSetup, "Zoom", zoom);

            File tofile = new File(outFilePath);
            if (tofile.exists()) {
                tofile.delete();
            }
            // 轉換格式
            Object[] obj2 = new Object[]{
                    // PDF格式=0
                    new Variant(0),
                    outFilePath,
                    //0=標準 (生成的PDF圖片不會變模糊) ; 1=最小文件
                    new Variant(1)
            };
            Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]);
            LOG.info("excel -> pdf 導出完成");
        } catch (Exception es) {
            es.printStackTrace();
            LOG.error(es.getMessage());
            throw es;
        } finally {
            if (excel != null) {
                Dispatch.call(excel, "Close", new Variant(false));
            }
            if (ax != null) {
                ax.invoke("Quit", new Variant[]{});
                ax = null;
            }
            ComThread.Release();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章