现在的位置: 首页 > 综合 > 正文

Java 调用word

2013年10月04日 ⁄ 综合 ⁄ 共 3653字 ⁄ 字号 评论关闭
  1. //java 调用word 
  2. package com.test;
  3. import com.jacob.activeX.ActiveXComponent;
  4. import com.jacob.com.Dispatch;
  5. import com.jacob.com.Variant;
  6. public class WordBean extends java.awt.Panel {
  7.      private ActiveXComponent MsWordApp = null;
  8.      private Dispatch document = null;  
  9.      public WordBean() {
  10.            super();
  11.      }
  12.  // 打开word文档
  13.      public void openWord(boolean makeVisible) {
  14.            if (MsWordApp == null) {
  15.                  MsWordApp = new ActiveXComponent("Word.Application");
  16.            }
  17.   // 设置打开word文档是否可见
  18.       Dispatch.put(MsWordApp, "Visible"new Variant(makeVisible));
  19.       }
  20.  // 打开wordDocument
  21.       public void openWordDocument(String openFile) {
  22.              Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
  23.              document = Dispatch.invoke(documents, "Open",Dispatch.Method,
  24.              new Object[] { openFile, new Variant(false),
  25.              new Variant(true) }, new int[1]).toDispatch();
  26.        }
  27.  // 创建word文档
  28.         public void createNewDocument() {
  29.   // Find the Documents collection object maintained by Word
  30.                Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
  31.               document = Dispatch.call(documents, "Add").toDispatch();
  32.         }
  33.  // 插入内容
  34.          public void insertText(String textToInsert) {
  35.   // Get the current selection within Word at the moment. If
  36.   // a new document has just been created then this will be at
  37.   // the top of the new doc
  38.               Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
  39.               Dispatch.put(selection, "Text", textToInsert);
  40.           }
  41.  // 文件另存为
  42.            public void saveFileAs(String saveFile) {
  43.                   Dispatch.invoke(document, "SaveAs", Dispatch.Method, new Object[] {
  44.                      saveFile, new Variant(0) }, new int[1]);
  45.                   Variant variant = new Variant(false);
  46.                   Dispatch.call(document, "Close", variant);
  47.   
  48.               //   Dispatch.call(document, "SaveAs", saveFile);
  49.            }
  50.            public void printFile() {
  51.           // Just print the current document to the default printer
  52.                  Dispatch.call(document, "PrintOut");
  53.             }
  54.  // 关闭word文档
  55.            public void closeDocument() {
  56.                 // 0 = wdDoNotSaveChanges
  57.                 // -1 = wdSaveChanges
  58.                 // -2 = wdPromptToSaveChanges
  59.                Dispatch.call(document, "Close"new Variant(0));
  60.                document = null;
  61.            } 
  62.  // 关闭officeWord
  63.            public void closeWord() {
  64.                    Dispatch.call(MsWordApp, "Quit");
  65.                    MsWordApp = null;
  66.                   document = null;
  67.            }
  68.  // 替换文本
  69.  public void replace(String oldText, String newText) {
  70.                    Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
  71.                    Dispatch find = MsWordApp.call(selection, "Find").toDispatch();
  72.                    Dispatch.put(find, "Text", oldText);
  73.                    Dispatch.call(find, "Execute");
  74.                    Dispatch.put(selection, "Text", newText);
  75.   
  76.                    Dispatch.call(selection, "MoveRight");
  77.   
  78.   
  79.            }
  80. }
  81. package com.test;
  82. public class WordTest {
  83.         public static void main(String[] args) {
  84.               WordBean word = new WordBean();
  85.               word.openWord(false);
  86.               //  word.createNewDocument();
  87.               //  word.insertText("1234567890");
  88.               //  word.saveFileAs("d:""1.doc");
  89.   
  90.              word.openWordDocument("d:""1.doc");
  91.              word.replace("2""xx"); //替换
  92.              word.replace("3""yy");
  93.              word.saveFileAs("d:""2.doc");
  94.                //  word.closeDocument();
  95.              word.closeWord();
  96.          } 
  97. }

抱歉!评论已关闭.