给你一段代码
procedure tdatamodule1.copydbdatatoexcel(target:tdbgrid);
var
icount,jcount:integer;
xlapp:variant;
sheet:variant;
begin
screen.Cursor:=crhourglass;
if not varisempty(xlapp) then
begin
xlapp.displayalerts:=false;
xlapp.quit;
varclear(xlapp);
end;
try
xlapp:=createoleobject('Excel.Application');
except
screen.Cursor:=crdefault;
exit;
end;
xlapp.workbooks.add[xlwbatworksheet];
xlapp.workbooks[1].worksheets[1].name:='档案管理系统';
sheet:=xlapp.workbooks[1].worksheets['档案管理系统'];
if not target.DataSource.DataSet.Active then
begin
screen.Cursor:=crdefault;
exit;
end;
target.DataSource.DataSet.first;
for icount:=0 to target.Columns.Count-1do
begin
sheet.cells[1,icount+1]:=target.Columns.Items[icount].Title.caption;
end;
jcount:=1;
while not target.DataSource.DataSet.Eofdo
begin
for icount:=0 to target.Columns.count-1do
begin
sheet.cells[jcount+1,icount+1]:=target.Columns.Items[icount].Field.AsString;
end;
inc(jcount);
target.DataSource.dataset.Next;
end;
xlapp.visible:=true;
screen.Cursor:=crdefault;
end;
记着引用excel2000