有两种方法应用excel:
1.excelapplication、excelworkbook、excelworksheet控件
应用此种方法打开excel方法如下:
Begin
Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=TRUE;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
........//对excel表的操作
ExcelApplication1.Quit; //释放excel
End;
2.用createoleobject 方法
Var
ExcelApp,MyWorkBook:Variant;
filename: string;
Begin
Try
ExcelApp:=CreateOleObject('Excel.Application');
Except
MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
Abort;
End;
filename:='d:/book.xls'; //要打开的excel文件
ExcelApp.workBooks.Open(filename);
MyworkBook1:=ExcelApp.workBooks[1];
......//对excel进行操作
MyWorkBook1.saveas(filename);
MyWorkBook1.close; //保存操作
ExcelApp.Quit;
ExcelApp:=Unassigned; //释放excel
End;