java 调用word

package com.test;

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

public class WordBean extends java.awt.Panel {
     private ActiveXComponent MsWordApp = null;

     private Dispatch document = null; 

     public WordBean() {
           super();
     }

 // 打开word文档
     public void openWord(boolean makeVisible) {

           if (MsWordApp == null) {
                 MsWordApp = new ActiveXComponent("Word.Application");
           }
  // 设置打开word文档是否可见
      Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible));
      }

 // 打开wordDocument
      public void openWordDocument(String openFile) {
             Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
             document = Dispatch.invoke(documents, "Open",Dispatch.Method,
             new Object[] { openFile, new Variant(false),
             new Variant(true) }, new int[1]).toDispatch();
       }

 // 创建word文档
        public void createNewDocument() {
  // Find the Documents collection object maintained by Word
               Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();

              document = Dispatch.call(documents, "Add").toDispatch();
        }

 // 插入内容
         public void insertText(String textToInsert) {
  // Get the current selection within Word at the moment. If
  // a new document has just been created then this will be at
  // the top of the new doc
              Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();

              Dispatch.put(selection, "Text", textToInsert);
          }

 // 文件另存为
           public void saveFileAs(String saveFile) {
                  Dispatch.invoke(document, "SaveAs", Dispatch.Method, new Object[] {
                     saveFile, new Variant(0) }, new int[1]);
                  Variant variant = new Variant(false);
                  Dispatch.call(document, "Close", variant);
  
              //   Dispatch.call(document, "SaveAs", saveFile);
           }

           public void printFile() {
          // Just print the current document to the default printer
                 Dispatch.call(document, "PrintOut");
            }

 // 关闭word文档
           public void closeDocument() {
                // 0 = wdDoNotSaveChanges
                // -1 = wdSaveChanges
                // -2 = wdPromptToSaveChanges
               Dispatch.call(document, "Close", new Variant(0));
               document = null;
           }

 // 关闭officeWord
           public void closeWord() {
                   Dispatch.call(MsWordApp, "Quit");
                   MsWordApp = null;
                  document = null;
           }

 // 替换文本
            public void replace(String oldText, String newText) {
                   Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();

                   Dispatch find = MsWordApp.call(selection, "Find").toDispatch();

                   Dispatch.put(find, "Text", oldText);
                   Dispatch.call(find, "Execute");
                   Dispatch.put(selection, "Text", newText);
  
                   Dispatch.call(selection, "MoveRight");
  
  
           }

}

package com.test;

public class WordTest {

        public static void main(String[] args) {
              WordBean word = new WordBean();
              word.openWord(false);
              //  word.createNewDocument();
              //  word.insertText("1234567890");
              //  word.saveFileAs("d://1.doc");
  
             word.openWordDocument("d://1.doc");
             word.replace("2", "xx"); //替换
             word.replace("3", "yy");
             word.saveFileAs("d://2.doc");
               //  word.closeDocument();
             word.closeWord();

         }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值