jacob操作word教程

用jacob做了一个东西,就是对word进行操作,开始时费了好大劲,后来总算是有点思路。现将已试验过的方法总结如下。

还有一点就是所用的JAR文件和DLL文件好像比较特殊,JDK换来换去就用了JDK1.6,jacob.jar为1.9的,dll为2005年2月26日的。

有什么问题可以在此留言,大家一起交流。

 

  1. import  com.jacob.activeX.ActiveXComponent;
  2. import  com.jacob.com.Dispatch;
  3. import  com.jacob.com.Variant;
  4. import  com.jacob.com.ComThread;
  5. public   class  StudyJacob {
  6.      // word文档
  7.      private  Dispatch doc =  null ;
  8.      // word运行程序对象
  9.      private  ActiveXComponent word =  null ;
  10.      // 所有word文档集合
  11.      private  Dispatch documents =  null ;
  12.      // 选定的范围或插入点
  13.      private   static  Dispatch selection =  null ;
  14.      // 设置是否保存后才退出的标志
  15.      private   boolean  saveOnExit =  true ;
  16.      /**
  17.      * word中的当前文档
  18.      */
  19.      private   static  Dispatch document =  null ;
  20.      // private static Dispatch textFrame = null;
  21.      //
  22.      /**
  23.      * 所有表格
  24.      */
  25.      private  Dispatch tables;
  26.      /**
  27.      * 当前表格
  28.      */
  29.      private  Dispatch table;
  30.      /**
  31.      * 当前单元格
  32.      */
  33.      private  Dispatch cell;
  34.      /**
  35.      * 当前表格中的所有行
  36.      */
  37.      private  Dispatch rows;
  38.      /**
  39.      * 表格中的某一行
  40.      */
  41.      private  Dispatch row;
  42.      /**
  43.      * 表格中的所有列
  44.      */
  45.      private  Dispatch columns;
  46.      /**
  47.      * 表格中的某一列
  48.      */
  49.      private  Dispatch column;
  50.      /**
  51.      * 打开word时同时要打开的文档,不指定时将新建一个空白文档
  52.      */
  53.      // private File openDoc;
  54.      private   static  Dispatch shapes;
  55.      private   static  Dispatch shape;
  56.      private   static  Dispatch textRange;
  57.      private   static  Dispatch textframe;
  58.      private  Dispatch range;
  59.      private  Dispatch paragraphs;
  60.      private  Dispatch paragraph;
  61.      // constructor
  62.      public  StudyJacob() {
  63.          if  (word ==  null ) {
  64.             word =  new  ActiveXComponent( "Word.Application" );
  65.             word.setProperty( "Visible"  new  Variant( true ));
  66.         }
  67.          if  (documents ==  null ) {
  68.             documents = word.getProperty( "Documents" ).toDispatch();
  69.         }
  70.     }
  71.      /**
  72.      * 创建一个新的word文档
  73.      * 
  74.      */
  75.      public   void  createNewDocument() {
  76.         doc = Dispatch.call(documents,  "Add" ).toDispatch();
  77.         selection = Dispatch.get(word,  "Selection" ).toDispatch();
  78.     }
  79.      /**
  80.      * 打开一个已存在的文档
  81.      * 
  82.      * @param docPath
  83.      */
  84.      public   void  openDocument(String docPath) {
  85.          if  ( this .doc !=  null ) {
  86.              this .closeDocument();
  87.         }
  88.         doc = Dispatch.call(documents,  "Open" , docPath).toDispatch();
  89.         selection = Dispatch.get(word,  "Selection" ).toDispatch();
  90.     }
  91.      /**
  92.      * 关闭当前word文档
  93.      * 
  94.      */
  95.      public   void  closeDocument() {
  96.          if  (doc !=  null ) {
  97.             Dispatch.call(doc,  "Save" );
  98.             Dispatch.call(doc,  "Close"  new  Variant(saveOnExit));
  99.             doc =  null ;
  100.         }
  101.     }
  102.      /**
  103.      * 关闭全部应用
  104.      * 
  105.      */
  106.      public   void  close() {
  107.         closeDocument();
  108.          if  (word !=  null ) {
  109.             Dispatch.call(word,  "Quit" );
  110.             word =  null ;
  111.         }
  112.         selection =  null ;
  113.         documents =  null ;
  114.     }
  115.      /**
  116.      * 把插入点移动到文件首位置
  117.      * 
  118.      */
  119.      public   void  moveStart() {
  120.          if  (selection ==  null )
  121.             selection = Dispatch.get(word,  "Selection" ).toDispatch();
  122.         Dispatch.call(selection,  "HomeKey"  new  Variant( 6 ));
  123.     }
  124.      /**
  125.      * 在当前插入点插入字符串
  126.      * 
  127.      * @param newText
  128.      *            要插入的新字符串
  129.      */
  130.      public   void  insertText(String newText) {
  131.         Dispatch.put(selection,  "Text" , newText);
  132.     }
  133.      /**
  134.      * 在当前插入点插入图片
  135.      * 
  136.      * @param imagePath
  137.      *            图片路径
  138.      */
  139.      public   void  insertImage(String imagePath) {
  140.         Dispatch.call(Dispatch.get(selection,  "InLineShapes" ).toDispatch(),
  141.                  "AddPicture" , imagePath);
  142.     }
  143.      /**
  144.      * 把选定的内容或者插入点向下移动
  145.      * 
  146.      * @param pos
  147.      *            移动的距离
  148.      */
  149.      public   void  moveDown( int  pos) {
  150.          if  (selection ==  null )
  151.             selection = Dispatch.get(word,  "Selection" ).toDispatch();
  152.          for  ( int  i =  0 ; i < pos; i++)
  153.             Dispatch.call(selection,  "MoveDown" );
  154.     }
  155.      /**
  156.      * 把选定的内容或插入点向上移动
  157.      * 
  158.      * @param pos
  159.      *            移动的距离
  160.      */
  161.      public   void  moveUp( int  pos) {
  162.          if  (selection ==  null )
  163.             selection = Dispatch.get(word,  "Selection" ).toDispatch();
  164.          for  ( int  i =  0 ; i < pos; i++)
  165.             Dispatch.call(selection,  "MoveUp" );
  166.     }
  167.      /**
  168.      * 把选定的内容或者插入点向左移动
  169.      * 
  170.      * @param pos
  171.      *            移动的距离
  172.      */
  173.      public   void  moveLeft( int  pos) {
  174.          if  (selection ==  null )
  175.             selection = Dispatch.get(word,  "Selection" ).toDispatch();
  176.          for  ( int  i =  0 ; i < pos; i++) {
  177.             Dispatch.call(selection,  "MoveLeft" );
  178.         }
  179.     }
  180.      /**
  181.      * 把选定的内容或者插入点向右移动
  182.      * 
  183.      * @param pos
  184.      *            移动的距离
  185.      */
  186.      public   void  moveRight( int  pos) {
  187.          if  (selection ==  null )
  188.             selection = Dispatch.get(word,  "Selection" ).toDispatch();
  189.          for  ( int  i =  0 ; i < pos; i++)
  190.             Dispatch.call(selection,  "MoveRight" );
  191.     }
  192.      /**
  193.      * 文件保存或另存为
  194.      * 
  195.      * @param savePath
  196.      *            一定要记得加上扩展名 .doc 保存或另存为路径
  197.      */
  198.      public   void  save(String savePath) {
  199.         Dispatch.call(
  200.                 (Dispatch) Dispatch.call(word,  "WordBasic" ).getDispatch(),
  201.                  "FileSaveAs" , savePath);
  202.     }
  203.      /**
  204.      * 从第tIndex个Table中取出值第row行,第col列的值
  205.      * 
  206.      * @param tableIndex
  207.      *            文档中的第tIndex个Table,即tIndex为索引取
  208.      * @param cellRowIdx
  209.      *            cell在Table第row行
  210.      * @param cellColIdx
  211.      *            cell在Talbe第col列
  212.      * @return cell单元值
  213.      * @throws Exception
  214.      */
  215.      public  String getCellString( int  tableIndex,  int  cellRowIdx,  int  cellColIdx)
  216.              throws  Exception {
  217.          // 所有表格
  218.         Dispatch tables = Dispatch.get(doc,  "Tables" ).toDispatch();
  219.          // 要取数据的表格
  220.         Dispatch table = Dispatch.call(tables,  "Item"  new  Variant(tableIndex))
  221.                 .toDispatch();
  222.         Dispatch cell = Dispatch.call(table,  "Cell"  new  Variant(cellRowIdx),
  223.                  new  Variant(cellColIdx)).toDispatch();
  224.         Dispatch.call(cell,  "Select" );
  225.          return  Dispatch.get(selection,  "Text" ).toString();
  226.     }
  227.      /**
  228.      * 从第tableIndex个Table中取出值第cellRowIdx行,第cellColIdx列的值
  229.      * 
  230.      * @param tIndex
  231.      *            文档中的第tIndex个Table,即tIndex为索引取
  232.      * @param cellRowIdx
  233.      *            cell在Table第row行
  234.      * @param cellColIdx
  235.      *            cell在Talbe第col列
  236.      * @return cell单元值
  237.      * @throws Exception
  238.      */
  239.      public   void  getCellValue( int  tableIndex,  int  cellRowIdx,  int  cellColIdx)
  240.              throws  Exception {
  241.          // 所有表格
  242.         Dispatch tables = Dispatch.get(doc,  "Tables" ).toDispatch();
  243.          // 要取数据的表格
  244.         Dispatch table = Dispatch.call(tables,  "Item"  new  Variant(tableIndex))
  245.                 .toDispatch();
  246.         Dispatch cell = Dispatch.call(table,  "Cell"  new  Variant(cellRowIdx),
  247.                  new  Variant(cellColIdx)).toDispatch();
  248.         Dispatch.call(cell,  "Select" );
  249.         Dispatch.call(selection,  "Copy" );
  250.     }
  251.      /**
  252.      * 在当前光标处做粘贴
  253.      */
  254.      public   void  paste() {
  255.         Dispatch.call(selection,  "Paste" );
  256.     }
  257.      /**
  258.      * 在当前光标处添加图片
  259.      * 
  260.      * @param imgPath
  261.      *            图片的地址
  262.      */
  263.      public   void  addImage(String imgPath) {
  264.          if  (imgPath !=  ""  && imgPath !=  null ) {
  265.             Dispatch image = Dispatch.get(selection,  "InLineShapes" )
  266.                     .toDispatch();
  267.             Dispatch.call(image,  "AddPicture" , imgPath);
  268.         }
  269.     }
  270.      /**
  271.      * 在指定的单元格里填写数据
  272.      * 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值