Jacob基本使用方法

package comtest;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ComTest {
	
	public static void main(String[] args) {
		ActiveXComponent axc = new ActiveXComponent("Excel.Application");
		System.out.println(axc.getProperty("Version"));
		Dispatch wb = axc.getProperty("WorkBooks").toDispatch();
		String fn = "F:\\vba\\vars.xls";
		Dispatch w = Dispatch.invoke(wb, "Open", Dispatch.Method, new Object[]{fn, new Variant(true),
				new Variant(false)}, new int[1]).toDispatch();
		
//		Dispatch vbe = axc.getProperty("VBE").toDispatch();
//		Dispatch cbs = Dispatch.call(vbe, "CommandBars").toDispatch();
//		int count = Dispatch.call(cbs, "Count").getInt();
//		System.out.println("vbe count:"+count);
//		Dispatch cb = Dispatch.call(cbs, "Item", new Integer(1)).toDispatch();
//		Dispatch cts = Dispatch.call(cb, "Controls").toDispatch();
//		int menucount = Dispatch.call(cts, "Count").getInt();
//		System.out.println("menucount:"+menucount);
//		Dispatch ct = Dispatch.call(cts, "Item", new Integer(8)).toDispatch();
//		Dispatch xcts = Dispatch.call(ct, "Controls").toDispatch();
//		Dispatch xct = Dispatch.call(xcts, "Item", new Integer(5)).toDispatch();
//		Dispatch.call(xct, "Execute");
//		
//		axc.invoke("SendKeys", new Variant(""), new Variant(false));
		
		Dispatch.call(w, "Close", new Variant(false));
		
		
	}

}




触发事件的
ActiveXComponent axc = new ActiveXComponent("Excel.Application");
		System.out.println(axc.getProperty("Version"));
		Dispatch wb = axc.getProperty("WorkBooks").toDispatch();
		String fn = "F:\\vba\\jiami.xls";
		Dispatch w = Dispatch.invoke(wb, "Open", Dispatch.Method, new Object[]{fn, new Variant(true),
				new Variant(false)}, new int[1]).toDispatch();
		
		Dispatch vbe = axc.getProperty("VBE").toDispatch();
		
		Dispatch cbs = Dispatch.call(vbe, "CommandBars").toDispatch();
//		int count = Dispatch.call(cbs, "Count").getInt();
//		System.out.println("vbe count:"+count);
		Dispatch cb = Dispatch.call(cbs, "Item", new Integer(1)).toDispatch();
		Dispatch cts = Dispatch.call(cb, "Controls").toDispatch();
		int menucount = Dispatch.call(cts, "Count").getInt();
		System.out.println("菜单个数:"+menucount);
		Dispatch ct = Dispatch.call(cts, "Item", new Integer(8)).toDispatch();
//		String menuName = Dispatch.call(ct, "Name").getString();
//		System.out.println("菜单名字:"+menuName);
		Dispatch cm = Dispatch.call(ct, "Controls").toDispatch();
		int gccount = Dispatch.call(cm, "Count").getInt();
		System.out.println("选项个数:"+gccount);
		Dispatch opt = Dispatch.call(cm, "Item", new Integer(5)).toDispatch();
//		String optName = Dispatch.call(opt, "Caption").getString();  //TooltipText
//		System.out.println("选项名字:"+optName);
		Dispatch.call(opt, "Execute");
		Dispatch.call(axc, "SendKeys", "123456", new Variant(true)

猜你喜欢

转载自foreach4.iteye.com/blog/2298624