给你一个过程:{功能:将数据集体保存为文本文件;} function datatotxtfile(ADataSet: TDataSet; ASCIIFile:string):boolean; //格式化字符串 function addspces(astring:String;lgth:integer;is_Right:boolean=true):string; var tmps:string; i:integer; begin result:=astring; if length(astring)<lgth then begin for i:=0 to lgth-length(astring)-1 do tmps:=tmps+' '; end; if is_Right then result:=result+tmps else result:=tmps+result; end; var tmpList: TStringList; AsciiRecord: String; fldindex:integer; begin result:=true; tmpList:= TStringList.Create; if ADataSet=nil then exit; try ADataSet.DisableControls; with ADataSet do begin for fldindex := 0 to FieldCount-1 do begin if fields[fldindex].Visible then AsciiRecord:= AsciiRecord + addspces(Fields[fldindex].DisplayLabel,fields[fldindex].DisplayWidth+2) end; tmpList.Append(AsciiRecord); First; while not EOF do begin AsciiRecord:= ''; for fldindex := 0 to FieldCount - 1 do begin if fields[fldindex].Visible then AsciiRecord:= AsciiRecord + addspces(Fields[fldindex].AsString,fields[fldindex].DisplayWidth+2) end; tmpList.Append(AsciiRecord); Next; end; end; try tmpList.SaveToFile(ASCIIFile) ; except result:=false; end; finally ADataSet.EnableControls; tmpList.Free; end; end;
自定义一个记录文件,然后从表中读记录写入文件! type filerec= record field1 field2 ...... end; filetypename=file of filerec; YouFile:FileTypeNamel; write(tablerec);
function datatotxtfile(ADataSet: TDataSet; ASCIIFile:string):boolean;
//格式化字符串
function addspces(astring:String;lgth:integer;is_Right:boolean=true):string;
var tmps:string;
i:integer;
begin
result:=astring;
if length(astring)<lgth then
begin
for i:=0 to lgth-length(astring)-1 do
tmps:=tmps+' ';
end;
if is_Right then result:=result+tmps
else result:=tmps+result;
end;
var tmpList: TStringList;
AsciiRecord: String;
fldindex:integer;
begin
result:=true;
tmpList:= TStringList.Create;
if ADataSet=nil then exit;
try
ADataSet.DisableControls;
with ADataSet do
begin
for fldindex := 0 to FieldCount-1 do
begin
if fields[fldindex].Visible then
AsciiRecord:= AsciiRecord + addspces(Fields[fldindex].DisplayLabel,fields[fldindex].DisplayWidth+2)
end;
tmpList.Append(AsciiRecord);
First;
while not EOF do
begin
AsciiRecord:= '';
for fldindex := 0 to FieldCount - 1 do
begin
if fields[fldindex].Visible then
AsciiRecord:= AsciiRecord + addspces(Fields[fldindex].AsString,fields[fldindex].DisplayWidth+2)
end;
tmpList.Append(AsciiRecord);
Next;
end;
end;
try
tmpList.SaveToFile(ASCIIFile) ;
except
result:=false;
end;
finally
ADataSet.EnableControls;
tmpList.Free;
end;
end;
type
filerec= record
field1
field2
......
end;
filetypename=file of filerec;
YouFile:FileTypeNamel;
write(tablerec);