微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Lazarus/Free Pascal 的 Grid、StringGrid 或 DBGrid 中打开 html 文件?

如何解决如何在 Lazarus/Free Pascal 的 Grid、StringGrid 或 DBGrid 中打开 html 文件?

我有一个关于在 Lazarus 或 FreePascal 中将 html 文件加载到网格中的问题。甚至有可能这样做吗?我已经尝试了一些代码,我会包含一些。

首先,我说的是带有表格的 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>'],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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。