posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          使用Java將Word轉(zhuǎn)為Html或txt

          Posted on 2007-09-07 15:29 semovy 閱讀(347) 評論(0)  編輯  收藏 所屬分類: JAVA應(yīng)用

          package org.client;

          // 使用Java將Word轉(zhuǎn)為Html或txt!
          import com.jacob.com.*;
          import com.jacob.activeX.*;
          import java.io.*;

          public class Client {

          // 取得指定目錄下面所有的doc文件名稱
          // ------------------------------------------------------------------------------
          // 方法原型: change(String paths, String savepaths)
          // 功能描述: 將指定目錄下面所有的doc文件轉(zhuǎn)化為HTML并存儲(chǔ)在相同目錄下
          // 輸入?yún)?shù): String
          // 輸出參數(shù): 無
          // 返 回 值: 無
          // 其它說明: 遞歸
          // ------------------------------------------------------------------------------
          public static void change(String paths, String savepaths) {

             File d = new File(paths);
             // 取得當(dāng)前文件夾下所有文件和目錄的列表
             File lists[] = d.listFiles();
             String path = new String("");

             // 對當(dāng)前目錄下面所有文件進(jìn)行檢索
             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("當(dāng)前正在轉(zhuǎn)換......");
                // 打印當(dāng)前目錄路徑
                System.out.println(paths);
                // 打印doc文件名
                System.out.println(filename.substring(0,
                  (filename.length() - 4)));

                ActiveXComponent app = new ActiveXComponent(
                  "Word.Application");// 啟動(dòng)word

                String docpath = paths + filename;
                String htmlpath = savepaths
                  + filename.substring(0, (filename.length() - 4));

                String inFile = docpath;
                // 要轉(zhuǎn)換的word文件
                String tpFile = htmlpath;
                // HTML文件

                try {
                 app.setProperty("Visible", new Variant(false));
                 // 設(shè)置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格式保存到臨時(shí)文件
                 Variant f = new Variant(false);
                 Dispatch.call(doc, "Close", f);
                } catch (Exception e) {
                 e.printStackTrace();
                } finally {
                 app.invoke("Quit", new Variant[] {});
                }
                System.out.println("轉(zhuǎn)化完畢!");
               }
              } else {
               path = paths;
               // 進(jìn)入下一級目錄
               path = path + lists[i].getName() + "\\";
               // 遞歸遍歷所有目錄
               change(path, savepaths);
              }
             }
          }

          // ------------------------------------------------------------------------------
          // 方法原型: main(String[] args)
          // 功能描述: main文件
          // 輸入?yún)?shù): 無
          // 輸出參數(shù): 無
          // 返 回 值: 無
          // 其它說明: 無
          // ------------------------------------------------------------------------------
          public static void main(String[] args) {
             String paths = new String("D:\\作業(yè)\\day54\\");
             String savepaths = new String("D:\\作業(yè)\\day54\\");
             change(paths, savepaths);
          }
          /*
             * 其中import的jar包是一個(gè)開源的東東,網(wǎng)上搜索即得。 Dispatch.invoke(doc, "SaveAs",
             * Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
             * 修改Variant(8)},里面得參數(shù)即可將Word轉(zhuǎn)化為各種類型。
             */

          }

          主站蜘蛛池模板: 宜兰县| 长治市| 绥芬河市| 建阳市| 普宁市| 东台市| 开封市| 慈利县| 池州市| 玉田县| 沧源| 商南县| 海阳市| 藁城市| 蕉岭县| 交口县| 兰考县| 孟村| 寿宁县| 玛曲县| 绥棱县| 兰州市| 梅河口市| 新邵县| 榆林市| 铜鼓县| 龙川县| 三江| 肃北| 遵化市| 彰武县| 铜鼓县| 九江市| 磐安县| 满洲里市| 嵊泗县| 庄浪县| 璧山县| 佳木斯市| 长海县| 青海省|