获取表格元素内容
获取表格元素内容 收藏
view plaincopy to clipboardprint?
<table id="Test_Table">
<thead>
<tr>
<th><span>姓名</span></th>
<th><span>性别</span></th>
</tr>
</thead>
<tbody>
<tr>
<td>ZDZ</td>
<td>男</td>
</tr>
</tbody>
</table>
<table id="Test_Table">
<thead>
<tr>
<th><span>姓名</span></th>
<th><span>性别</span></th>
</tr>
</thead>
<tbody>
<tr>
<td>ZDZ</td>
<td>男</td>
</tr>
</tbody>
</table>
表格内容如上,第一行是 姓名, 性别,第二行是zdz , 男,
现要获得第二行第二列元素的内容,即"男",接口顺序依次是
IHTMLTable ---> IHTMLTableRow ---> IHTMLElement
代码如下:
view plaincopy to clipboardprint?
CString getInfo()
{
// TODO: 在此添加控件通知处理程序代码
CString strInfo;
HRESULT hr;
IDispatch *pDisp;
pDisp = m_webBrowser.get_Document(); // 获取webbrowser控件文档接口
IHTMLDocument2* pDoc;
hr = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDoc ); // 获取操作界面的文档接口
IHTMLElementCollection* pColl = NULL;
hr = pDoc->get_all( &pColl ); // 获取网页元素集合
VARIANT index;
VARIANT varID;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
varID = StringToVariant("Test_Table"); // 表格元素ID
hr = pColl->item(varID, index, &pDisp); // 获得表格位置
if ( (hr == S_OK) && (pDisp != NULL) )
{
IHTMLTable * pTable; // 获得表格元素接口
hr = pDisp->QueryInterface(IID_IHTMLTable,(void **)&pTable );
if ( (hr == S_OK) && (pTable != NULL) )
{
IHTMLElementCollection* pColl2 = NULL;
pTable->get_rows(&pColl2); // 获取表格行
IDispatch* pDisp2;
V_I4(&index) = 1;
hr = pColl2->item( index,index, &pDisp2 ); // 获取第2行位置
if ( (hr == S_OK) && (pDisp2 != NULL) )
{
IHTMLTableRow* pRow; // 获取行元素接口
hr = pDisp2->QueryInterface(IID_IHTMLTableRow,(void **)&pRow);
if( (hr == S_OK) && (pRow != NULL) )
{
IHTMLElementCollection* pColl3 = NULL;
pRow->get_cells(&pColl3); // 获取格子元素
IDispatch* pDisp3;
V_I4(&index) = 1;
hr = pColl3->item( index,index, &pDisp3 ); // 获取第2行第2格元素
if ( (hr == S_OK) && (pDisp2 != NULL) )
{
IHTMLElement* pElem; // 获取元素接口
hr = pDisp3->QueryInterface(IID_IHTMLElement,(void **)&pElem);
if( hr == S_OK )
{
BSTR bstr = SysAllocString(L"");
pElem->get_innerText(&bstr); // 获取该表格元素的文本信息
strInfo = bstr;
pElem->Release();
}
pDisp3->Release();
}
pRow->Release();
}
pDisp2->Release();
}
pTable->Release();
}
pDisp->Release();
}
pDoc->Release();
pColl->Release();
return strInfo;
}
CString getInfo()
{
// TODO: 在此添加控件通知处理程序代码
CString strInfo;
HRESULT hr;
IDispatch *pDisp;
pDisp = m_webBrowser.get_Document(); // 获取webbrowser控件文档接口
IHTMLDocument2* pDoc;
hr = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDoc ); // 获取操作界面的文档接口
IHTMLElementCollection* pColl = NULL;
hr = pDoc->get_all( &pColl ); // 获取网页元素集合
VARIANT index;
VARIANT varID;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
varID = StringToVariant("Test_Table"); // 表格元素ID
hr = pColl->item(varID, index, &pDisp); // 获得表格位置
if ( (hr == S_OK) && (pDisp != NULL) )
{
IHTMLTable * pTable; // 获得表格元素接口
hr = pDisp->QueryInterface(IID_IHTMLTable,(void **)&pTable );
if ( (hr == S_OK) && (pTable != NULL) )
{
IHTMLElementCollection* pColl2 = NULL;
pTable->get_rows(&pColl2); // 获取表格行
IDispatch* pDisp2;
V_I4(&index) = 1;
hr = pColl2->item( index,index, &pDisp2 ); // 获取第2行位置
if ( (hr == S_OK) && (pDisp2 != NULL) )
{
IHTMLTableRow* pRow; // 获取行元素接口
hr = pDisp2->QueryInterface(IID_IHTMLTableRow,(void **)&pRow);
if( (hr == S_OK) && (pRow != NULL) )
{
IHTMLElementCollection* pColl3 = NULL;
pRow->get_cells(&pColl3); // 获取格子元素
IDispatch* pDisp3;
V_I4(&index) = 1;
hr = pColl3->item( index,index, &pDisp3 ); // 获取第2行第2格元素
if ( (hr == S_OK) && (pDisp2 != NULL) )
{
IHTMLElement* pElem; // 获取元素接口
hr = pDisp3->QueryInterface(IID_IHTMLElement,(void **)&pElem);
if( hr == S_OK )
{
BSTR bstr = SysAllocString(L"");
pElem->get_innerText(&bstr); // 获取该表格元素的文本信息
strInfo = bstr;
pElem->Release();
}
pDisp3->Release();
}
pRow->Release();
}
pDisp2->Release();
}
pTable->Release();
}
pDisp->Release();
}
pDoc->Release();
pColl->Release();
return strInfo;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LightBoat09/archive/2010/08/21/5826381.aspx