本文整理自网络,侵删。
function TForm1.DBGridToTxt(aDBGrid:TDBGrid;isTitle:boolean):boolean;var s:string;
FileName:string; sTemp:TSTrings; i : integer; j : integer; SFD:TSaveDialog; RecNo:integer;begin SFD:=TSaveDialog.Create(nil); SFD.Filter:='文本文件|*.TXT'; SFD.DefaultExt := '.TXT';
sTemp := TStringList.Create; try if SFD.Execute then Begin FileName := SFD.FileName; if isTitle then // 带标题 begin s := ''; for i := 0 to aDBGrid.Columns.Count-1 do begin if aDBGrid.Columns[i].Visible then s := s + Format('%s'+ '|',[aDBGrid.columns[i].title.caption]); end; sTemp.Add(s); end; RecNo := aDBGrid.DataSource.DataSet.RecNo; aDBGrid.DataSource.DataSet.DisableControls; aDBGrid.DataSource.DataSet.First; while not aDBGrid.DataSource.DataSet.Eof do begin s := ''; for j:=0 to aDBGrid.Columns.Count-1 do begin if aDBGrid.Columns[j].Visible then s := s + Format('%s',[aDBGrid.DataSource.DataSet.FieldByName( aDBGrid.Columns[j].FieldName).asString])+'|'; end; sTemp.Add(s); sTemp.Add(''); aDBGrid.DataSource.DataSet.Next; end; sTemp.SaveToFile(FileName); aDBGrid.DataSource.DataSet.RecNo := RecNo; aDBGrid.DataSource.DataSet.EnableControls; end; finally SFD.Free; sTemp.Free; end;end;
相关阅读 >>
Delphi winapi: getwindow - 获取与指定窗口具有指定关系的窗口的句柄
Delphi xe10 fdmemtable 数据保存本地和从本地加载
Delphi issameday、istoday - 判断是不是同一天、判断是不是今天
Delphi 简单的操作memo1剪切 复制 粘贴 撤销 全选 清空
更多相关阅读请进入《Delphi》频道 >>