如何使用webbrowser实现html表格操作

2025-04-30 00:25:28
推荐回答(2个)
回答1:

使用webbrowser实现html表格操作需要引入mshtml.dll这个链接库。
1、比如有如下表格代码:










序号 违章时间 违章地点 违章行为 执行机关 是否处理

2、用mshtml.dll提供的方法解析:
IHTMLDocument2 document = webbrower.Document.DomDocument as IHTMLDocument2;
IHTMLElementCollection tables = (IHTMLElementCollection)document.all.tags("TABLE");
foreach (IHTMLTable table in tables)
{
IHTMLTableSection tts= table.tHead;//因为TH标签是属于表格的表头信息;
foreach(IHTMLElement th in tts.rows)
{
System.Diagnostics.Debug.WriteLine(th.innerText)//遍历输出内容
}
}

回答2:

调用JS方法
HTMLWindow2 win = (IHTMLWindow2)webBrowser.Document.Window.DomWindow;
win.execScript("", "Javascript");

function DoAdd(a, b) {
return a + b;
}
object i= webBrowser.Document.InvokeScript("DoAdd", new object[] { 1, 2 });
int sum = Convert.ToInt32(i);