我有一个关于将 html 文件加载到 Lazarus 或 FreePascal 中的网格的问题。有可能做到这一点吗?我已经尝试了一些代码,我将包括一些。
首先,我说的是其中包含表格的 html 文件,如下所示:
<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>
我已经尝试将它加载到网格中(使用StringList.Delimiter:=#9;
),但这根本不起作用。我也尝试过使用 Pos,它更接近了,但是使用这段代码,我几乎陷入了一个循环中(我认为是因为当我打开文件时程序没有反应)。那么我能做些什么呢
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;