Delphi与Excel (转)
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; //最大行数
maxCol:=XLApp.ActiveCell.Column ; //最大列数
//写数据到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);