java 调用word

java 调用word
文章出处:http://www.diybl.com/course/3_program/java/javajs/200843/108206.html

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();

         }

}

posted on 2008-12-08 15:17  小星星☆★  阅读(923)  评论(1编辑  收藏  举报

导航