给我分。我给源代码,调试通过的。
procedure tfrmprint2.saveAsExcel;
var
fieldNum: integer;
i,j : integer;
exls,sheet: variant;
begin
try
exls:=createoleobject('Excel.application');
sheet:=createoleobject('excel.sheet');
except
application.MessageBox('请先安装MICROSOFT EXCEL!','提示',mb_ok+mb_iconinformation);
exit;
end;
sheet:=exls.workBooks.Add;
with query2 do
begin
DisableControls;
fieldNum := dbgrid1.fieldCount;
for i:=1 to fieldNum do
begin
exls.Cells[1,i].value:=Fields[i-1].FieldName;
end;
first;
i:=2;
while not eof do
begin
for j:=1 to fieldNum do
begin
exls.Cells[i,j]:=fields[j-1].AsString;
end;
next;
i:=i+1;
end;
EnableControls;
exls.visible:=true;
end;
end;