用jacob實現word到html的轉換

來源:互聯網
上載者:User

          軟體的互通性是一個我們經常面臨的問題,如果 Java 可以自由的調用其他語言和平台的成熟代碼,可以充分利用您的 Java 技能,大大提高您的生產力。現有的 Java COM Interop技術有很多種實現,JACOB 開源項目提供了一個簡單方便的通用調用架構。

          JACOB 開源項目提供的是一個 JVM 獨立的Automation 伺服程式實現,其核心是基於 JNI 技術實現的 Variant, Dispatch 等介面,設計參考了 Microsoft VJ++ 內建的通用Automation 伺服程式,但是 Microsoft 的實現僅僅支援自身的 JVM。通過 JACOB,可以方便地在 Java 語言中進行晚期綁定方式的調用。

 

下面用jacob實現word到html的轉化。

1·配置開發和運行環境。

首先,使用 JACOB 來“搭橋”,需要下載最新的 JACOB 相關類庫,包括 Jacob.jar 和 Jacob.dll。

(1)將 jar 檔案添加到您的 Eclipse 工程的構建路徑。

 (2)需要把 DLL 檔案添加到系統的 PATH 環境變數中,或者也可以在程式中設定運行時的 PATH 變數。 一個簡單的“橋”已經搭起來了,現在就可以輕鬆的調用您熟悉的 COM 類別庫了。需要在作業系統中註冊 COM 組件,可以使用 Redemption 工具包本身提供的安裝程式,也可以採用 Windows 系統內建的 REGSVR32 工具來註冊 DLL 檔案。
 java代碼如下: 

package test;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordToHtml {

 public boolean changeFormat (String FileName){
       
        String  FileFormat = FileName.substring(FileName.length()-4,FileName.length());
        System.out.println("檔案尾碼"+FileFormat);
       
        if(FileFormat.equalsIgnoreCase(".doc")){
            String DocFile = FileName;
           
            System.out.println("word檔案路徑:"+DocFile);
            //word檔案的完整路徑
           
            String HtmlFile = DocFile.substring(0, (DocFile.length() - 4)) + ".htm";
           
            System.out.println("htm檔案路徑:"+HtmlFile);
            //html檔案的完整路徑
           
            ActiveXComponent app = new ActiveXComponent("Word.Application");
            //啟動word    
            try
            {
                app.setProperty("Visible", new Variant(false));
                //設定word程式非可視化運行
               
                Dispatch docs = (Dispatch) app.getProperty("Documents").toDispatch();
               
                Dispatch doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{DocFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();

                //開啟word檔案
               
                Dispatch.invoke(doc,"SaveAs",Dispatch.Method, new Object[]{HtmlFile,new Variant(8)}, new int[1]);
                //作為htm格式儲存檔案
               
                Dispatch.call(doc, "Close",new Variant(false));
                //關閉檔案
               
            }
            catch (Exception e){
                e.printStackTrace();
            }
            finally {
                app.invoke("Quit", new Variant[] {});
                //退出word程式
            }
            //轉化完畢
            return true;
        }
        else{
         return false;
        }
       
    }
    public static void main(String[] args ) {
        WordToHtml d = new WordToHtml();
        File f=new File("c:\\a.doc");
        System.out.println(f.getAbsolutePath());
        System.out.println(d.changeFormat( f.getAbsolutePath()));
    }

 這樣就完成了從word到html的轉換。     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.