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

          真正的快樂來源于創造

            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 何楊 閱讀(970) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 修水县| 汨罗市| 革吉县| 乌拉特中旗| 家居| 进贤县| 博湖县| 克拉玛依市| 民县| 陇西县| 峨眉山市| 桂平市| 博湖县| 西乌珠穆沁旗| 建平县| 突泉县| 进贤县| 阿拉善右旗| 信阳市| 寿光市| 玛多县| 久治县| 青铜峡市| 万宁市| 河池市| 山阴县| 虞城县| 固原市| 颍上县| 介休市| 东安县| 扎兰屯市| 通渭县| 木兰县| 白银市| 永安市| 皋兰县| 伊春市| 吉水县| 固原市| 新绛县|