怎样获取html表格中的数据,然后存入数据库!

解决方案 »

  1.   

    表格定义如下:
    <table>
    <tr><td>内容1</td></tr>
    <tr><td>内容2</td></tr>
    </table>只要表格字符串按<td></td>分组出来就可以了
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      WebBrowser1.Navigate('about:blank');
      while WebBrowser1.ReadyState < READYSTATE_INTERACTIVE do
        Application.ProcessMessages;
      WebBrowser1.OleObject.Document.body.innerHTML := Memo1.Text;
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      i, j: integer;
      ovTable: OleVariant;
    begin
      //HTML中的第一個Table
      ovTable := WebBrowser1.OleObject.Document.all.tags('TABLE').item(0);
      StringGrid1.RowCount := ovTable.Rows.Length ;
      StringGrid1.ColCount := ovTable.Rows.Item(i).Cells.Length;  for i := 0 to (ovTable.Rows.Length - 1) do
      begin
        for j := 0 to (ovTable.Rows.Item(i).Cells.Length - 1) do
        begin
          StringGrid1.Cells[j,i] := ovTable.Rows.Item(i).Cells.Item(j).InnerText;
        end;
      end;
    end;(注:先把上述网页Html Source放入Memo1.Text、
       再準備一個TWebBrowser和TStringGrid控件。
        Memo1.Text的内容 -> TWebBrowser -> TStringGrid
      此例只取了第一個Table的内容...)
      

  3.   

    "怎样获取html表格中的数据,然后存入数据库"我还以为你想取html表格中的数据,然后再一一对应存入数据库的表中。
    因此给出一个读取html表格中的数据的方法...你若想直接取到数据集中,
    可先把html转换成xml,然后用TClientDataSet控件直接连接xml...
      

  4.   

    HTML 如果不规则,夹杂了别的东西,还是用上面那个方法好些就是cronuz(cronus) 兄弟的这个代码
    //HTML中的第一個Table
      ovTable := WebBrowser1.OleObject.Document.all.tags('TABLE').item(0);
      StringGrid1.RowCount := ovTable.Rows.Length ;
      StringGrid1.ColCount := ovTable.Rows.Item(i).Cells.Length;  for i := 0 to (ovTable.Rows.Length - 1) do
      begin
        for j := 0 to (ovTable.Rows.Item(i).Cells.Length - 1) do
        begin
          StringGrid1.Cells[j,i] := ovTable.Rows.Item(i).Cells.Item(j).InnerText;
        end;
      end;如果很规范,比如是一张表,就可以存成XML格式,ADO和ClientDataSet都支持LoadFromFile
    获得记录集
      

  5.   

    同意cronuz(cronus) 
    也可以先取到数据集中再存入数据库
    procedure TForm1.Button3Click(Sender: TObject);
    var
      i, j: integer;
      ovTable: OleVariant;
      tempStr: string;
    begin
      ovTable := WebBrowser1.OleObject.Document.all.tags('TABLE').item(0);
      for i := 0 to (ovTable.Rows.Length - 1) do
      begin
        for j := 0 to (ovTable.Rows.Item(i).Cells.Length - 1) do
        begin      
        ADoquery1.Edit;
        DBgrid1.Fields[j+1].Value := ovTable.Rows.Item(i).Cells.Item(j).InnerText;
        Adoquery1.Post;
        end;
      end;
    end;
      

  6.   

    楼主:你的大鬼qiluping怎麼又進此貼了。
       難道你還不知自己己上”光栄榜”↓了嗎?http://community.csdn.net/Expert/topic/3071/3071709.xml?temp=.253689   難道真要一不做、二不休? 你会玩”死”自己的。
       我可叫你給害残了、被破拿了”水分”。 hehe~~~