利用Variant变量用DELPHI操作EXCEL,代码摘要:
var v:Variant;
procedure TForm1.Button1Click(Sender: TObject); begin //打开EXCEL新建文件 try v:= CreateOleObject('Excel.Application'); v.Visible := CheckBox1.Checked;//是否显示 v.Workbooks.Add;//新建EXCEL文件 v.Workbooks[1].WorkSheets[1].Name := 'DELPHI演示'; // Sheet:= v.Workbooks[1].WorkSheets['DELPHI演示'];//等效下面的语句 Sheet:= v.Workbooks[1].WorkSheets[1]; Sheet.Cells[1,1] :='DELPHI盒子'; Sheet.Cells[2,1] :='http://www.delphibox.com'; except Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。'); v.DisplayAlerts := false;//是否提示存盘 v.Quit;//如果出错则退出 exit; end; Application.Restore; Application.BringToFront; end; |