我以前做过,给你一段代码,希望能给你带来点启示。
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
i,j:integer;
aa,bb:string;
Myarray:array[1..100,1..2]of integer;
begin
if ListBox1.ItemIndex>0 then
ListBox1.ItemIndex:=ListBox1.ItemIndex-1;
label3.Caption:=inttostr(ListBox1.ItemIndex);
//建立数据导入的联结关系,得到字段对应数组。
//array〔i,1〕 记录目的字段;array〔i,2〕记录EXCEl中的列数。
for i:=0 to listbox1.Items.Count-1 do begin
myarray[i+1,1]:=i+1;
for j:=1 to ExcelWorksheet1.Cells.Columns.Count do begin
aa:=ExcelWorksheet1.Cells.item[1,j];
if trim(aa)='' then break;
if listbox2.Items=aa then
myarray[i+1,2]:=j;
end;
end;
//数据导入
i:=1;
query1.Active:=true;
query1.Edit;
while trim(ExcelWorksheet1.Cells.item[i+1,1])<>'' do begin
aa:=ExcelWorksheet1.Cells.item[i+1,1];
if query1.Locate('ID',aa,[loCaseInsensitive])then begin
query1.edit;
for j:=1 to listbox1.Items.Count do begin
if myarray[j,2]<>0 then
Query1.Fields[j-1].Value:=ExcelWorksheet1.Cells.item[i+1,myarray[j,2]];
end;
// showmessage('有重复记录。')
end else begin
query1.edit;
query1.insert;
for j:=1 to listbox1.Items.Count do begin
if myarray[j,2]<>0 then
Query1.Fields[j-1].Value:=ExcelWorksheet1.Cells.item[i+1,myarray[j,2]];
end;
end;
Label3.caption:=inttostr(i);
i:=i+1;
end;
query1.post;
showmessage('共导入'+inttostr(i-1)+'条员工信息。');
end;