procedure TForm1.Button1Click(Sender: TObject);
const
BeginRow = 3; BeginCol = 1;
var
Excel: OleVariant;
iRow,iCol : integer;
AUnit: string;
AValue: integer;
begin
if not OpenDialog1.Execute then exit;
try
Excel := CreateOLEObject('Excel.Application');
except
Application.MessageBox('Excel没有安装!', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
Exit;
end;
Excel.Visible := false;
Excel.WorkBooks.Open(OpenDialog1.FileName);
with dm.DataBase1 do begin
if Not InTransaction then StartTransaction ;
end;
try
iRow := BeginRow;
iCol := BeginCol;
while trim(Excel.WorkSheets[1].Cells[iRow,iCol].value) <> '' do begin
with Query1 do begin
Append;
Fields[0].AsString := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value);
Fields[1].AsString := trim(Excel.WorkSheets[1].Cells[iRow,iCol+1].value);
Fields[2].Asstring := trim(Excel.WorkSheets[1].Cells[iRow,iCol+2].value);
Fields[3].Asstring := trim(Excel.WorkSheets[1].Cells[iRow,iCol+3].value);
Fields[4].AsString := trim(Excel.WorkSheets[1].Cells[iRow,iCol+4].value);
iRow := iRow + 1;
iCol := BeginCol;
// Application.ProcessMessages;
end;
end;
Excel.Quit;
Query1.ApplyUpdates;
dm.DataBase1.Commit;
except
Application.MessageBox('导入数据出错!请检查文件的格式是否正确!', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
Excel.Quit;
dm.DataBase1.Rollback;
raise;
end;
Query1.CommitUpdates;
Query1.open;
end;