Link to home
Start Free TrialLog in
Avatar of MissManal
MissManal

asked on

HTML Table To StringGRID?

Hi,

I have an HTML file which contains only one HTML table. I need to extract this html table to TStringGrid Component. Can anyone help me with this task?
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

hum. CSV file is much much much (and maybe another much) more easier to handle than HTML table.

For once, there could be a lot of HTML garbage around your table, and other tags inside the cells. And I will not speak about nested tables which would complicate the task.

So, first, I will assume you can isolate the HTML fragment containing ONLY the <TABLE>...</TABLE> text in a string.
Please provide such a sample so that we can test, in the meantime I will code a procedure that fills the stringlist :

Procedure FillStringGridWithHTMLTable(aStrGrid:TStringGrid;HTMLTable:String);
begin
...
end;
Avatar of MissManal
MissManal

ASKER

I can't convert the html table to csv so please is there anyway to extract the html table from the html page and then convert it to stringgrid table?
and here it is
Procedure FillStringGridWithHTMLTable(aStrGrid:TStringGrid;HTMLTable:String);
Var
 P,TagStart:Integer;
 Function GetNextTag(Var Tag:String):Boolean;
 Var
  P2:Integer;
 begin
  Result:=False;
  Repeat
   P:=PosEx('<',HTMLTable,P);
   if P<1 then Exit;
   TagStart:=P;
   P2:=PosEx('>',HTMLTable,P);
   if P2<1 then Exit;
   Tag:=Trim(Copy(HTMLTable,P+1,P2-P-1));
   P:=P2+1;
   P2:=Pos(' ',Tag);
   if P2>0 then Tag:=Copy(Tag,1,P2-1);
   Tag:=UpperCase(Tag);
   Result:=(Tag='TR') Or (Tag='/TR') Or (Tag='TD') Or (Tag='/TD') Or (Tag='/TABLE');
  Until Result;
 end;
 procedure AddCell(L,C:Integer;Text:String);
 begin
  if L>=aStrGrid.RowCount then aStrGrid.RowCount:=aStrGrid.RowCount+1;
  if C>=aStrGrid.ColCount then
   begin
    aStrGrid.ColCount:=aStrGrid.ColCount+1;
    aStrGrid.Cells[aStrGrid.ColCount-1,0]:='Col'+IntToStr(aStrGrid.ColCount);
   end;
  try
   aStrGrid.Cells[C,L]:=Text;
  except
   // should not happen
  end;
 end;

Var
 L,C,CellStart:Integer;
 Tag:String;
begin
 P:=1;
 L:=1;
 C:=0;
 aStrGrid.RowCount:=2;
 aStrGrid.ColCount:=1;
 aStrGrid.Cells[0,0]:='Col1';
 while GetNextTag(Tag) do
  begin
   if Tag='TR' then C:=0;
   if Tag='/TR' then Inc(L);
   if Tag='TD' then CellStart:=P;
   if Tag='/TD' then
    begin
     AddCell(L,C,Copy(HTMLTable,CellStart,TagStart-CellStart));
     Inc(C);
    end;
   if Tag='/TABLE' then Exit;
  end;
end;

Open in new window

Please give us the link of the source since you removed it.
I tried the code but it seems not working as i expected.

What I need is a function that extract a table from html file To stringgrid. The table size is variance and it different from html page to page.

For example: the code should extract the table from her
http://news.bbc.co.uk/sport1/hi/football/eng_prem/table/default.stm


and can also extract it from her too
http://web.wi.mit.edu/young/expression/table.html

and also her
http://www.independent.co.uk/news/education/schools/gcse-results-comprehensive-school-results-table-2060948.html

So the code should works in any Table size and it should automatically ignore the non-table html code and only extract the table.
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)