var v: Variant; y: Integer; Begin If OpenDialog1.Execute Then begin v := CreateOleObject('Excel.Application'); try v.Workbooks.Open(OpenDialog1.FileName); v.WorkSheets[1].Activate; y:=v.ActiveSheet.UsedRange.Rows.Count; try if y>1 then begin v.Cells[1,5].Value:='WL时间'; v.Cells[1,6].Value:='LS时间'; v.Range[v.Cells[2,5],v.Cells[y,5]].FormulaR1C1:='=(RC[-1]-RC[-2])*24'; v.Range[v.Cells[2,6],v.Cells[y,6]].FormulaR1C1:='=(RC[-3]-RC[-4])*24'; end else Showmessage('指定文件没有数据。'); finally v.DisplayAlerts:=false;//不提示 v.ActiveWorkBook.Close(true,OpenDialog1.FileName);//存盘退出 v.Quit; end; except v.DisplayAlerts:=false; v.WorkBooks.Close; v.Quit; Showmessage('初始化Excel失败,可能没装Excel,或者其他错误。'); end; end; end;