uses comobj,Excel97,excel2000;
//Write data from Excel to access library
PRodedure ExcelToMdb(EXLfile:string;);
var
sheet,XLApp,workbook : variant;
iRow,MaxRow:integer;
begin
screen.Cursor:=crHourGlass;
try
//Create object
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];
//Get the maximum number of rows maxRow
XLApp.ActiveCell.SpecialCells(xlLastCell).Select;
maxRow:=XLApp.ActiveCell.Row; //Maximum number of rows
//Write data to Access library
ADOTable1.open;
for iRow:=2 to MaxRow do
if sheet.cells[iRow,1]<>'' then //The keyword is not empty
begin
ADOTable1.Append;
ADOTable1.fieldByName('ID').asInteger:=
strToInt(sheet.cells[iRow,1]);
ADOTable1.fieldByName('code').asString:=sheet.cells[iRow,2];//encoding
ADOTable1.fieldByName('name').asString:=sheet.cells[iRow,3];//name
ADOTable1.post;
end;
finally
if not VarIsEmpty(XLApp) then begin //Release the object
XLApp.displayAlerts:=false;
XLApp.ScreenUpdating:=true;
XLApp.quit;
end;
screen.Cursor:=crDefault;
end;
end;
//==================== Some other attribute methods===============//
curRow:=XLApp.ActiveCell.Row;//Current row number
XLApp.displayAlerts:=false;//Whether to display warnings and messages when running macros
XLApp.ScreenUpdating:=false; //Screen update function, improve speed;
//After finishing the operation, ScreenUpdating is set back to True
XLApp.run('macroName',params...)//Run macro
workbook.save;
workBook.SaveAs(AFileName,xlNormal,'','',False,False);