users ComObj
procedure saveToExcel();
var
Ole:variant;
Ac,Ar,a,b:integer;
begin
if not Form12.ADOQuery2.Active then
begin
ShowMessage('数据集没有打开');
Exit;
end;
Try
Ole:=createoleobject('Excel.Application'); //创建OLE对象
Except
Showmessage('Excel没有安装或不正确');
Exit;
end;
begin
Ole.workbooks.add; //添加工作薄
Form12.ADOQuery2.First; //将数据集中的数据导入Excel表格
Form12.ADOQuery2.DisableControls;
Ac:=Form12.ADOQuery2.FieldCount;
Ar:=Form12.ADOQuery2.RecordCount;
for a:=1 to Ac do //将数据表中的数据导入表格
Ole.Cells[1,a]:=Form12.DBGrid1.Fields[a-1].FieldName;
for a:=2 to Ar+1 do
begin
for b:=0 to Ac-1 do
Ole.cells[a,b+1]:=Form12.DBGrid1.Fields[b].AsString;
Form12.ADOQuery2.Next;
end;
Ole.cells[Ar+2,1] := '满足条件记录的总数为:'+inttostr(Form12.ADOQuery2.RecordCount)+'条';
application.MessageBox('数据导出完成!','提示',mb_ok+mb_iconinformation);
Ole.visible := true;
end;
end;