Uses Comobj, Excel97, Excel2000;
// write data from Excel to Access database prodedure ExcelToMdb (EXLfile: string;); var sheet, XLApp, workbook: variant; iRow, MaxRow: integer; begin screen.Cursor: = crHourGlass; try // create the 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 line
// Write data to Access library adotable1.open; for irow: = 2 to maxrow do if sheet.cells [iRow, 1] <> '' TEN // keyword is not empty begin adotable1.append; adotable1.fieldbyname (' ID '). Asinteger: = start (Sheet.cells [IROW, 1]); adotable1.fieldbyname (' code '). Asstring: = sheet.cells [Irow, 2]; // Code Adotable1.fieldByname (' Name ' ) .sstring: = sheet.cells [iRow, 3]; // Name adotable1.post; end;
Finally if NOT VARISEMPTY (XLAP) THEN BEGIN / / Release Object XLAPP.DISPLAYALERTS: = false; xlapp.screenupdating: = true; xlapp.quit; end; screen.cursor: = crdefault; end;
/ / =================== Other attribute methods ================ //
Currow: = xlapp.activecell.row; // Current Row XLapp.displayAlerts: = false; // Whether the macro is displayed WARNING and message xlapp.screenupdating: = false; // screen update function, increase speed; // End Screenupdating set back to TRUE after running
XLapp.Run ('macroname', params ...) // Run macro Workbook.save; Workbook.saveas (AfileName, Xlnormal, '', ', false, false);