java word jacob_使用Jacob与Word文件交互

importcom.jacob.com.*;importcom.jacob.activeX.*;/***

*@authorLonsy*/publicclassWordHandle {//运行时的Word程序privateActiveXComponent app;//word对象privateDispatch words;//当前的word文档privateDispatch doc;//当前光标位置privateDispatch cursor;//当前文档是否只读privatebooleanreadOnly;//当前文档中所有表格privateDispatch tables;//当前所在表格privateDispatch table;privateintcount;publicWordHandle()

{this.app=newActiveXComponent("Word.Application");this.app.setProperty("Visible",newVariant(false));//设置word不可见words=this.app.getProperty("Documents").toDispatch();this.doc=null;this.cursor=null;this.readOnly=true;this.count=0;

}/*** 打开word文件

*@paramfileName 文件名,使用绝对路径

*@paramreadOnly 是否只读

*@return*@throwsjava.lang.Exception*/publicbooleanopen(String fileName,booleanreadOnly)throwsException

{if(doc!=null)

{

System.out.println("当前文件未关闭");returnfalse;

}this.doc=Dispatch.invoke(this.words,"Open", Dispatch.Method,newObject[] {fileName,newVariant(false),newVariant(readOnly)},newint[1]).toDispatch();this.cursor=app.getProperty("Selection").toDispatch();this.tables=Dispatch.get(this.doc,"Tables").toDispatch();this.readOnly=readOnly;this.count=Dispatch.get(Dispatch.get(this.doc,"Words").toDispatch(),"Count").getInt()-1;

System.out.println("打开文件"+fileName+(readOnly?"ReadOnly":"Writable"));returntrue;

}/*** 建立新的Word文件

*@return*@throwsjava.lang.Exception*/publicbooleannewFile()throwsException

{if(doc!=null)

{

System.out.println("当前文件未关闭");returnfalse;

}this.doc=Dispatch.call(this.words,"Add").toDispatch();this.readOnly=false;this.cursor=app.getProperty("Selection").toDispatch();this.tables=Dispatch.get(this.doc,"Tables").toDispatch();

System.out.println("新建word文档");returntrue;

}/*** 关闭当前文件,并不保存

*@return*/publicbooleanclose()

{

String fileName=null;if(this.doc!=null)

{try{

fileName=Dispatch.get(this.doc,"Name").getString();

Dispatch.call(this.doc,"Close",newVariant(false));

}catch(Exception e)

{

e.printStackTrace();

}finally{this.doc=null;

}

}

System.out.println("关闭文件"+fileName);returntrue;

}/*** 退出

*@return*/publicbooleanquit()

{try{this.app.invoke("Quit",newVariant[] {});

}catch(Exception e)

{

e.printStackTrace();

}

System.out.println("退出word");returntrue;

}/*** 另存为

*@paramfileName 目标文件名,为绝对路径

*@return*/publicbooleansaveAs(String fileName)throwsException

{if(this.doc==null)

{

System.out.println("当前无文件");returnfalse;

}else{

Dispatch.call(this.doc,"SaveAs", fileName);

System.out.println("另存为"+fileName);returntrue;

}

}/*** 保存

*@return*/publicbooleansave()throwsException

{if(this.doc==null)

{

System.out.println("当前无文档,无法保存");returnfalse;

}else{if(this.readOnly)

{

System.out.println("只读文档,保存失败");returnfalse;

}

Dispatch.call(this.doc,"Save");

System.out.println("保存完成");returntrue;

}

}/*** 将光标或选定内容向右移动

*@paramstep

*@return*/publicbooleanmoveRight(intsteps)throwsException

{//int start = Dispatch.get(this.cursor, "Start").getInt();//Dispatch.put(this.cursor, "Start", start + steps);for(inti=0; i

{

Dispatch.call(cursor,"MoveRight");

}returntrue;

}/*** 将光标或选定内容向左移动

*@paramstep

*@return*/publicbooleanmoveLeft(intsteps)throwsException

{for(inti=0; i

{

Dispatch.call(cursor,"MoveLeft");

}returntrue;

}/*** 从当前位置起查找指定内容,并将光标移到相应位置上

*@paramstr

*@return光标位置,未找到则为0

*@throwsjava.lang.Exception*/publicintsearch(String str)throwsException

{//从cursor所在位置开始查询Dispatch find=Dispatch.call(this.cursor,"Find").toDispatch();//设置要查找的内容Dispatch.put(find,"Text", str);//向前查找Dispatch.put(find,"Forward","True");//设置格式Dispatch.put(find,"Format","True");//大小写匹配Dispatch.put(find,"MatchCase","True");//全字匹配Dispatch.put(find,"MatchWholeWord","True");//查找if(!Dispatch.call(find,"Execute").getBoolean())return0;else{returnDispatch.get(this.cursor,"Start").getInt();

}

}/*** 从当前位置起查找指定内容,并只将光标开始处移到相应位置上

*@paramstr

*@return光标位置,未找到则为0

*@throwsjava.lang.Exception*/publicintsearchOnly(String str)throwsException

{//从cursor所在位置开始查询Dispatch find=Dispatch.call(this.cursor,"Find").toDispatch();//设置要查找的内容Dispatch.put(find,"Text", str);//向前查找Dispatch.put(find,"Forward","True");//大小写匹配Dispatch.put(find,"MatchCase","True");//全字匹配Dispatch.put(find,"MatchWholeWord","True");if(!Dispatch.call(find,"Execute").getBoolean())return0;else{intstart=Dispatch.get(this.cursor,"Start").getInt();

Dispatch.put(this.cursor,"End",this.count);//System.out.println(start);returnstart;

}

}publicString getBetween(intstart,intend)throwsException

{

Dispatch range=Dispatch.get(this.cursor,"Range").toDispatch();

Dispatch.call(range,"SetRange", start, end);returnDispatch.get(range,"Text").getString();

}publicString getLineAfter(intstart)throwsException

{

Dispatch.put(this.cursor,"Start", start);intlength=Dispatch.call(this.cursor,"EndKey").getInt()+start;returngetBetween(start, length);

}publicString getLine(intposition)throwsException

{

Dispatch.put(this.cursor,"Start", position);

Dispatch.call(this.cursor,"SelectRow");intstart=Dispatch.get(this.cursor,"Start").getInt();intend=Dispatch.get(this.cursor,"End").getInt();returngetBetween(start, start+end);

}publicbooleangotoPage(intindex)throwsException

{

Dispatch.invoke(this.cursor,"Goto", Dispatch.Method,newObject[] {1,2, String.valueOf(index)},newint[1]);//Dispatch.call(this.cursor, "GoTo", "wdGoToLine", "wdGoToNext", String.valueOf(index), null);returntrue;

}publicintgetCurrentCursor()throwsException

{returnDispatch.get(this.cursor,"Start").getInt();

}publicbooleansetCursorMode()throwsException

{

Dispatch.put(this.cursor,"End", Dispatch.get(this.cursor,"Start").getInt());returntrue;

}publicbooleangotoHome()throwsException

{

Dispatch.put(this.cursor,"Start",0);returntrue;

}/*** 在光标后指定便宜插入字符串,并将光标移到新位置

*@paramstr

*@return*@throwsjava.lang.Exception*/publicbooleaninsert(intsteps, String str)throwsException

{intstart=Dispatch.get(this.cursor,"Start").getInt()+steps;

Dispatch.put(this.cursor,"Start", start);

Dispatch.call(this.cursor,"InsertBefore", str);this.getCount();

Dispatch.put(this.cursor,"Start", start+str.length());//System.out.println(Dispatch.get(this.cursor, "Start").getInt() + "   " + (Dispatch.get(this.cursor, "Start").getInt()+Dispatch.get(this.cursor, "End").getInt()));returntrue;

}/*** 用指定的字符串替代当前选定的内容

*@paramstr

*@return*@throwsjava.lang.Exception*/publicbooleanreplace(String str)throwsException

{

Dispatch.put(this.cursor,"Text", str);returntrue;

}/*** 获取当前文档中的表格数

*@return*@throwsjava.lang.Exception*/publicintgetTableNum()throwsException

{returnDispatch.get(this.tables,"Count").getInt();

}/*** 设定当前工作表格

*@paramindex

*@return*@throwsjava.lang.Exception*/publicbooleansetCurrentTable(intindex)throwsException

{this.table=Dispatch.call(this.tables,"Item",newVariant(index)).toDispatch();

Dispatch.call(this.table,"Select");returntrue;

}/*** 获取指定单元格的内容

*@paramrow

*@paramcol

*@return*@throwsjava.lang.Exception*/publicString getCell(introw,intcol)throwsException

{

Dispatch cell=Dispatch.call(table,"Cell", Integer.toString(row), Integer.toString(col)).toDispatch();

Dispatch.call(cell,"Select");

String tmp=Dispatch.get(this.cursor,"Text").getString();//System.out.println(".." + tmp);if(tmp.length()>2)

{returntmp.substring(0, tmp.length()-2);

}elsereturn"";

}/*** 用指定的字符串代替当前表格中指定的单元格

*@paramrow

*@paramcol

*@paramstr

*@return*@throwsjava.lang.Exception*/publicbooleanreplaceCell(introw,intcol, String str)throwsException

{

Dispatch cell=Dispatch.call(table,"Cell", Integer.toString(row), Integer.toString(col)).toDispatch();

Dispatch.call(cell,"Select");

Dispatch.put(this.cursor,"Text", str);returntrue;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值