JAVA Excel com组件_jacob java调用com组件基础运用

1、jacob环境部署

82fc08422c8e363634b2cdef764931f0.png

2297813384d14cab473ad709a71d2256.png

jdk32位或者64位把相应的.dll文件放到jdk安装目录的bin文件夹下

2、基础运行

ActiveXComponent ax = new ActiveXComponent("a1");//构建ActiveX组件实例

其中的a1的值和你需要调用的ActiveX控件有关

MS控件名

a1的值

InternetExplorer

InternetExplorer.Application

Excel

Excel.Application

Word

Word.Application

Powerpoint

Powerpoint.Application

vb/java Script

ScriptControl

windows media Player

WMPlayer.OCX

Outlook

Outlook.Application

Visio

Visio.Application

DAO

DAO.PrivateDBEngine.35

MultiFace

MultiFace.Face

import  com.jacob.activeX.ActiveXComponent;

import  com.jacob.com.Dispatch;

import  com.jacob.com.Variant;

import  com.jacob.com.ComThread;

public   class  StudyJacob {

// word文档

private  Dispatch doc =  null ;

// word运行程序对象

private  ActiveXComponent word =  null ;

// 所有word文档集合

private  Dispatch documents =  null ;

// 选定的范围或插入点

private   static  Dispatch selection =  null ;

// 设置是否保存后才退出的标志

private   boolean  saveOnExit =  true ;

/**

* word中的当前文档

*/

private   static  Dispatch document =  null ;

// private static Dispatch textFrame = null;

//

/**

* 所有表格

*/

private  Dispatch tables;

/**

* 当前表格

*/

private  Dispatch table;

/**

* 当前单元格

*/

private  Dispatch cell;

/**

* 当前表格中的所有行

*/

private  Dispatch rows;

/**

* 表格中的某一行

*/

private  Dispatch row;

/**

* 表格中的所有列

*/

private  Dispatch columns;

/**

* 表格中的某一列

*/

private  Dispatch column;

/**

* 打开word时同时要打开的文档,不指定时将新建一个空白文档

*/

// private File openDoc;

private   static  Dispatch shapes;

private   static  Dispatch shape;

private   static  Dispatch textRange;

private   static  Dispatch textframe;

private  Dispatch range;

private  Dispatch paragraphs;

private  Dispatch paragraph;

// constructor

public  StudyJacob() {

if  (word ==  null ) {

word =  new  ActiveXComponent( "Word.Application" );

word.setProperty( "Visible" ,  new  Variant( true ));

}

if  (documents ==  null ) {

documents = word.getProperty( "Documents" ).toDispatch();

}

}

/**

* 创建一个新的word文档

*

*/

public   void  createNewDocument() {

doc = Dispatch.call(documents,  "Add" ).toDispatch();

selection = Dispatch.get(word,  "Selection" ).toDispatch();

}

/**

* 打开一个已存在的文档

*

* @param docPath

*/

public   void  openDocument(String docPath) {

if  ( this .doc !=  null ) {

this .closeDocument();

}

doc = Dispatch.call(documents,  "Open" , docPath).toDispatch();

selection = Dispatch.get(word,  "Selection" ).toDispatch();

}

/**

* 关闭当前word文档

*

*/

public   void  closeDocument() {

if  (doc !=  null ) {

Dispatch.call(doc,  "Save" );

Dispatch.call(doc,  "Close" ,  new  Variant(saveOnExit));

doc =  null ;

}

}

/**

* 关闭全部应用

*

*/

public   void  close() {

closeDocument();

if  (word !=  null ) {

Dispatch.call(word,  "Quit" );

word =  null ;

}

selection =  null ;

documents =  null ;

}

/**

* 把插入点移动到文件首位置

*

*/

public   void  moveStart() {

if  (selection ==  null )

selection = Dispatch.get(word,  "Selection" ).toDispatch();

Dispatch.call(selection,  "HomeKey" ,  new  Variant( 6 ));

}

/**

* 在当前插入点插入字符串

*

* @param newText

*要插入的新字符串

*/

public   void  insertText(String newText) {

Dispatch.put(selection,  "Text" , newText);

}

/**

* 在当前插入点插入图片

*

* @param imagePath

*图片路径

*/

public   void  insertImage(String imagePath) {

Dispatch.call(Dispatch.get(selection,  "InLineShapes" ).toDispatch(),

"AddPicture" , imagePath);

}

/**

* 把选定的内容或者插入点向下移动

*

* @param pos

*移动的距离

*/

public   void  moveDown( int  pos) {

if  (selection ==  null )

selection = Dispatch.get(word,  "Selection" ).toDispatch();

for  ( int  i =  0 ; i 

Dispatch.call(selection,  "MoveDown" );

}

/**

* 把选定的内容或插入点向上移动

*

* @param pos

*移动的距离

*/

public   void  moveUp( int  pos) {

if  (selection ==  null )

selection = Dispatch.get(word,  "Selection" ).toDispatch();

for  ( int  i =  0 ; i 

Dispatch.call(selection,  "MoveUp" );

}

/**

* 把选定的内容或者插入点向左移动

*

* @param pos

*移动的距离

*/

public   void  moveLeft( int  pos) {

if  (selection ==  null )

selection = Dispatch.get(word,  "Selection" ).toDispatch();

for  ( int  i =  0 ; i 

Dispatch.call(selection,  "MoveLeft" );

}

}

/**

* 把选定的内容或者插入点向右移动

*

* @param pos

*移动的距离

*/

public   void  moveRight( int  pos) {

if  (selection ==  null )

selection = Dispatch.get(word,  "Selection" ).toDispatch();

for  ( int  i =  0 ; i 

Dispatch.call(selection,  "MoveRight" );

}

/**

* 文件保存或另存为

*

* @param savePath

*一定要记得加上扩展名 .doc 保存或另存为路径

*/

public   void  save(String savePath) {

Dispatch.call(

(Dispatch) Dispatch.call(word,  "WordBasic" ).getDispatch(),

"FileSaveAs" , savePath);

}

/**

* 从第tIndex个Table中取出值第row行,第col列的值

*

* @param tableIndex

*文档中的第tIndex个Table,即tIndex为索引取

* @param cellRowIdx

*cell在Table第row行

* @param cellColIdx

*cell在Talbe第col列

* @return cell单元值

* @throws Exception

*/

public  String getCellString( int  tableIndex,  int  cellRowIdx,  int  cellColIdx)

throws  Exception {

// 所有表格

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" );

return  Dispatch.get(selection,  "Text" ).toString();

}

/**

* 从第tableIndex个Table中取出值第cellRowIdx行,第cellColIdx列的值

*

* @param tIndex

*文档中的第tIndex个Table,即tIndex为索引取

* @param cellRowIdx

*cell在Table第row行

* @param cellColIdx

*cell在Talbe第col列

* @return cell单元值

* @throws Exception

*/

public   void  getCellValue( int  tableIndex,  int  cellRowIdx,  int  cellColIdx)

throws  Exception {

// 所有表格

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.call(selection,  "Copy" );

}

/**

* 在当前光标处做粘贴

*/

public   void  paste() {

Dispatch.call(selection,  "Paste" );

}

/**

* 在当前光标处添加图片

*

* @param imgPath

*图片的地址

*/

public   void  addImage(String imgPath) {

if  (imgPath !=  ""  && imgPath !=  null ) {

Dispatch image = Dispatch.get(selection,  "InLineShapes" )

.toDispatch();

Dispatch.call(image,  "AddPicture" , imgPath);

}

}

/**

* 在指定的单元格里填写数据

*

* @param tableIndex

*文档中的第tIndex个Tableÿ

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值