·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » java通过jacob调用word

java通过jacob调用word

类别: JAVA教程  评论数:0 总得分:0

  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[] {});    }



-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1