zoukankan      html  css  js  c++  java
  • 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();

             }

    }
  • 相关阅读:
    【HANA系列】SAP HANA跟我学HANA系列之创建计算视图一
    【HANA系列】SAP HANA计算视图中的RANK使用方法
    【ABAP系列】SAP ABAP7.40新语法简介第一篇
    【ABAP系列】SAP ABAP7.40新语法简介第二篇
    【MM系列】SAP 根据PO查找对应的打印FORM
    【ABAP系列】SAP BOM反查
    【FICO系列】SAP FICO折旧记账时出现错误:没有找到与所做选择一致的数据
    【PI系列】SAP IDOC发送状态03,PI没有收到消息的解决办法
    Struts2_属性驱动
    struts2_struts.xml配置文件讲解
  • 原文地址:https://www.cnblogs.com/xinxindiandeng/p/1350384.html
Copyright © 2011-2022 走看看