首先,要下载jacob.jar包和jacob.dll文件,然后把jacob.dll放在jre/bin目录下或者system32目录下,把jacob.jar包放在项目的lib目录下。
简单例子一:打开Word
package test;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Variant;
public class TestCom {
/**
* @param args
*/
public static void main(String[] args) {
ActiveXComponent app = new ActiveXComponent("Word.Application");
try {
app.setProperty("Visible", new Variant(true)); //显示Word
} catch (Exception e) {
e.printStackTrace();
}
}
}
简单例子二:调用本地Com+组件
package test;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Variant;
public class TestCom {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ActiveXComponent obj = new ActiveXComponent("WFUtils.WFConfig");
try {
//第一个参数getconfig为方法名,第二个参数pageRoot为getconfig方法的第一个参数,第三个参数/foshan/为getconfig方法的第二个参数,所有方法的参数都可以统一放在Variant中。
Variant str=obj.invoke("getconfig",new Variant("pageRoot"),new Variant("/foshan/"));
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}