java调用ole ie_SWT中通过Automatioin的方式访问IE(升级版)

package test;

import org.eclipse.swt.SWTException;

import org.eclipse.swt.internal.ole.win32.COM;

import org.eclipse.swt.internal.ole.win32.GUID;

import org.eclipse.swt.internal.ole.win32.IUnknown;

import org.eclipse.swt.ole.win32.OLE;

import org.eclipse.swt.ole.win32.OleClientSite;

import org.eclipse.swt.widgets.Composite;

/**

*

* @author 杨中科

*

*/

public class AutomationClientSite extends OleClientSite

{

public AutomationClientSite(Composite parent, int style, String progId)

{

super(parent, style);

try

{

appClsid = getClassID(progId);

if (appClsid == null)

OLE.error(OLE.ERROR_INVALID_CLASSID);

//使用CoCreateInstance创建一个进程外Automation服务器

int[] address = new int[1];

int result = COM.CoCreateInstance(getClassID(progId), 0,

COM.CLSCTX_INPROC_SERVER | COM.CLSCTX_LOCAL_SERVER,

COM.IIDIUnknown, address);

if (result != COM.S_OK)

OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result);

objIUnknown = new IUnknown(address[0]);

} catch (SWTException e)

{

dispose();

disposeCOMInterfaces();

throw e;

}

}

protected GUID getClassID(String progId)

{

GUID guid = new GUID();

// create a null terminated array of char

char[] buffer = null;

if (progId != null)

{

int count = progId.length();

buffer = new char[count + 1];

progId.getChars(0, count, buffer, 0);

}

if (COM.CLSIDFromProgID(buffer, guid) != COM.S_OK)

{

int result = COM.CLSIDFromString(buffer, guid);

if (result != COM.S_OK)

OLE.error(result);

}

return guid;

}

}

public class OleUtils

{

public static int getIdOfName(OleAutomation auto, String name)

{

int[] ret = auto.getIDsOfNames(new String[]{name});

return ret[0];

}

public static void setProperty(OleAutomation auto, String name,Variant value)

{

int id = getIdOfName(auto, name);

auto.setProperty(id, new Variant[]{value});

}

public static Variant getProperty(OleAutomation auto, String name)

{

int id = getIdOfName(auto, name);

return auto.getProperty(id);

}

public static Variant invoke(OleAutomation auto, String name)

{

return invoke(auto,name,new Variant[0]);

}

public static Variant invoke(OleAutomation auto, String name,Variant... params)

{

int id = getIdOfName(auto, name);

return auto.invoke(id,params);

}

}

package test;

import org.eclipse.swt.ole.win32.OleAutomation;

import org.eclipse.swt.ole.win32.Variant;

/**

*

* @author 杨中科

*

*/

public class HtmlElement

{

private OleAutomation auto;

public HtmlElement(OleAutomation auto)

{

this.auto = auto;

}

protected OleAutomation getOleAutomation()

{

return auto;

}

public void setProperty(String name, Variant value)

{

OleUtils.setProperty(auto, name, value);

}

public Variant getPropertyAsVariant(String name)

{

Variant value = OleUtils.getProperty(auto, name);

return value;

}

public void setProperty(String name, int value)

{

OleUtils.setProperty(auto, name, new Variant(value));

}

public int getPropertyAsInt(String name)

{

Variant value = OleUtils.getProperty(auto, name);

return value.getInt();

}

public void setProperty(String name, boolean value)

{

OleUtils.setProperty(auto, name, new Variant(value));

}

public boolean getPropertyAsBool(String name)

{

Variant value = OleUtils.getProperty(auto, name);

return value.getBoolean();

}

public void setProperty(String name, String value)

{

OleUtils.setProperty(auto, name, new Variant(value));

}

public String getPropertyAsString(String name)

{

Variant value = OleUtils.getProperty(auto, name);

return value.getString();

}

public HtmlElement getPropertyAsHtmlElement(String name)

{

Variant value = OleUtils.getProperty(auto, name);

return new HtmlElement(value.getAutomation());

}

public Variant invoke(String name,Variant... params)

{

return OleUtils.invoke(auto, name,params);

}

public int invoke_Int(String name,Variant... params)

{

return invoke(name,params).getInt();

}

public boolean invoke_Bool(String name,Variant... params)

{

return invoke(name,params).getBoolean();

}

public String invoke_String(String name,Variant... params)

{

return invoke(name,params).getString();

}

public HtmlElement invoke_HtmlElement(String name,Variant... params)

{

return new HtmlElement(invoke(name,params).getAutomation());

}

}

package test;

import org.eclipse.swt.ole.win32.OleAutomation;

import org.eclipse.swt.ole.win32.OleClientSite;

import org.eclipse.swt.ole.win32.Variant;

/**

* 更多方法参考MSDN“InternetExplorer Object”文档

*

* @author 杨中科

*

*/

public class IEAutomation extends HtmlElement

{

public IEAutomation(OleClientSite clientSite)

{

super(new OleAutomation(clientSite));

}

public void setVisible(boolean value)

{

setProperty("Visible", value);

}

public boolean isVisible()

{

return getPropertyAsBool("Visible");

}

public void setMenuBar(boolean value)

{

setProperty("MenuBar", value);

}

public boolean isMenuBar()

{

return getPropertyAsBool("MenuBar");

}

public void setStatusBar(boolean value)

{

setProperty("StatusBar", value);

}

public boolean isStatusBar()

{

return getPropertyAsBool("StatusBar");

}

public void setToolBar(boolean value)

{

setProperty("ToolBar", value);

}

public boolean isToolBar()

{

return getPropertyAsBool("ToolBar");

}

public int getHWND()

{

return getPropertyAsInt("HWND");

}

public String getReadyState()

{

return getPropertyAsString("ReadyState");

}

public String getLocationURL()

{

return getPropertyAsString("LocationURL");

}

public boolean getBusy()

{

return getPropertyAsBool("Busy");

}

public void navigate(String url)

{

invoke("Navigate", new Variant(url));

}

public HtmlElement getDocument()

{

return getPropertyAsHtmlElement("Document");

}

public String getInnerHtml()

{

HtmlElement document = getPropertyAsHtmlElement("Document");

HtmlElement body = document.getPropertyAsHtmlElement("body");

return body.getPropertyAsString("innerHtml");

}

public HtmlElement getElementById(String id)

{

HtmlElement document = getDocument();

return document.invoke_HtmlElement("getElementById", new Variant(id));

}

public void quit()

{

invoke("Quit");

}

}

使用范例:

protected void 自动填表()

{

//"kw"为关键字输入框

HtmlElement txtKW = ie.getElementById("kw");

//自动填表

txtKW.setProperty("value", "杨中科");

HtmlElement btnSB = ie.getElementById("sb");

//自动点击【百度一下】按钮自动提交查询

btnSB.invoke("click");

}

private void automation() throws Exception

{

OleFrame frame = new OleFrame(composite, SWT.NONE);

AutomationClientSite client =

new AutomationClientSite(frame,SWT.NONE,"InternetExplorer.Application");

ie = new IEAutomation(client);

ie.setVisible(true);

ie.setMenuBar(false);

ie.setToolBar(false);

ie.setStatusBar(false);

int hwnd = ie.getHWND();

OS.SetParent(hwnd, composite.handle);

// 窗口最大化

OS.SendMessage(hwnd, OS.WM_SYSCOMMAND, OS.SC_MAXIMIZE, 0);

OS.SendMessage(hwnd, OS.WM_ACTIVATE, 0, 0);

ie.navigate("http://www.baidu.com");

//等待加载完毕,正确的方式应该是在网页onComplete的时候继续执行,但是没弄明白OLE 的EventSink机制怎么搞到SWT中来

//所以先凑合着Sleep循环检测getBusy()的值,当不busy的时候再进行后续处理

while(ie.getBusy())

{

Thread.sleep(10);

}

msgBox(ie.getInnerHtml());

//"sb"为【百度一下】这个按钮的id

HtmlElement btnSB = ie.getElementById("sb");

//取value属性

String txt = btnSB.getPropertyAsString("value");

msgBox("按钮上的文字:"+txt);

msgBox("网址:"+ie.getLocationURL());

composite.addDisposeListener(new DisposeListener() {

public void widgetDisposed(DisposeEvent e)

{

//必须手动指定退出,否则会报异常

ie.quit();

}

});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Word 添加 OLE 对象,可以使用 Java 的 COM Bridge 组件,通过调用 Word 的 COM 接口实现。 以下是一个示例代码,演示如何在 Word 添加 Excel 表格: ```java import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class WordOLE { public static void main(String[] args) { ActiveXComponent word = new ActiveXComponent("Word.Application"); word.setProperty("Visible", new Variant(true)); Dispatch documents = word.getProperty("Documents").toDispatch(); Dispatch document = Dispatch.call(documents, "Add").toDispatch(); Dispatch selection = Dispatch.get(word, "Selection").toDispatch(); // 添加 OLE 对象 Dispatch shapes = Dispatch.get(selection, "Shapes").toDispatch(); Dispatch oleObject = Dispatch.call(shapes, "AddOLEObject", "Excel.Sheet", "C:\\example\\example.xlsx", false, false, "", 0, "", 0).toDispatch(); // 调整 OLE 对象的位置和大小 Dispatch frame = Dispatch.get(oleObject, "Anchor").toDispatch(); Dispatch.call(frame, "Select"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(frame, "Select"); Dispatch.call(oleObject, "Activate"); // 保存并关闭文档 Dispatch.call(document, "SaveAs", "C:\\example\\example.docx"); Dispatch.call(document, "Close", new Variant(false)); Dispatch.call(word, "Quit"); } } ``` 在这个例子,我们使用了 jacob 库来调用 COM 接口。首先,我们打开 Word 应用程序并创建一个新文档。然后,我们获取当前的选区并在其添加 OLE 对象。接下来,我们使用选区调整 OLE 对象的位置和大小,然后激活它以确保它能够正常工作。最后,我们保存并关闭文档,并退出 Word 应用程序。 请注意,在此过程,我们需要提供 Excel 表格的路径和类型。在本例,我们将路径设置为 "C:\\example\\example.xlsx",并将类型设置为 "Excel.Sheet"。您需要根据您的情况进行更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值