java jacob api_jacob操作API

importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.ComThread;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;/***

*

*@authorBruceLeey

**/publicclassMSWordManager {//word文档privateDispatch doc;//word运行程序对象privateActiveXComponent word;//所有word文档集合privateDispatch documents;//选定的范围或插入点privateDispatch selection;privatebooleansaveOnExit=true;publicMSWordManager() {

ComThread.InitSTA();if(word==null) {

word=newActiveXComponent("Word.Application");

word.setProperty("Visible",newVariant(false));

}if(documents==null)

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

}/*** 设置退出时参数

*

*@paramsaveOnExit

*            boolean true-退出时保存文件,false-退出时不保存文件*/publicvoidsetSaveOnExit(booleansaveOnExit) {this.saveOnExit=saveOnExit;

}/*** 创建一个新的word文档

**/publicvoidcreateNewDocument() {

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

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

}/*** 打开一个已存在的文档

*

*@paramdocPath*/publicvoidopenDocument(String docPath) {

closeDocument();

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

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

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

*

*@parampos

*            移动的距离*/publicvoidmoveUp(intpos) {if(selection==null)

selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0; i

Dispatch.call(selection,"MoveUp");

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

*

*@parampos

*            移动的距离*/publicvoidmoveDown(intpos) {if(selection==null)

selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0; i

Dispatch.call(selection,"MoveDown");

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

*

*@parampos

*            移动的距离*/publicvoidmoveLeft(intpos) {if(selection==null)

selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0; i

Dispatch.call(selection,"MoveLeft");

}

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

*

*@parampos

*            移动的距离*/publicvoidmoveRight(intpos) {if(selection==null)

selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0; i

Dispatch.call(selection,"MoveRight");

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

**/publicvoidmoveStart() {if(selection==null)

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

Dispatch.call(selection,"HomeKey",newVariant(6));

}/*** 从选定内容或插入点开始查找文本

*

*@paramtoFindText

*            要查找的文本

*@returnboolean true-查找到并选中该文本,false-未查找到文本*/publicbooleanfind(String toFindText) {if(toFindText==null||toFindText.equals(""))returnfalse;//从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");//查找并选中returnDispatch.call(find,"Execute").getBoolean();

}/*** 把选定选定内容设定为替换文本

*

*@paramtoFindText

*            查找字符串

*@paramnewText

*            要替换的内容

*@return*/publicbooleanreplaceText(String toFindText, String newText) {if(!find(toFindText))returnfalse;

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

}/*** 全局替换文本

*

*@paramtoFindText

*            查找字符串

*@paramnewText

*            要替换的内容*/publicvoidreplaceAllText(String toFindText, String newText) {while(find(toFindText)) {

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

Dispatch.call(selection,"MoveRight");

}

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

*

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值