使用Java將Word轉為Html或txt等···

來源:互聯網
上載者:User
前一段時間為這個問題頭疼的不得了,後來查閱了資料終於解決了;現將程式放出,以便以後參考。

 

//------------------------------------------------------------------------------
//著作權 (C) 浪潮集團商用系統有限公司  保留所有權利
//檔案名稱: wordtohtml           檔案版本: 1.00.00
//作    者: 郭鑄     作者郵箱: guozhu@langchao.com  完成日期: 2004-10-20
//檔案描述:
//其它描述:
//類 列 表:
//  wordtohtml: 將指定目錄下面所有的doc檔案轉化為HTML並儲存在相同目錄下
//修改曆史:
//  #   版本     修改日期    作者                 修改內容
//  ----------------------------------------------------------------------------
//  1   1.00.01  2004-10-14  作者姓名             修改內容描述
//  ----------------------------------------------------------------------------
//------------------------------------------------------------------------------
import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;

//取得指定目錄下面所有的doc檔案名稱
public class wordtohtml
{
//------------------------------------------------------------------------------
//方法原型: change(String paths)
//功能描述: 將指定目錄下面所有的doc檔案轉化為HTML並儲存在相同目錄下
//輸入參數: String
//輸出參數: 無
//返 回 值: 無
//其它說明: 遞迴
//------------------------------------------------------------------------------ 
 public static void change(String paths, String savepaths)
 { 
   
  File d = new File(paths);
  //取得當前檔案夾下所有檔案和目錄的列表
  File lists[] = d.listFiles();
  String pathss = new String("");

  //對目前的目錄下面所有檔案進行檢索
  for(int i = 0; i < lists.length; i ++)
  {
   if(lists[i].isFile())
   {
    String filename = lists[i].getName();
    String filetype = new String("");
    //取得檔案類型
    filetype = filename.substring((filename.length() - 3), filename.length());
    
    //判斷是否為doc檔案
    if(filetype.equals("doc"))
    {
     System.out.println("當前正在轉換......");
     //列印目前的目錄路徑
     System.out.println(paths);
     //列印doc檔案名稱
     System.out.println(filename.substring(0, (filename.length() - 4)));
     
     ActiveXComponent app = new ActiveXComponent("Word.Application");//啟動word
     
     String docpath = paths + filename;
     String htmlpath = savepaths + filename.substring(0, (filename.length() - 4));
     
     String inFile = docpath;
    //要轉換的word檔案
     String tpFile = htmlpath;
    //HTML檔案

     boolean flag = false;
     
     try
     {
      app.setProperty("Visible", new Variant(false));
        //設定word不可見
      Object docs = app.getProperty("Documents").toDispatch();
      Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();
        //開啟word檔案
      Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
        //作為html格式儲存到臨時檔案
      Variant f = new Variant(false);
      Dispatch.call(doc, "Close", f);
      flag = true;
     }
     catch (Exception e)
     {
      e.printStackTrace();
     }
     finally
     {
      app.invoke("Quit", new Variant[] {});
     }
     System.out.println("轉化完畢!");
    }
   }
   else
   {
    pathss = paths;
    //進入下一級目錄
    pathss = pathss + lists[i].getName() + "";    
    //遞迴遍曆所有目錄
    change(pathss, savepaths);
   }
  }
  
 } 
//------------------------------------------------------------------------------
//方法原型: main(String[] args)
//功能描述: main檔案
//輸入參數: 無
//輸出參數: 無
//返 回 值: 無
//其它說明: 無
//------------------------------------------------------------------------------  
 public static void main(String[] args)
 {
  
  String paths = new String("D:Work2004.10.8 est system est01word");
  String savepaths = new String ("D:Work2004.10.8 est system est01html");

  change(paths, savepaths);

 }
}

其中import的jar包是一個開源的東東,網上搜尋即得。
Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
修改Variant(8)},裡面得參數即可將Word轉化為各種類型。

建立於: 2004-12-31 11:12:00,修改於: 2004-12-31 11:12:00,已瀏覽587次,有評論0條

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.