procedure SaveToExecl(DBGrid1: TDBGrid); var s:TStringList; str:string; i:Integer; ASaveDialog : TSaveDialog; tofileName : string; begin str:=''; tofileName := ''; DBGrid1.DataSource.DataSet.DisableControls; try for i:=0 to DBGrid1.DataSource.DataSet.FieldCount-1 do str:=str+DBGrid1.DataSource.DataSet.fields.DisplayLabel+char(9); str:=str+#13; if not DBGrid1.DataSource.DataSet.IsEmpty then begin DBGrid1.DataSource.DataSet.First; while not(DBGrid1.DataSource.DataSet.eof) do begin for i:=0 to DBGrid1.DataSource.DataSet.FieldCount-1 do str:=str+DBGrid1.DataSource.DataSet.Fields.AsString+char(9); str:=str+#13; DBGrid1.DataSource.DataSet.next; end;//end while end; finally DBGrid1.DataSource.DataSet.EnableControls; end; s:=TStringList.Create; try s.Add(str); ASaveDialog := TSaveDialog.Create(nil); try ASaveDialog.Title := '保存文件'; ASaveDialog.Filter := 'Microsof Excel (*.xls)|*.xls'; if ASaveDialog.Execute then begin tofileName := ASaveDialog.FileName; if pos(uppercase('.xls'),uppercase(tofileName)) = 0 then tofileName := tofileName +'.xls'; if Fileexists(tofileName) then begin if MessageDLG('文件已存在,确认覆盖?',mtWarning,[mbOK,mbCancel],0) = mrOk then begin Deletefile(tofileName); s.SaveToFile(tofileName);//保存 Application.MessageBox('数据导出完毕!','信息',MB_OK); end; end else begin s.SaveToFile(tofileName);//保存 Application.MessageBox('数据导出完毕!','信息',MB_OK+MB_ICONINFORMATION); end; end; finally FreeAndNil(ASaveDialog); end; finally FreeAndNil(s); end;