use   comobj;  
   
  procedure   TForm1.Button3Click(Sender:   TObject);  
  var  
      e:variant;  
      i,j:integer;  
  begin   
      if(DBGrid1.DataSource.DataSet.State = dsBrowse) then
    begin
      e:=createoleobject('excel.application');  
      e.visible:=true;  
      e.caption:='收发存汇总表';  
      e.workbooks.add;  
   
        e.activesheet.rows[1].font.bold:=true;       //第一行列名属性设置;  
        e.activesheet.rows[1].font.color:=clred;  
        for   j:=0   to   DBGrid1.DataSource.DataSet.FieldCount-1   do       //第一行列名;
        begin  
        e.cells[1,j+1].value:=DBGrid1.DataSource.DataSet.Fields[j].FieldName   ;
        next;  
        end;  
   
        DBGrid1.DataSource.DataSet.first;                                                   //导出内容;
        for   i:=1   to   DBGrid1.DataSource.DataSet.recordcount   do
          begin  
              begin  
                for   j:=0   to   DBGrid1.DataSource.DataSet.FieldCount-1   do   //;
                begin  
                    e.cells[i+2,j+1].value:=DBGrid1.DataSource.DataSet.Fields[j].AsString   ;
                    next;  
                end;  
              end;  
              DBGrid1.DataSource.DataSet.next;
              next;
          end;  
            ShowMessage('数据导出成功');  
            e.workbooks.close;  
            e.quit;
    end;       
  end;