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

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

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


 public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx,
  String txt) {
  // 所有表格
  Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  // 要填充的表格
  Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
  .toDispatch();
  Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
  new Variant(cellColIdx)).toDispatch();
  Dispatch.call(cell, "Select");
  Dispatch.put(selection, "Text", txt);
  }
  /**
  * 获得指定的单元格里数据
  *
  * @param tableIndex
  * @param cellRowIdx
  * @param cellColIdx
  * @return
  */
  public String getTxtFromCell(int tableIndex, int cellRowIdx, int cellColIdx) {
  // 所有表格
  Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  // 要填充的表格
  Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
  .toDispatch();
  Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
  new Variant(cellColIdx)).toDispatch();
  Dispatch.call(cell, "Select");
  String ret = "";
  ret = Dispatch.get(selection, "Text").toString();
  ret = ret.substring(0, ret.length()-1); //去掉最后的回车符;
  return ret;
  }
  /**
  * 在当前文档拷贝剪贴板数据
  * @param pos
  */
  public void pasteExcelSheet(String pos) {
  moveStart();
  if (this.find(pos)) {
  Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
  Dispatch.call(textRange, "Paste");
  }
  }
  /**
  * 在当前文档指定的位置拷贝表格
  *
  * @param pos
  * 当前文档指定的位置
  * @param tableIndex
  * 被拷贝的表格在word文档中所处的位置
  */
  public void copyTable(String pos, int tableIndex) {
  // 所有表格
  Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  // 要填充的表格
  Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
  .toDispatch();
  Dispatch range = Dispatch.get(table, "Range").toDispatch();
  Dispatch.call(range, "Copy");
  if (this.find(pos)) {
  Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
  Dispatch.call(textRange, "Paste");
  }
  }
  /**
  * 在当前文档指定的位置拷贝来自另一个文档中的表格
  *
  * @param anotherDocPath
  * 另一个文档的磁盘路径
  * @param tableIndex
  * 被拷贝的表格在另一格文档中的位置
  * @param pos
  * 当前文档指定的位置
  */
  public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex,
  String pos) {
  Dispatch doc2 = null;
  try {
  doc2 = Dispatch.call(documents, "Open", anotherDocPath)
  .toDispatch();
  // 所有表格
  Dispatch tables = Dispatch.get(doc2, "Tables").toDispatch();
  // 要填充的表格
  Dispatch table = Dispatch.call(tables, "Item",
  new Variant(tableIndex)).toDispatch();
  Dispatch range = Dispatch.get(table, "Range").toDispatch();
  Dispatch.call(range, "Copy");
  if (this.find(pos)) {
  Dispatch textRange = Dispatch.get(selection, "Range")
  .toDispatch();
  Dispatch.call(textRange, "Paste");
  }
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  if (doc2 != null) {
  Dispatch.call(doc2, "Close", new Variant(saveOnExit));
  doc2 = null;
  }
  }
  }

  本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/desertsurge/archive/2009/12/29/5097381.aspx

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

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


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