當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          package com.heyang;

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

          public class ModifyWordDocument {
              
          public static void main(String[] args) throws Exception {
                  
          // 初始化com的線程,非常重要!!使用結束后要調用 realease方法
                  ComThread.InitSTA();

                  
          // 實例化ActiveX組件對象:對word進行操作
                  ActiveXComponent wrdCom = new ActiveXComponent("Word.Application");

                  
          // 獲取Dispatch的Documents對象
                  Dispatch wrdDocs = wrdCom.getProperty("Documents").toDispatch();

                  
          // 設置打開的word應用程序是否可見
                  wrdCom.setProperty("Visible"new Variant(true));

                  
          // 打開一個已經存在的文檔
                  Dispatch doc = Dispatch.call(wrdDocs, "Open""c:\\abc.doc")
                          .toDispatch();

                  
          // 獲得當前word文檔文本
                  Dispatch docSelection = Dispatch.get(wrdCom, "Selection").toDispatch();

                  
          // 從selection所在位置開始查詢
                  Dispatch find = Dispatch.call(docSelection, "Find").toDispatch();

                  
          // 設置要查找的內容
                  Dispatch.put(find, "Text""測試");
                  
          // 向前查找
                  Dispatch.put(find, "Forward""True");
                  
          // 設置格式
                  Dispatch.put(find, "Format""True");
                  
          // 大小寫匹配
                  Dispatch.put(find, "MatchCase""True");
                  
          // 全字匹配
                  Dispatch.put(find, "MatchWholeWord""True");

                  Dispatch.call(find, 
          "Execute").getBoolean();
                  Dispatch.put(docSelection, 
          "Text""岳飛");

                  
          // 另存為
                  Dispatch.call(doc, "SaveAs"new Variant("C:\\abc.doc")); // 保存一個新文檔

                  
          // 保存關閉
                  if (doc != null) {
                      Dispatch.call(doc, 
          "Save");
                      Dispatch.call(doc, 
          "Close"new Variant(true));
                      doc 
          = null;
                  }

                  
          // 關閉word文件
                  wrdCom.invoke("Quit"new Variant[] {});
                  
          // 釋放com線程。根據jacob的幫助文檔,com的線程回收不由java的垃圾回收器處理
                  ComThread.Release();
              }
          }
          posted on 2011-06-03 11:42 何楊 閱讀(968) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 黄浦区| 大埔县| 湟源县| 宝坻区| 东乡族自治县| 墨脱县| 海原县| 新郑市| 和田县| 甘孜| 缙云县| 湘潭市| 神农架林区| 汽车| 林西县| 长葛市| 阳谷县| 南靖县| 涞源县| 敦煌市| 巴塘县| 海阳市| 鸡泽县| 治多县| 仙桃市| 广饶县| 陈巴尔虎旗| 霍州市| 平昌县| 河北区| 应城市| 蓬溪县| 芜湖县| 雅安市| 石楼县| 西乌珠穆沁旗| 乌苏市| 丰原市| 石景山区| 台安县| 沙田区|