con1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+MyExcelFile+';Extended Properties=excel 8.0;Persist Security Info=False';
con1.Connected:=true;
qry1.Close;
qry1.SQL.Clear;
qry1.SQL.Add('SELECT *  FROM [sheet1$]');
qry1.Open;
pb1.Max:=qry1.RecordCount;
lbl1.Caption:='0';
lbl2.Caption:='0';
for i:=1 to qry1.RecordCount   do
begin
Application.ProcessMessages;
self.refresh;
qry2.Close;
qry2.SQL.Clear;
qry2.SQL.Add('select *  from   aa ');
qry2.Open;
//if   ADOQuery2.FieldByName('csbh').AsString=''   then
                        //begin
lbl1.Caption:=IntToStr(StrTOint(lbl1.caption)+1);
                      //   ShowMessage(IntToStr(ADOQuery2.RecordCount));   
qry2.Close;
qry2.SQL.Clear;
qry2.SQL.Add('insert   into   aa(q1,q2,ddate,flag)');
qry2.SQL.Add('Values('+QuotedStr(qry1.fieldbyName('q1).AsString)+','+QuotedStr(qry1.fieldbyName('q2').AsString)+',getdate(),0)');
                      //   ShowMessage(ADOQuery2.SQL.text);
try
qry2.ExecSQL;
except
end;
Application.ProcessMessages;
qry1.Next;
pb1.Position   :=i;
end;
qry2.Active:=false;
qry2.Close;
qry2.SQL.Add('select * from aa);
qry2.Open;
qry2.Active:=true;
Application.MessageBox('导入完毕!','提示',MB_OK+MB_Iconinformation);
导入数据库后多一条,最后一条重复导入,帮忙分析问题出在哪里?
还有就是打开opendialog选择excel时,选择取消,会提示“无效的参数量”怎么解决?还有,导入后,对比另外一张表bb,通过导入的内容更新bb表中的q2列值,如何实现更新的同时,在dbgrid中动态逐条显示“修改成功”?

解决方案 »

  1.   

    还有就是打开opendialog选择excel时,选择取消,会提示“无效的参数量”怎么解决?
    这样解决:
    if opendialog1.excute then
    begin这里导入的代码
    end else 
    Exit;
      

  2.   

    建议楼主这样试试:
    将EXCEL文件的所需要的列读取到dataset中,将DATASET设置为启用缓存,而DATASET的数据源设置成楼主要写入的表名称。
      例:select F1,F2,F3 from TableName1 where 1=2那么保存的时候 dataset1.updataBatch(arall)即可;这样操作很方便,只要控制好从EXCEL读取数据那步就行了。