如何把网页上的某一数据导入excel中 ( 积分: 50 )

  • 主题发起人 主题发起人 huxhang
  • 开始时间 开始时间
H

huxhang

Unregistered / Unconfirmed
GUEST, unregistred user!
如何把网页上的某一块表格数据导入excel中?
 
如何把网页上的某一块表格数据导入excel中?
 
以前回过一个帖子,找不达到了,代码如下,供参考,只是读取表格内容的代码,导入到Execl的自己写了
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, ExtCtrls, StdCtrls, Buttons, mshtml;
type
TfrmMain = class(TForm)
WebBrowser1: TWebBrowser;
Panel1: TPanel;
memResult: TMemo;
bbtnAnalyse: TBitBtn;
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure WebBrowser1DownloadComplete(Sender: TObject);
private
{ Private declarations }
FFinished : Boolean;
public
{ Public declarations }
end;

var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
FFinished := False;
WebBrowser1.Navigate('http://odds.sports.sina.com.cn/odds/windrawwin.php');
end;

procedure TfrmMain.BitBtn1Click(Sender: TObject);
var
ovTable, ovTableCol, ovTR, ovTRCol, ovTD, ovTDCol: OleVariant;
nTableCount, nTRCount, nTDCount: integer;
st: string;
i, j, k: integer;
strGameList: string;
// 比赛列表
strGameDate: string;
// 比赛时间
function GetGameList(_TableCol: OleVariant): string;
var
Table_, TR_, TD_: OleVariant;
begin
// 获得比赛列表
Table_ := _TableCol.item(1);
TR_ := Table_.all.tags('TR').item(0);
TD_ := TR_.all.tags('TD').item(2);
Result := TD_.InnerText;
end;

function GetGameDate(_TableCol: OleVariant): string;
var
Table_, TR_, TD_: OleVariant;
begin
// 获得比赛列表
Table_ := _TableCol.item(6);
TR_ := Table_.all.tags('TR').item(0);
TD_ := TR_.all.tags('TD').item(0);
Result := TD_.InnerText;
end;
begin
if not FFinished then
Application.MessageBox('正在下载网页,请稍候……', '提示', MB_OK + MB_ICONINFORMATION);
try
ovTableCol := WebBrowser1.OleObject.Document.all.tags('TABLE');
strGameList := GetGameList(ovTableCol);
// 比赛列表
strGameDate := GetGameDate(ovTableCol);
// 比赛列表
nTableCount := ovTableCol.length;
for i := 7 to nTableCount - 1do
begin
ovTable := ovTableCol.item(i);
ovTRCol := ovTable.all.tags('TR');
nTRCount := ovTRCol.length;
for j := 0 to nTRCount - 1do
begin
ovTR := ovTRCol.item(j);
if Pos('比赛时间', ovTR.InnerText) > 0 then
Continue;
ovTDCol := ovTR.all.tags('TD');
nTDCount := ovTDCol.length;
for k := 0 to nTDCount - 1do
begin
ovTD := ovTDCol.item(k);
st := ovTD.InnerText;
if Pos(Trim(st), strGameList) > 0 then
begin
// 表示是联赛头,属于另外启一行
end;
memResult.Lines.Add(IntToStr(i) + ' ' + IntToStr(j) + ' ' + IntToStr(k) + ' ' + st);
//memResult.Lines.Add(ovTD.InnerText);
end;
end;
end;
except
On e: Exceptiondo
Application.MessageBox(PChar(e.Message), '错误', MB_OK + MB_ICONWARNING);
end;
end;

procedure TfrmMain.WebBrowser1DownloadComplete(Sender: TObject);
begin
FFinished := True;
end;
end.

object frmMain: TfrmMain
Left = 220
Top = 77
Width = 802
Height = 586
BorderIcons = [biSystemMenu, biMinimize]
Caption = '新浪球赛分析'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 12
object WebBrowser1: TWebBrowser
Left = 0
Top = 0
Width = 794
Height = 317
Align = alClient
TabOrder = 0
OnDownloadComplete = WebBrowser1DownloadComplete
ControlData = {
4C00000010520000C32000000000000000000000000000000000000000000000
000000004C000000000000000000000001000000E0D057007335CF11AE690800
2B2E126208000000000000004C0000000114020000000000C000000000000046
8000000000000000000000000000000000000000000000000000000000000000
00000000000000000100000000000000000000000000000000000000}
end
object Panel1: TPanel
Left = 0
Top = 317
Width = 794
Height = 242
Align = alBottom
BevelInner = bvRaised
BevelOuter = bvSpace
Caption = 'Panel1'
TabOrder = 1
object memResult: TMemo
Left = 2
Top = 2
Width = 691
Height = 238
Align = alLeft
ImeName = '中文 (简体) - 拼音加加3.11'
ScrollBars = ssBoth
TabOrder = 0
end
object bbtnAnalyse: TBitBtn
Left = 704
Top = 16
Width = 75
Height = 25
Caption = '开始'
TabOrder = 1
end
object BitBtn1: TBitBtn
Left = 724
Top = 84
Width = 75
Height = 25
Caption = 'BitBtn1'
TabOrder = 2
OnClick = BitBtn1Click
end
end
end

转一篇控制Execl的文章
http://www.delphibbs.com/delphibbs/dispq.asp?lid=759711
 
chenybin:谢谢你,你的代码我已收藏,在拜读啊,这是我的问题,能帮忙吗?有分谢
http://bbs.2ccc.com/topic.asp?topicid=187608
 
这个问题而已搞定吗?搞定记得结贴就可以了,你说的链接我会去看的
 
后退
顶部