我现在有多个EXCEL,一个SQL的表,表结构完全一致。EXCEL和SQL表都有一个“唯一标示”的字段。
现在想把EXCEL导入SQL中,要求
1、SQL中如果已有“唯一标示”相同的记录,则不导入EXCEL中的该记录。
2、最好有进度条显示,因为的记录非常多,几十万条记录呢
3、我已经知道逐条对比导入的方法,速度太慢。
4、查了一点资料'insert into data select * from ' +
    'OpenDataSource(''Microsoft.Jet.OLEDB.4.0'',''Data Source="'+filename+'";Extended Properties="Excel 5.0;HDR=Yes;";Persist Security Info=False'')...sheet1$' ;这个无法实现条件过滤导入。
请各位帮帮忙。

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      excelx,excely,sqlStr:string;
      StrValue:array [1..24] of string;
      tmpStr:integer;
      ExcelApp,WorkBook:Variant;
      i,j,ExcelRowCount:integer;
    begin 
      if Trim(ExcelPath)='' then
      begin
        MessageBox(handle,'没有选中Excel文件!','警告信息',MB_Ok or MB_ICONWARNING);
        Exit;
      end;
      try
        ExcelApp := CreateOleObject('Excel.Application');
        WorkBook := ExcelApp.WorkBooks.Open(ExcelPath);//使用opendialog对话框指定
        //excel档路径
        ExcelApp.Visible := false;
        ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
        ProgressBar1.Max := ExcelRowCount - 1;
        for i := 2 to ExcelRowCount do
        //第一行为字段名信息。不取。
        begin
          ProgressBar1.Position := i ;
          label2.Caption := '总共 '+IntToStr(ProgressBar1.Max) + '条记录,正在导入第'+ IntToStr(i-1)+'条记录';
          application.ProcessMessages ;
          //excelx := excelapp.Cells[i,1].Value;
          //excely := excelapp.Cells[i,2].Value;
          for j:=1 to 24 do
            StrValue[j]:= excelapp.Cells[i,j].Value;
          if Trim(StrValue[12])='是' then  StrValue[12]:='1' else StrValue[12]:= '0';
          if Trim(StrValue[13])='是' then  StrValue[13]:='1' else StrValue[13]:= '0';
          if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then
          //指定excel档的第 i 行 ,第 1,2(看情况而定)行如果为空就退出,这样的设定,最好是你的档案里这两行对应数据库中不能为空的数据
            Exit
          else
          begin
            with ADOQuery2 do //插入 t_zbclass[账本类别] 表
            begin
              close;
              sql.clear;
              sqlStr:= 'select * from t_zbclass where FName='+QuotedStr(StrValue[8]);
              sql.add(sqlStr);
              open;
            end;
          end;
        end;
        label2.Caption := '总共 '+IntToStr(ProgressBar1.Max) + '条记录导入完成';
        ProgressBar1.Position :=ProgressBar1.Max;
      finally
      WorkBook.Close;
      ExcelApp.Quit;
      ExcelApp := Unassigned;
      WorkBook := Unassigned;
    end;
      

  2.   


    --以下为:将excel表中和data表不重复的数据导入data表
    insert into data 
    select * from 
      OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="Data Source";Extended Properties="Excel 5.0;HDR=Yes;";Persist Security Info=False'')...sheet1$ A
    where not exists(select 1 from data where id = A.id) 
      

  3.   


    /*
    --晕,标点没弄对
    --总之,后在查出excel表中数据后 
    给sheet1$加个别名,如: A
    加入这句 where not exists(select 1 from data where id = A.id)
    */
    insert into data 
    select * from 
      OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="Data Source";Extended Properties="Excel 5.0;HDR=Yes;";Persist Security Info=False'')'...sheet1$ A
    where not exists(select 1 from data where id = A.id) 
      

  4.   

    给表加唯一约束
    然后insert into
      

  5.   

    终于根据simonhehe的提示完成条件过滤导入,感谢啊!!
    一开始老出错,无法获取表名,后来才发现是他们给的EXCEL版本太低,居然是EXCEL2.1。真晕啊!!
    现将代码放出,给有需要的朋友使用。
    Var
      sFileName, sTableName : String;
      sl : TStringList;
      sql:string;
    begin
    try
      //首先获取表名
      sl:=tstringlist.Create;
      con1.Provider:='Microsoft.Jet.OLEDB.4.0';
      con1.ConnectionString:='Data Source='+ FileName+';Extended Properties=''Excel 8.0;HDR=no'';';
      con1.GetTableNames(sl);
      con1.Connected:=false;
    except
      sl.free;
      exit;
    end;
    try
      sql := 'insert into data select * from ' +
        'OpenDataSource(''Microsoft.Jet.OLEDB.4.0'',''Data Source="'+filename+
        '";Extended Properties="Excel 5.0;HDR=Yes;";Persist Security Info=False'')...['+sl[0]+']'
        +' A '+
        'where not exists(select 1 from data where 唯一标识 = A.唯一标识)';
      Form1.con1.Execute(sql);
      sl.Free;
      con1.Connected:=false;
      ShowMessage('导入数据成功');
    except
      ShowMessage('导入数据发生错误!');
    end;