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;
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。