Word格式轉化為Html

來源:互聯網
上載者:User

標籤:word格式轉化為html

package com.wiseweb.util;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class WordtoHtml {//------------------------------------------------------------------------------//方法原型: change(String paths)//功能描述: 將指定目錄下面所有的doc檔案轉化為HTML並儲存在相同目錄下//輸入參數: String//輸出參數: 無//返 回 值: 無//其它說明: 遞迴//------------------------------------------------------------------------------   public static boolean change (String FolderPath,String FileName){        String FileFormat = "";        System.out.println(FolderPath);        FileFormat = FileName.substring(FileName.length()-4,FileName.length());        System.out.println(FileFormat);        if(FileFormat.equalsIgnoreCase(".doc"))        {            String DocFile = FolderPath +"//"+ FileName;            System.out.println("word檔案路徑:"+DocFile);            //word檔案的完整路徑            String HtmlFile = DocFile.substring(0, (DocFile.length() - 4)) + ".html";            System.out.println("htm檔案路徑:"+HtmlFile);            //html檔案的完整路徑            ActiveXComponent app = new ActiveXComponent("Word.Application");            //啟動word            try            {                app.setProperty("Visible", new Variant(false));                //設定word程式非可視化運行                Dispatch docs = 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;        }        return false;    }//------------------------------------------------------------------------------//方法原型: main(String[] args)//功能描述: main檔案//輸入參數: 無//輸出參數: 無//返 回 值: 無//其它說明: 無//------------------------------------------------------------------------------   public static void main(String[] args) {  //  String paths = new String("E://wordToHtml");  String paths = new String("E:");//  String filename = "a.doc";  String filename = "servlet和jsp學習指南(Budi kurniawan).doc";  change(paths, filename); }}
其實原理就是利用word把文檔開啟,然後另存新檔html格式就ok了。

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.