WEB开发网
开发学院WEB开发Jsp java通过jacob调用word 阅读

java通过jacob调用word

 2008-01-05 09:07:27 来源:WEB开发网   
核心提示: public static void main(String[] args) { //启动Word,生成一个ActivexComponent对象 ActiveXComponent app = new ActiveXComponent("word.application"); //要转换的w

  public static void main(String[] args) {   //启动Word,生成一个ActivexComponent对象   ActiveXComponent app = new ActiveXComponent("word.application");   //要转换的word文件   String inFile = "D:\\PRoject\\java_word\\java_word\\wordtemplet.doc";   //要报存的目标文件   String tpFile ="D:\\project\\java_word\\java_word\\wordtemplet3.doc";   boolean flag = false;   Dispatch xlo = app.getObject();   String oldText="1234";   String newText="test";   boolean visible=false;   String bookMarkKey="LB_KJGG";   try {    //设置word不可见    app.setProperty("Visible", new Variant(visible));    //log.info("设置word不可见成功!");    System.out.println("设置word不可见成功!");    Dispatch docs = app.getProperty("Documents").toDispatch();    Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method                  ,new Object[]                  {inFile, new Variant(false), new Variant(true)}                  , new int[1]).toDispatch(); //打开word文件    //在word2003的vba文档中application有UserName属性。    String userName=app.getPropertyAsString("UserName");    System.out.println("用户名:"+userName);    Dispatch selection=app.getProperty("Selection").toDispatch();    //得到一个组件    System.out.println("Selection");    Dispatch find = app.call(selection, "Find").toDispatch();

    //查找什么文本    Dispatch.put(find, "Text", oldText);    //替换文本    Dispatch.call(find,"ClearFormatting");    Dispatch.put(find, "Text", oldText);    Dispatch.call(find, "Execute");    Dispatch.put(selection, "Text", newText);    // Dispatch.call(app, "SaveAs", inFile);    System.out.println("replace");        //把指定的值设置到指定的标签中去    Dispatch activeDocument=app.getProperty("ActiveDocument").toDispatch();    System.out.println("activedocument");    Dispatch bookMarks = app.call(activeDocument, "Bookmarks").toDispatch();    System.out.println("bookmarks");    boolean bookMarkExist1=Dispatch.call(bookMarks,"Exists",bookMarkKey).toBoolean();    if(bookMarkExist1==true){     System.out.println("exists bookmark!");     Dispatch rangeItem = Dispatch.call(bookMarks, "Item",bookMarkKey).             toDispatch();           System.out.println("range item!");           Dispatch range = Dispatch.call(rangeItem, "Range").toDispatch();           System.out.println("range !");           //取标签的值           String bookMarkValue=Dispatch.get(range,"Text").toString();           bookMarkValue="test";           if(bookMarkValue!=null){            Dispatch.put(range, "Text",               new Variant(bookMarkValue));           }         }    else{     System.out.println("not exists bookmark!");    }    //保存文件    Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {tpFile, new Variant(0)}            , new int[1]);    //作为word格式保存到目标文件    Variant f = new Variant(false);    Dispatch.call(doc, "Close", f);    flag = true;   }   catch (Exception e) {    e.printStackTrace();   }   finally {    app.invoke("Quit", new Variant[] {});   }

Tags:java 通过 jacob

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接