前期总结之jacob说明

 

这是 jacob 官方网站的原话,本人就不再多说了。 jacob 实现 有些像 封装 com 功能的 jni 调用 的集合及 承载 com 对象的容器。 jacob 作者 Dan Adler 使用了 c++ 编写了一批程序库实现对 com 的引用 / 承载 / 调用,然后使用 java jni 技术调用这些程序库,实现 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) 方法 // 详细内容见 MSDN office2000 开发文档
// 通过设置以下的各种属性来匹配查找 / 替换功能
// 目前设置属性为全篇全部替换
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");
// 文档关闭, tSaveOnExit true 时关闭保存,否则关闭不保存
Dispatch.call((Dispatch) oDocument, "Close", new Variant(tSaveOnExit));
// 退出 word 程序
oWord.invoke("Quit", new Variant[0]);
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值