Jacob的简单使用

                这几天接到了一个项目,就是关于做中控指纹考勤机二次开发的,而中控的考勤机的dll是activex控件,所以使用jacob可以直接读取。在用jacob时,在做new active object的时候,里面传的参数是中控考勤机dll注册后的prog Id(zkemkeeper.ZKEM.1是在注册表里面找到的,在注册里面就搜索zkemsdk.dll就可以找到其progid了),不是classid。

                做这个项目时,要把中控的dll文件和jacob的jacob.dll拷到system32 下面 ,在做该项目时,要在工程中引入jacob.jar包.

下面介绍几个class:

1. ActiveXComponent Class: 可以说将com封装成一个组件。
2. Dispatch Class: 表示MS level dispatch object!相当于在windows上的资料结构。
3. Variant Class: 在java 与com中做互相通信的参数  

4. ComException Class: COM JNI 的错误处理。

幾個主要的函數:

1. call method: Dispatch class的函數!可用來呼叫COM/DLL的函數!它可以回傳結果(Variant variable)!
2. callSub method: 跟call函數類似!不過不回傳值!
3. get method: Dispatch class的函數!用來取得COM的屬性!
4. put method: Dispatch class的函數!用來設定COM的屬性!
5. invoke method: 等同於call函數!不過使用方式較複雜!
6. invokeSub method: 等同於callSub函數!不過使用方式較複雜!
7. getProperty method: ActiveXComponent class的函數!用來取得COM的屬性!
8. setProperty method: ActiveXComponent class的函數!用來設定COM的屬性!


通常要建立一個JACOB程式分成下面幾個步驟:

1. 構建ActiveX元件物件:
ActiveXComponent word = new ActiveXComponent("Word.Application");
其中的ActiveXComponent建構式內的值和你需要調用的ActiveX控制項有關!
一般常用的MS控制項

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

另外可用clsid建立! 如:  ActiveXComponent word = new ActiveXComponent("clsid:000209FF-0000-0000-C000-000000000046");

2. 設定或元件屬性:
如我要將word開啟後不隱藏!
word.setProperty("Visible", new Variant(true));//都是透過Variant來傳參數與COM溝通
Variant vs=Word.getProperty("Visible");

Dispatch class則透過了get與put
如:
Dispatch oDocuments = Word.getProperty("Documents").toDispatch();//透過toDispatch轉型
Variant v= Dispatch.get(oDocuments,"Parent");

3. 執行功能:
可以透過call! 也可以透過callN! 兩者不同點在於callN是以陣列方式傳參數!
Dispatch oDocument = Dispatch.call(oDocuments, "Open",  new Variant("C://atest.doc")).toDispatch();
Dispatch oDocument = Dispatch.callN(oDocuments, "Open", new Variant[]{ new Variant("C://atest.doc")}).toDispatch();

再用一個Dispatch的變數oDocument! 是為了之後能關閉close
Dispatch.call(oDocument, "Close", new Variant(false));
        
4. 關閉與回收資源
若有開啟檔案則要關閉與回收資源
if (Word != null) {
            Word.invoke("Quit", new Variant[] {new Variant(0)});
            Word = null;
            ComThread.Release();
}

jacob-1.18 包含jacob-1.18-x64.dll jacob-1.18-x86.dll 亲测可用: 下面为转pdf使用方法 package com.pdf.doctopdf.pdf; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import java.io.File; public class TestJacob { public static void main(String args[]) { ActiveXComponent app = null; String wordFile = "C:\\Users\\admin\\Desktop\\jar\\年会系统优化_20210820_V0.2.docx"; String pdfFile = "C:\\Users\\admin\\Desktop\\jar\\测试pdf.pdf"; System.out.println("开始转换..."); // 开始时间 long start = System.currentTimeMillis(); try { // 打开word app = new ActiveXComponent("Word.Application"); // 获得word中所有打开的文档 Dispatch documents = app.getProperty("Documents").toDispatch(); System.out.println("打开文件: " + wordFile); // 打开文档 Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch(); // 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在 File target = new File(pdfFile); if (target.exists()) { target.delete(); } System.out.println("另存为: " + pdfFile); // 另存为,将文档报错为pdf,其中word保存为pdf的格式宏的值是17 Dispatch.call(document, "SaveAs", pdfFile, 17); // 关闭文档 Dispatch.call(document, "Close", false); // 结束时间 long end = System.currentTimeMillis(); System.out.println("转换成功,用时:" + (end - start) + "ms"); }catch(Exception e) { e.getMessage(); System.out.println("转换失败"+e.getMessage()); }finally { // 关闭office app.invoke("Quit", 0); } } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值