procedure OutPutDBGridToCSV(AGrd:TDBGrid; AQuery: TQuery);
var i,ColNum: Integer; Title,Data,SaveName: String; ALst: TStringList;begin try ALst:=TStringList.Create; ALst.Clear;ColNum:=0;
ColNum:=AGrd.Columns.Count-1;Title:='';
Title:='"'+AGrd.Columns.Items[0].Title.Caption+'"'; for i := 1 to AGrd.Columns.Count-1 do begin Title:=Title+',"'+AGrd.Columns.Items[i].Title.Caption+'"'; end; ALst.Add(Title);with AQuery do
begin First; while not Eof do begin Data:='"'+FieldByName(AGrd.Columns.Items[0].FieldName).AsString+'"'; for i := 1 to ColNum do begin Data:=Data+',"'+FieldByName(AGrd.Columns.Items[i].FieldName).AsString+'"'; end; ALst.Add(Data); Next; end; end; SaveName:=FormatDateTime('yyyyMMDDhhmmss',Now); SaveName:='C:\'+SaveName+'.csv'; ALst.SaveToFile(SaveName); finally ALst.Free; end; Application.MessageBox(PChar('文件保存至'+SaveName),'系统提示',64);end;