try
ExcelApp:=CreateOleObject('Excel.Application');
MyWorkBook:=CreateOleobject('Excel.Sheet');
except
application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','',
mb_OK+mb_IconStop);
Exit;
end;
MyworkBook:= ExcelApp.workBooks.Open(opendialog1.FileName);
//打开文件后,对文件进行操作
i := 2;
while true do
begin
j:= MyWorkBook.WorkSheets[1].Cells[i,1].Value;
{上面一行代码就是对单元格进行取值}
try
tmphykh:=inttostr(j);
except
showmessage('can not convert to string, error at the'+inttostr(i)+'line');
exit;
end;
tmpName:= MyWorkBook.WorkSheets[1].Cells[i,2].Value;
hykhlen:= Length(tmphykh);
newcardstr:= MyWorkBook.WorkSheets[1].Cells[i,3].Value;
i := i + 1;
if (tmpName = '') and (tmphykh = '') then
break;
if hykhlen<>0 then
begin
if hykhlen <7 then
begin
k:=7-hykhlen;
tmpstr:='';
for m:=0 to k-1 do
tmpstr:=tmpstr+'0';
tmphykh:=tmpstr+tmpHykh;
end;
hykhlen:=length(newcardstr);
if hykhlen <5 then
begin
k:=5-hykhlen;
tmpstr:='';
for M:=0 to k-1 do
tmpstr:=tmpstr+'0';
newcardstr:=tmpstr+newcardstr;
end;
with adoquery2 do
begin
close;
SQL.Clear;
SQL.Add('select id,hykh,[name] from customer where hykh=:hykhvalue');
Parameters.ParamByName('hykhvalue').Value:= tmphykh;
Open;
prior;
if not eof then
if trim(tmpName) = trim(FieldByName('Name').AsString) then
begin
close;
SQL.Clear;
SQL.Add('update customer set newcard='''+newcardstr+''' where hykh=:hykhvalue');
Parameters.ParamByName('hykhvalue').Value:= tmphykh;
ExecSQL;
num:=num+1;
end; {if}
end; {with}
end; {if}
end; { while}