Reputation: 31
I have a question about loading html files into a grid in Lazarus or FreePascal. Is it possible to even do that? I've tried some pieces of code, I'll include some.
First, I'm talking about the kind of html files with a table in it, like this:
<html><head><title>201401061209</title></head><body><table width=451
border=0 cellspacing=0 cellpadding=0 class="realtable">
<tr class="trcolor">
<th></th>
<th align="left" width=40>Di</th>
<th align="left" width=40>Wo</th>
<th align="left" width=40>Do</th>
<th align="left" width=40>Vr</th>
<th align="left" width=40>Za</th>
<th align="left" width=40>Zo</th>
</tr>
<tr>
<td>Zonneschijn (%)</td>
<td align="left" width=40> 30</td>
<td align="left" width=40>20</td>
<td align="left" width=40>10</td>
<td align="left" width=40>10</td>
<td align="left" width=40>10</td>
<td align="left" width=40>10</td>
</tr>
<tr>
<td>Neerslagkans (%)</td>
<td align="left" width=40> 30</td>
<td align="left" width=40>50</td>
<td align="left" width=40>80</td>
<td align="left" width=40>40</td>
<td align="left" width=40>20</td>
<td align="left" width=40>30</td>
</tr>
</table></body></html>
I've already tried to load it into a grid (using StringList.Delimiter:=#9;
) but that didn't work at all. I've tried to use Pos too, which came closer but with this code I'm litterally stuck in a loop (I think because the program doesn't react when I've opened a file). So what can I do to
procedure HtmlToGrid(Grid: TStringGrid; const FileName: string;
Sender: TObject);
var
StringList, Line: TStringList;
Row, Col: Integer;
i, positie1, positie2: Integer;
tekst, gekniptetekst: string;
einddoc: boolean;
begin
Grid.RowCount := 0; //clear any previous data
StringList := TStringList.Create;
StringList.LoadFromFile(filename);
Grid.RowCount := Stringlist.Count;
try
Line := TStringList.Create;
try
einddoc:=False;
while einddoc=False do begin
positie1:= Pos('<tr ', StringList.Text);
positie2:= Pos('</tr>', StringList.Text);
for Row := 0 to StringList.Count-1 do begin //voor elke rij
tekst:=StringList.Strings[Row];
//Line[Row]:= GetPart(['<td align="left" width=40>'],['</td>'],tekst);
if positie1 > 0 then begin
//positie1:= positie1 + 26;
if positie2 > 0 then begin //als beide posities bestaan
//einderij:=False;
for Col := 0 to Grid.ColCount-1 do begin //voor elke kolom
gekniptetekst:= GetPart(['<td align="left"
width=40>'],['</td>'],tekst);
if Col<Line.Count then
Grid.Cells[Col,Row]:= gekniptetekst
else
Grid.Cells[Col,Row]:= '';
end;
end;
end;
end;
if Pos('</table>', StringList.Text)-30<positie2 then
einddoc:=True;
end;
finally
Line.Free;
end;
finally
StringList.Free;
end;
{prob := Pos('<th', StringList.Text);
if (Sender is TLabel) then
TLabel(Sender).Caption := IntToStr(prob);
}
end;
Upvotes: 0
Views: 503