首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>java教程>资讯:Java调用com组件操作word使用总结(jacob)

Java调用com组件操作word使用总结(jacob)

www.jz123.cn  2010-01-05   来源:   中国建站    责任编辑(袁袁)    我要投递新闻


 /**
  * 把选定的内容或者插入点向右移动
  *
  * @param pos
  * 移动的距离
  */
  public void moveRight(int pos) {
  if (selection == null)
  selection = Dispatch.get(word, "Selection").toDispatch();
  for (int i = 0; i < pos; i++)
  Dispatch.call(selection, "MoveRight");
  }
  /**
  * 把插入点移动到文件首位置
  *
  */
  public void moveStart() {
  if (selection == null)
  selection = Dispatch.get(word, "Selection").toDispatch();
  Dispatch.call(selection, "HomeKey", new Variant(6));
  }
  /**
  * 从选定内容或插入点开始查找文本
  *
  * @param toFindText
  * 要查找的文本
  * @return boolean true-查找到并选中该文本,false-未查找到文本
  */
  @SuppressWarnings("static-access")
  public boolean find(String toFindText) {
  if (toFindText == null || toFindText.equals(""))
  return false;
  // 从selection所在位置开始查询
  Dispatch find = word.call(selection, "Find").toDispatch();
  // 设置要查找的内容
  Dispatch.put(find, "Text", toFindText);
  // 向前查找
  Dispatch.put(find, "Forward", "True");
  // 设置格式
  Dispatch.put(find, "Format", "True");
  // 大小写匹配
  Dispatch.put(find, "MatchCase", "True");
  // 全字匹配
  Dispatch.put(find, "MatchWholeWord", "True");
  // 查找并选中
  return Dispatch.call(find, "Execute").getBoolean();
  }
  /**
  * 把选定选定内容设定为替换文本
  *
  * @param toFindText
  * 查找字符串
  * @param newText
  * 要替换的内容
  * @return
  */
  public boolean replaceText(String toFindText, String newText) {
  if (!find(toFindText))
  return false;
  Dispatch.put(selection, "Text", newText);
  return true;
  }
  /**
  * 全局替换文本
  *
  * @param toFindText
  * 查找字符串
  * @param newText
  * 要替换的内容
  */
  public void replaceAllText(String toFindText, String newText) {
  while (find(toFindText)) {
  Dispatch.put(selection, "Text", newText);
  Dispatch.call(selection, "MoveRight");
  }
  }

上一篇:Java通过jacob对excel,word进行打印等操作 下一篇:Java转义字符在正则表达式中的应用

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图