unit Unit1;
interface
uses db, classes, dialogs,sysutils;
{过程 DataSetToASCII - 从TDataSet 类型的变量 ADataSet 中输出记录
到一个指定的 ASCII 文本文件. 各字段被名为 Delimiter 的分隔字符分隔。
只有 Tag 属性为 0 (缺省值)的字段被输出.各条记录间用换行符分开。
如果参数 QuoteStrings 为 True 则字段类型为
ftString,ftMemo,ftFmtMemo,ftFixedChar 和 ftWideString 的字段将被引号引起再输出}
procedure DataSetToASCII(const ADataSet: TDataSet; const ASCIIFile: TFileName; const Delimiter: Char; const QuoteStrings: Boolean);
implementation
procedure DataSetToASCII(const ADataSet: TDataSet; const ASCIIFile: TFileName; const Delimiter: Char; const QuoteStrings: Boolean);
var tmpList: TStringList;
i,LastIndex: LongInt;
AsciiRecord: String;
begin
tmpList:= TStringList.Create;
try
with ADataSet do begin
LastIndex:= Fields.Count - 1;
First;
while not EOF do begin
AsciiRecord:= '';
for i := 0 to LastIndex do
if Fields.Fields.Tag = 0 then begin
if QuoteStrings and
(Fields.Fields.DataType in [ftString,ftMemo,ftFmtMemo,ftFixedChar,ftWideString]) then
AsciiRecord:= AsciiRecord + QuotedStr(Fields.Fields.AsString)
else
AsciiRecord:= AsciiRecord + Fields.Fields.AsString;
if i < LastIndex then
AsciiRecord:= AsciiRecord + Delimiter;
end;
tmpList.Append(AsciiRecord);
Next
end
end;
try
tmpList.SaveToFile(ASCIIFile)
except
ShowMessage('Could not save table to specified file: ' + ASCIIFile)
end;
finally
tmpList.Free;
end;
end;
end.