请问怎么将EXCEL中的数据导入到SQL的数据表中?

解决方案 »

  1.   

    给你一个控制Excel的例子,你一行行读出来写到SQL里面去
    procedure TfrmSG.Button1Click(Sender: TObject);  //uses Excel2000 , ComObj
    var
      pc_i:integer;
      xlapp,sheet: variant;
      WBK: OleVariant;
    begin
      xlapp := createoleobject('excel.application');
      xlapp.visible := True;      //Excel是否可见
      if Trim(Edit1.Text)='' then
      begin
        Application.MessageBox('文件名不能为空','警告',MB_OK);
        Exit;
      end;
      WBK := xlapp.workbooks.Open(Edit1.Text);
      sheet := xlapp.workbooks[1].worksheets['sheet1'];
      sheet.Cells.SpecialCells(XlCellTypeLastCell,EmptyParam).Activate;  //showmessage(IntToStr(xlapp.ActiveCell.Row));
      //showmessage(IntToStr(xlapp.ActiveCell.Column));
      //excelworksheet2.UsedRange[1].Rows.Count));
      //excelworksheet2.UsedRange[1].Columns.Count));
      pc_i:=1;
      Edit2.Text := sheet.cells[pc_i,1];     //写入的话用 sheet.cells[pc_i,1] := Edit2.Tex;
      WBK.Close(SaveChanges := True);
      xlapp.Quit;
    end;
      

  2.   

    那你怎么来打开存在的EXCEL呢
      

  3.   

    //参考如下代码~~
    //可以处理成SQL插入语句~~
    var
      vTemplate: OleVariant;
      vCell: OleVariant;
      vCol, vRow: Integer;
    begin
      with TExcelApplication.Create(nil) do try
        Connect;
        Visible[0] := True;
        vTemplate := 'c:\temp\temp.xls';
        Workbooks.Add(vTemplate, 0);
        for vCol := 1 to 3 do
          for vRow := 1 to 3 do begin
            vCell := Chr(Ord('A') + vCol - 1) + IntToStr(vRow);
            StringGrid1.Cells[vCol, vRow] := Range[vCell, vCell].Value;
          end;
        Disconnect;
      finally
        Free;
      end;
    end;