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;

相关阅读 >>

Delphi 如何在toolbar中的toolbutton上显示文字

Delphi jpg图片 旋转 切边 缩放

Delphi读写firebird二进制数据

Delphi检查字符串是否为纯数字

Delphi文件复制函数

Delphi+access错误"不正常地定义参数对象。提供了不一致或不完整的信息。"

Delphi Delphi 学习大师qq群讨论的字符串位置替换方法

winapi 字符及字符串函数(9): lstrcat - 合并字符串

Delphi idftp用法

Delphixe firemonkey 如何画图

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...