根据字段的设置输出到文本,If 要打印,自己再做处理!
procedure SaveDataSet(DataSet: TDataSet;
FileName: String);
var
sFormat: String;
aData: array of String;
aFmtWidth: array of Integer;
i: Integer;
sl: TStringList;
sToAdd: String;
procedure PrepareTitle(var s: String;
const l: Integer;
DisplayName: String;
bLeftAlign: Boolean = True);
const
sAlignment: array[Boolean] of String = ('-', '');
var
sf: String;
begin
sf := '%' + sAlignment[bLeftAlign] + IntToStr(l) + 's ';
s := s + Format(sf, [DisplayName]);
end;
procedure PrepareFormatStr(const l: Integer;
FieldName: String);
begin
SetLength(aFmtWidth, Length(aFmtWidth) + 1);
aFmtWidth[High(aFmtWidth)] := l;
SetLength(aData, Length(aData) + 1);
aData[High(aData)] := FieldName;
end;
function PrepareData(afs: array of Integer;
a: array of String): String;
var
j: Integer;
begin
Result := EmptyStr;
with DataSetdo
for j := Low(a) to High(a)do
PrepareTitle(Result, afs[j], FieldByName(a[j]).AsString, FieldByName(a[j]).Alignment = taLeftJustify);
end;
begin
sFormat := EmptyStr;
sl := TStringList.Create;
Screen.Cursor := crSQLWait;
try
with DataSet.Fieldsdo
begin
for i := 0 to Count - 1do
begin
PrepareTitle(sFormat, Fields.DisplayWidth, Fields.DisplayName, Fields.Alignment = taLeftJustify);
end;
sl.Add(sFormat);
sl.Add(' ');
sFormat := '';
for i := 0 to Count - 1do
begin
PrepareFormatStr(Fields.DisplayWidth, Fields.FieldName)
end
end;
with DataSetdo
begin
First;
while not Eofdo
begin
sToAdd := '';
sToAdd := PrepareData(aFmtWidth, aData);
sl.Add(sToAdd);
Next;
end;
end;
sl.SaveToFile(FileName);
finally
sl.Free;
Screen.Cursor := crDefault;
end
end;