java xls轉(zhuǎn)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>
轉(zhuǎn)pdf
     /**
     * 根據(jù)格式類型轉(zhuǎn)換xls文件
     *
     * @param srcPath  xls path 源文件
     * @param descPath pdf path 目標(biāo)文件
     * @return the file
     * @throws Exception the exception
     */
    public File convertXlsPDF(String srcPath, String descPath) throws Exception {
        // 實(shí)例化ComThread線程與ActiveXComponent
        ComThread.InitSTA();
        ActiveXComponent app = new ActiveXComponent("Excel.Application");
        try {
            // 隱藏時(shí)進(jìn)行應(yīng)用操作
            app.setProperty("Visible", new Variant(false));
            app.setProperty("DisplayAlerts", new Variant(false));
            // 實(shí)例化模板Document對象
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            // 打開Document進(jìn)行另存為操作
            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();
        }
    }
轉(zhuǎn)xlsx
    /**
     * 根據(jù)格式類型轉(zhuǎn)換xls文件
     *
     * @param srcPath  xls path 源文件
     * @param descPath xlsx path 目標(biāo)文件
     * @return the file
     * @throws Exception the exception
     */
    public File convertXlsfmt(String srcPath, String descPath) throws Exception {


        // 實(shí)例化ComThread線程與ActiveXComponent
        ComThread.InitSTA();
        ActiveXComponent app = new ActiveXComponent("Excel.Application");
        try {
            // 隱藏時(shí)進(jìn)行應(yīng)用操作
            app.setProperty("Visible", new Variant(false));
            app.setProperty("DisplayAlerts", new Variant(false));
            // 實(shí)例化模板Document對象
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            // 打開Document進(jìn)行另存為操作
            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();
        }
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容