简单的例子
1 procedure TForm1.Button1Click(Sender: TObject); 2 var 3 ExcelApp,MyWorkBook: OLEVariant; 4 begin 5 opendialog1.Filter:='Microsoft Excel Workbook (*.xls)|*.XLS|'; 6 edit2.Text := 'sheet1'; 7 if opendialog1.Execute then 8 begin 9 edit1.Text:=opendialog1.FileName; 10 try 11 ExcelApp:=CreateOleObject('Excel.Application'); 12 MyWorkBook:=CreateOleobject('Excel.Sheet'); 13 except 14 application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','', 15 mb_OK+mb_IconStop); 16 Exit; 17 end; 18 MyworkBook:= ExcelApp.workBooks.Open(opendialog1.FileName); 19 //打开文件后,对文件进行操作 20 label1.caption := MyWorkBook.WorkSheets[1].Cells[1,1].Value; 21 end; 22 end;