java xls转xlsx/pdf

下载依赖的jar包,将包添加到工程目录下的resources/lib/中

链接: https://pan.baidu.com/s/15kwoT2eeTGVjNcvZRdccpw?pwd=rcg1 
提取码: rcg1 

在pom.xml添加依赖

        <!--添加本地的jacob.jar包-->
        <dependency>
            <groupId>com.jacob</groupId>
            <artifactId>jacob</artifactId>
            <version>1.19</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/lib/jacob.jar</systemPath>
        </dependency>
转pdf
     /**
     * 根据格式类型转换xls文件
     *
     * @param srcPath  xls path 源文件
     * @param descPath pdf path 目标文件
     * @return the file
     * @throws Exception the exception
     */
    public File convertXlsPDF(String srcPath, String descPath) throws Exception {
        // 实例化ComThread线程与ActiveXComponent
        ComThread.InitSTA();
        ActiveXComponent app = new ActiveXComponent("Excel.Application");
        try {
            // 隐藏时进行应用操作
            app.setProperty("Visible", new Variant(false));
            app.setProperty("DisplayAlerts", new Variant(false));
            // 实例化模板Document对象
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            // 打开Document进行另存为操作
            Dispatch excel = Dispatch.call(excels, "Open", srcPath, false, true).toDispatch();

            Dispatch.call(excel, "ExportAsFixedFormat", XLTYPE_PDF, descPath);
            Dispatch.call(excel, "Close", false);
            return new File(descPath);
        } catch (Exception e) {
            throw e;
        } finally {
            // 释放线程与ActiveXComponent
            app.invoke("Quit", new Variant[]{});
            ComThread.Release();
        }
    }
转xlsx
    /**
     * 根据格式类型转换xls文件
     *
     * @param srcPath  xls path 源文件
     * @param descPath xlsx path 目标文件
     * @return the file
     * @throws Exception the exception
     */
    public File convertXlsfmt(String srcPath, String descPath) throws Exception {


        // 实例化ComThread线程与ActiveXComponent
        ComThread.InitSTA();
        ActiveXComponent app = new ActiveXComponent("Excel.Application");
        try {
            // 隐藏时进行应用操作
            app.setProperty("Visible", new Variant(false));
            app.setProperty("DisplayAlerts", new Variant(false));
            // 实例化模板Document对象
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            // 打开Document进行另存为操作
            Dispatch excel = Dispatch.call(excels, "Open", srcPath, false, true).toDispatch();

            Dispatch.invoke(excel,
                    "SaveAs",
                    Dispatch.Method,
                    new Object[]{descPath, new Variant(EXCEL_XML_2_XLS)
                    },
                    new int[1]);

            Variant f = new Variant(false);
            Dispatch.call(excel, "Close", f);

            return new File(descPath);
        } catch (Exception e) {
            throw e;
        } finally {
            // 释放线程与ActiveXComponent
            app.invoke("Quit", new Variant[]{});
            ComThread.Release();
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容