當(dāng)柳上原的風(fēng)吹向天際的時(shí)候...

          真正的快樂(lè)來(lái)源于創(chuàng)造

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            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的線程,非常重要!!使用結(jié)束后要調(diào)用 realease方法
                  ComThread.InitSTA();

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

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

                  
          // 設(shè)置打開(kāi)的word應(yīng)用程序是否可見(jiàn)
                  wrdCom.setProperty("Visible"new Variant(true));

                  
          // 打開(kāi)一個(gè)已經(jīng)存在的文檔
                  Dispatch doc = Dispatch.call(wrdDocs, "Open""c:\\abc.doc")
                          .toDispatch();

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

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

                  
          // 設(shè)置要查找的內(nèi)容
                  Dispatch.put(find, "Text""測(cè)試");
                  
          // 向前查找
                  Dispatch.put(find, "Forward""True");
                  
          // 設(shè)置格式
                  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")); // 保存一個(gè)新文檔

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

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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 肃宁县| 泰和县| 凤台县| 浙江省| 普宁市| 陆川县| 新泰市| 梁山县| 宕昌县| 平凉市| 松江区| 巴塘县| 余江县| 井研县| 道真| 沽源县| 博乐市| 通城县| 丰台区| 虹口区| 新沂市| 博兴县| 常宁市| 栾川县| 韶山市| 沂源县| 张家界市| 凌云县| 开平市| 修武县| 宣城市| 广宗县| 泾阳县| 唐河县| 乌拉特前旗| 绥德县| 阳春市| 介休市| 砚山县| 合肥市| 西昌市|