delphi DBGrid 保存TXT  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi DBGrid 保存TXT


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;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.045692920684814 seconds