我用的是D2007,可以用中文来命名变量名,下面这些代码大富翁的离线包都有的,我只是做了重新的组合而已:
type
TDataRec = record
通用名: string;
商品名: string;
剂型: string;
规格: string;
包装材质: string;
转换比: string;
质量层次: string;
单位: string;
报价: string;
供应商名称: string;
生产企业名称: string;
end;
TDataArr = array of TDataRec;
function GetHtmlTableCell(aTable: IHTMLTable; aRow, aCol: Integer): IHTMLElement;
var
Row: IHTMLTableRow;
begin
Row := aTable.rows.item(aRow, aRow) as IHTMLTableRow;
Result := Row.cells.item(aCol, aCol) as IHTMLElement;
end;
function TfrmMain.GetDataFromUrl(const URL: string): TDataArr; //获取数据
var
Docintf: IHTMLDocument2;
tblintf: IHTMLTable;
iRow, RowCount, iTalbeNum, iTableCount,DataIndex: Integer;
list: IHTMLElementCollection;
item: IHTMLElement;
begin
bUrlComplete := False;
frmWebBrowser.Navigate(URL);
while not bUrlComplete do
Application.ProcessMessages;
Docintf := frmWebBrowser.Document as IHTMLDocument2;
//获取所有的表
list := Docintf.all.tags('table') as IHTMLElementCollection;
iTableCount := list.length;
DataIndex := 0;
for iTalbeNum := 0 to Pred(iTableCount) do
begin
item := list.item(iTalbeNum, varempty) as IHTMLElement;
if item.getAttribute('id', 0) = 'AutoNumber3' then
begin
tblintf := list.item(iTalbeNum, varempty) as IHTMLTable;
//从表格获取数据
RowCount := tblintf.rows.Length;
for iRow := 0 to RowCount - 1 do
begin
SetLength(Result, DataIndex + 1); //标题行排除
with Result[DataIndex] do
begin
通用名 := Trim(GetHtmlTableCell(tblintf, iRow, 0).innerText);
商品名 := Trim(GetHtmlTableCell(tblintf, iRow, 1).innerText);
剂型 := Trim(GetHtmlTableCell(tblintf, iRow, 2).innerText);
规格 := Trim(GetHtmlTableCell(tblintf, iRow, 3).innerText);
包装材质 := Trim(GetHtmlTableCell(tblintf, iRow, 4).innerText);
转换比 := Trim(GetHtmlTableCell(tblintf, iRow, 5).innerText);
质量层次 := Trim(GetHtmlTableCell(tblintf, iRow, 6).innerText);
单位 := Trim(GetHtmlTableCell(tblintf, iRow, 7).innerText);
报价 := Trim(GetHtmlTableCell(tblintf, iRow, 8).innerText);
供应商名称 := Trim(GetHtmlTableCell(tblintf, iRow, 9).innerText);
生产企业名称 := Trim(GetHtmlTableCell(tblintf, iRow, 10).innerText);
end;
Inc(DataIndex);
end;
end;
end;
end;