Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Downloading url using delphi 2

Status
Not open for further replies.

delfy

MIS
Feb 8, 2005
96
JM
Is there a way to download a url using delphi as excel file?? i know i can use the TDownLoadURL component but how do i specify that i want in it in an excel file format
 
I don't know what version of Delphi you are using, but in Delphi 6 you can do the following:

1) Drop a TRichEdit, a TButton and a TIdHTTP (found in the Indy Clients tab of the Delphi 6 component palette) onto a form.

2) Write a procedure such as the following, where HTTP1 is your TIdHTTP component and reHTML is your TRichEdit:
Code:
procedure TForm1.GetHTMLContent(URL: String);
var
  PostDataStream : TStringStream;
begin
  PostDataStream := TStringStream.Create('');
  try
    HTTP1.Get(URL, PostDataStream);
    reHTML.Text := PostDataStream.DataString;
  finally
    PostDataStream.Free;
  end;
end;

3) Then call your function passing the appropriate URL, for example from a TButton OnClick event:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  GetHTMLContent('[URL unfurl="true"]http://www.jamstockex.com/LASTQUOT.HTM');[/URL]
end;

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I had nothing to do for a moment ago...

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  MS: TMemoryStream;
  TS: TStrings;
  SaveFrom, SaveTo, i: Integer;
begin
  TS := TStringList.Create;
  MS := TMemoryStream.Create;
  try
    // Get data
    IdHTTP1.Get('[URL unfurl="true"]http://www.jamstockex.com/LASTQUOT.HTM',[/URL] MS);
    MS.Position := 0;
    TS.LoadFromStream(MS);

    // Erase all beginning stuff
    SaveFrom := -1;
    SaveTo := -1;

    for i := 0 to TS.Count -1 do
    begin
      if (SaveFrom = -1) and (Pos('ORDINARY SHARES', TS[i]) <> 0) then
        SaveFrom := i+1;

      if (SaveTo = -1) and (Pos('PREFERENCE SHARES', TS[i]) <> 0) then
        SaveTo := i-1;
    end;

    // Check if valid span
    if (SaveFrom = -1) or (SaveTo = -1) then
    begin
      ShowMessage('Error decode page!');
      Exit;
    end;

    // Decode page
    for i := SaveFrom to SaveTo do
    begin
      memo1.lines.add( Copy(TS[i], 17, 27) + Copy(TS[i], 105, 7) );
    end;
  finally
    MS.Free;
    TS.Free;
  end;
end;

KungTure-RX.jpg

//Nordlund
 
What I neglected to mention was that my code simply retrieved the HTML source code. After doing this, you need to parse the HTML picking out the relevant data - as Nordlund has shown.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
You can get a neater look in a TMemo or TRichEdit by setting the font to one that contains fixed-width characters e.g. Courier. Then declare two variables and replace the bottom section of Nordlund's code with the second code segment below:
Code:
var
  security, lastSale: String;
Code:
...
// Decode page
for i := SaveFrom to SaveTo do
begin
  security := Trim(Copy(TS[i], 17, 27));
  lastSale := Trim(Copy(TS[i], 105, 7));
  memo1.lines.add(Format('%-20s %8s', [security, lastSale]));
end;
...

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top