打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
利用WebBrowser读取网页中表格的数据
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MSHTML, StdCtrls, OleCtrls, SHDocVw;

type
  TForm1
= class(TForm)
    Button1: TButton;
    WebBrowser1: TWebBrowser;
   
procedure FormCreate(Sender: TObject);
   
procedure Button1Click(Sender: TObject);
  private
   
{ Private declarations }
  public
   
{ Public declarations }
 
end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetHtmlTableCell(aTable: IHTMLTable; aRow, aCol: Integer): IHTMLElement;
var
  Row: IHTMLTableRow;

begin
  Result :
= nil;
 
if aTable = nil then Exit;
 
if aTable.rows = nil then Exit;
  Row :
= aTable.rows.item(aRow, aRow) as IHTMLTableRow;
 
if Row = nil then Exit;
  Result :
= Row.cells.item(aCol, aCol) as IHTMLElement;
end;

function GetHtmlTable(aDoc: IHTMLDocument2; aIndex: Integer): IHTMLTable;
var
  list: IHTMLElementCollection;
begin
  Result :
= nil;
 
if aDoc = nil then Exit;
 
if aDoc.all = nil then Exit;
  list :
= aDoc.all.tags('table') as IHTMLElementCollection;
 
if list = nil then Exit;
  Result :
= list.item(aIndex, aIndex) as IHTMLTable;
end;

function GetWebBrowserHtmlTableCellText(const AWebBrowser: TWebBrowser;
 
const TableIndex, RowIndex, ColIndex: Integer;
 
var ResValue: string): Boolean;
var
  Docintf: IHTMLDocument2;
  tblintf: IHTMLTable;
  node: IHTMLElement;
begin
  ResValue :
= ' ';
  docintf :
= AWebBrowser.Document as IHTMLDocument2;
  tblintf :
= GetHtmlTable(docintf, TableIndex);
  node :
= GetHtmlTableCell(tblintf, RowIndex, ColIndex);
  Result :
= node <> nil;
 
if Result then
    ResValue :
= Trim(node.innerText);
end;

function GetHtmlTableRowHtml(aTable: IHTMLTable; aRow: Integer): IHTMLElement;
var
  Row: IHTMLTableRow;
begin
  Result :
= nil;
 
if aTable = nil then Exit;
 
if aTable.rows = nil then Exit;
  Row :
= aTable.rows.item(aRow, aRow) as IHTMLTableRow;
 
if Row = nil then Exit;
  Result :
= Row as IHTMLElement;
end;

function GetWebBrowserHtmlTableCellHtml(const AWebBrowser: TWebBrowser;
 
const TableIndex, RowIndex, ColIndex: Integer;
 
var ResValue: string): Boolean;
var
  Docintf: IHTMLDocument2;
  tblintf: IHTMLTable;
  node: IHTMLElement;
begin
  ResValue :
= ' ';
  docintf :
= AWebBrowser.Document as IHTMLDocument2;
  tblintf :
= GetHtmlTable(docintf, TableIndex);
  node :
= GetHtmlTableCell(tblintf, RowIndex, ColIndex);
  Result :
= node <> nil;
 
if Result then
    ResValue :
= Trim(node.innerHTML);
end;


function GeHtmlTableHtml(aTable: IHTMLTable; aRow: Integer): IHTMLElement;
var
  Row: IHTMLTableRow;
begin
  Result :
= nil;
 
if aTable = nil then Exit;
 
if aTable.rows = nil then Exit;
  Row :
= aTable.rows.item(aRow, aRow) as IHTMLTableRow;
 
if Row = nil then Exit;
  Result :
= Row as IHTMLElement;
end;

function GetWebBrowserHtmlTableHtml(const AWebBrowser: TWebBrowser;
 
const TableIndex, RowIndex: Integer;
 
var ResValue: string): Boolean;
var
  Docintf: IHTMLDocument2;
  tblintf: IHTMLTable;
  node: IHTMLElement;
begin
  ResValue :
= ' ';
  docintf :
= AWebBrowser.Document as IHTMLDocument2;
  tblintf :
= GetHtmlTable(docintf, TableIndex);
  node :
= GeHtmlTableHtml(tblintf, RowIndex);
  Result :
= node <> nil;
 
if Result then
    ResValue :
= node.innerHtml;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Navigate(
'H:\ReadTable\00.html');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ResValue: string;
begin
 
if GetWebBrowserHtmlTableCellText(WebBrowser1, 0, 0, 0, ResValue) then
 
begin
    ShowMessage(ResValue);
 
end else
 
begin
    ShowMessage(
'指定的表格不存在 ');
 
end;
end;

end.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Replace html in active TWebbrowser document (IHTMLTxtRange)
引用 Delphi操作EXCEL 根据别人的组件改写的支持ADO
GZ模板定义名称
可折叠视图
go 简单处理 excel 文件
Go语言操作Mysql语言基础知识
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服