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();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章