procedure TForm1.BitBtn3Click(Sender: TObject);
var
Row: Integer;
ExcelApp, ExcelWorkBook, ExcelWorkSheet: Variant;
FileName: string;
begin
if OpenDialog1.Execute then
FileName := OpenDialog1.FileName;
if Trim(FileName)='''' then
Application.MessageBox(''請選擇將要導入的文件!'',''系統提示'',MB_OK+MB_ICONINFORMATION);
ExcelApp := CreateOleObject(''Excel.Application'');
ExcelWorkBook := ExcelApp.WorkBooks.Open(FileName);
ExcelWorkSheet := ExcelApp.WorkSheets.Item[1];
if not ADOQuery1.Active then ADOQuery1.Open;
try
ADOConnection1.BeginTrans;
for Row:=3 to ExcelWorkSheet.UsedRange.Rows.Count do begin
with ADOQuery1 do begin
Append;
FIEldByName(''UserID'').AsString := ExcelWorkSheet.Cells.Item[Row,1];
FIEldByName(''UserName'').AsString := ExcelWorkSheet.Cells.Item[Row,2];
FIEldByName(''PassWord'').AsString := ExcelWorkSheet.Cells.Item[Row,3];
Post;
end;
end;
ADOConnection1.CommitTrans;
Application.MessageBox(''數據導入成功'',''系統提示'',MB_OK+MB_ICONINFORMATION);
except
ADOConnection1.RollbackTrans;
Application.MessageBox(''數據導入失敗!'',''系統提示'',MB_OK+MB_ICONINFORMATION);
end;
end;