uses comobj,excel97,Excel2000;
//從Excel寫數據到Access庫
prodedure ExcelToMdb(EXLfile:string;);
var
sheet,XLApp,workbook : variant;
iRow,MaxRow:integer;
begin
screen.Cursor:=crHourGlass;
try
//創建對象
XLApp:=createOleObject('Excel.Application');
XLApp.displayAlerts:=false;
XLApp.ScreenUpdating:=false;
XLApp.WorkBooks.Add(EXLfile);
workbook := XLApp.workbooks[1];
sheet:=workbook.worksheets[1];
//sheet:=XLApp.WorkBooks[1].worksheets[1];
//取得最大行數 maxRow
XLApp.ActiveCell.SpecialCells(xlLastCell).Select;
maxRow:=XLApp.ActiveCell.Row; //最大行數
//寫數據到Access庫
ADOTable1.open;
for iRow:=2 to MaxRow do
if sheet.cells[iRow,1]<>'' then //關鍵字不為空
begin
ADOTable1.Append ;
ADOTable1.fIEldByName('ID').asInteger:=
strToInt(sheet.cells[iRow,1]);
ADOTable1.fIEldByName('code').asString:=sheet.cells[iRow,2]; //編碼
ADOTable1.fIEldByName('name').asString:=sheet.cells[iRow,3]; //名稱
ADOTable1.post;
end;
finally
if not VarIsEmpty(XLApp) then begin //釋放對象
XLApp.displayAlerts:=false;
XLApp.ScreenUpdating:=true;
XLApp.quit;
end;
screen.Cursor:=crDefault;
end;
end;
//=================== 其他一些屬性方法 ===============//
curRow:=XLApp.ActiveCell.Row; //當前行數
XLApp.displayAlerts:=false; //運行宏時是否顯示警告和消息
XLApp.ScreenUpdating:=false; //屏幕更新功能,提高速度;
//結束運行後,ScreenUpdating 設回到 True
XLApp.run('MacroName',params...)//運行宏
workbook.save ;
workBook.SaveAs(AFileName,xlNormal,'','',False,False);