前期总结之jacob说明

本文介绍使用JACOB库在Java中操作Word文档的方法,包括文档打开、内容查找替换、格式设置等,并提供了一些示例代码。

 

这是jacob官方网站的原话,本人就不再多说了。jacob实现有些像封装com功能的jni调用的集合及承载com对象的容器。jacob作者Dan Adler使用了c++编写了一批程序库实现对com的引用/承载/调用,然后使用javajni技术调用这些程序库,实现JAVA-COM Bridge关于作者如何封装的可以参考http://danadler.com/jacob/,其方法和类与微软的javasdk文档一致,有兴趣的朋友可以look一下。
 
参照各种文档和资料(主要是网上的一些好的有价值的帖子)得出JACOB操作WORD的一些方法。(来源本目录下testJab.jar
//本文各种方法仅是作者参照网上资料所得,向他们表示谢意和致敬,特此声明)
打开WORD程序WINWORD.EXE(非WORD文档)
ActiveXComponent oWord = new ActiveXComponent("Word.Application");
// 打开WORD文档时用户可见不可见,设为false时不可见
oWord.setProperty("Visible", new Variant(tVisible));
//取得Documents属性
Object oDocuments = oWord.getProperty("Documents").toDispatch();
//打开sInputDoc指定的word文档
Object oDocument = Dispatch.call((Dispatch) oDocuments, "Open",
sInputDoc).toDispatch();
//取得word程序的select功能,相当如在手动在word中选择“查找/替换”
Object oSelection = oWord.getProperty("Selection").toDispatch();
//查找
Object oFind = oWord.call((Dispatch) oSelection, "Find").toDispatch();
// ////////////////////////////////////////////////////////////////////
//设置查找的内容
Dispatch.put((Dispatch) oFind, "Text", sOldText);
//执行查找,默认设置,查找一次并向下查找
//返回一个variant变量,转换成boolean属性
//成功查找到返回true,否则false
//查找成功,word中,sOldText为选中状态
boolean find = Dispatch.call((Dispatch) oFind, "Execute").getBoolean();
//如查找成功,则sNewText替换查找到的sOldText
//否则sNewText将填入到目前光标所在地
Dispatch.put((Dispatch) oSelection, "Text", sNewText);
 
 
// ////////////////////////////////////////////////////////////////////
// 所以使用Find对象的Execute(FindText, MatchCase, MatchWholeWord,
// MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap,
// Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics,
// MatchAlefHamza, MatchControl)方法 //详细内容见MSDNoffice2000开发文档
//通过设置以下的各种属性来匹配查找/替换功能
//目前设置属性为全篇全部替换
Variant True = new Variant(true);
Variant False = new Variant(false);
Variant FindText = new Variant(sOldText);
//替换的内容
Variant ReplaceWith = new Variant(replaceStr);
Variant Format = False;
Variant Forward = False;
Variant MatchCase = False;
Variant MatchWholeWord = False;
 
Variant MatchWildcards = False;
Variant MatchSoundsLike = False;
Variant MatchAllWordForms = False;
 
int wdFindWrap_FindContinue = 1;
Variant Wrap = new Variant(wdFindWrap_FindContinue);
int wdReplace_ReplaceAll = 2;
Variant Replace = new Variant(wdReplace_ReplaceAll);
//查找的内容
Dispatch.put((Dispatch) oFind,"Text",sOldText);
boolean find = Dispatch.callN(
(Dispatch) oFind,
"Execute",
new Variant[] { FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms,
Forward, Wrap, Format, ReplaceWith, Replace })
.getBoolean();
System.out.println(find);
FindText = new Variant("河南省");
find = Dispatch.callN(
(Dispatch) oFind,
"Execute",
new Variant[] { FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms,
Forward, Wrap, Format, ReplaceWith, Replace })
.getBoolean();
 
// ////////////////////////////////////////////////////////////////////光标移动
//光标下移一行
Dispatch.call((Dispatch) oSelection, "MoveDown");
//光标上移一行
Dispatch.call((Dispatch) oSelection, "MoveUp");
//光标右移一行
Dispatch.call((Dispatch) oSelection, "MoveRight");
//光标左移一行
Dispatch.call((Dispatch) oSelection, "MoveLeft");
// ////////////////////////////////////////////////////////////////////
//设置oSelection中的格式Font
//只有oSelection有选中内容,设置属性才有效
Object oFont = Dispatch.get((Dispatch) oSelection, "Font")
.toDispatch();
Dispatch.put((Dispatch) oFont, "Bold", "1");
Dispatch.put((Dispatch) oFont, "Italic", "1");
Dispatch.put((Dispatch) oFont, "Underline", "1");
Dispatch.put((Dispatch) oFont, "Size", "20");
Dispatch.put((Dispatch) oFont, "Color", "1,0,0,0");
//Dispatch.put((Dispatch) oFont, "centre", "1");
//Dispatch.put((Dispatch) oFont, "block", "1");
//Object range=Dispatch.get((Dispatch) oSelection,"Range").toDispatch();
//Dispatch.put((Dispatch) range, "Size", "20");
 
// ////////////////////////////////////////////////////////////////////
Object oAlign = Dispatch.get((Dispatch) oSelection,
"ParagraphFormat").toDispatch();
Dispatch.put((Dispatch) oAlign, "Alignment", "1");
// Dispatch.put(oAlign, "Color", "1");
// ////////////////////////////////////////////////////////////////////
Object oWordBasic = Dispatch.call(oWord, "WordBasic").getDispatch();
//文档savaAs功能
// Dispatch.call(oWord, "SaveAs", sInputDoc);
//文档保存功能
tSaveOnExit = true;// 设为true时,则在退出时保存
Dispatch.call(oWord, "SaveAs");
Dispatch.call((Dispatch) oDocument, "SaveAs");
Dispatch.call((Dispatch) oDocument, "SaveAs", "E:/temp/tword7.doc");
//文档关闭,tSaveOnExittrue时关闭保存,否则关闭不保存
Dispatch.call((Dispatch) oDocument, "Close", new Variant(tSaveOnExit));
//退出word程序
oWord.invoke("Quit", new Variant[0]);
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值