供你参考
Procedure SendDataTotxtfile(PriKey,Txt_Name : String;DsArr : Array of TClientDataSet);
var
i,j,MaxLen,CurLen,StartPos,n : integer;
PField,Fieldstr,tempstr : String;
FDisp : Array of String;
FTxt : TextFile;
begin
SetLength(Fdisp,length(dsArr));
AssignFile(FTxt,Txt_Name);
ReWrite(FTxt);
MaxLen := 0;
StartPos := Low(DsArr);
for i := StartPos to High(DsArr) do
begin
CurLen := 0;
FieldStr := '';
for j := 0 to DsArr.Fields.Count - 1 do
begin
CurLen := CurLen + DsArr.Fields[j].DataSize;
if (Pos(DsArr.Fields[j].FieldName,PriKey) <> 0) and (i<>0) then
begin
for n := 1 to DsArr.Fields[j].DataSize do
FieldStr := FieldStr + ' ';
end else begin
FieldStr := FieldStr + DsArr.Fields[j].DisplayLabel;
for n := 1 to DsArr.Fields[j].DataSize -
Length(DsArr.Fields[j].DispLayLabel) do
FieldStr := FieldStr + ' ';
end;
end;
if CurLen + DsArr[StartPos].Fields[0].DataSize > MaxLen then
MaxLen := CurLen + DsArr[StartPos].Fields[0].DataSize;
FDisp := FieldStr;
end;
DsArr[StartPos].First;
if Length(DsArr) = 1 then
Writeln(FTxt,Fdisp[0]);
While Not DsArr[StartPos].Eof do
begin
FieldStr := '';
for i := 0 to DsArr[StartPos].Fields.Count - 1 do
begin
FieldStr := FieldStr + DsArr[startpos].Fields.AsString;
for n := 1 to DsArr[StartPos].Fields.DataSize -
Length(DsArr[StartPos].Fields.asstring) do
FieldStr := FieldStr + ' ';
end;
if Length(DsArr) > 1 then
writeln(Ftxt,Fdisp[0]);
Writeln(Ftxt,FieldStr);
For i := StartPos+1 to High(DsArr) do
begin
FieldStr := '';
DsArr.Filter := '';
DsArr.Filtered := False;
tempstr := PriKey;
While Pos(',',tempstr) <> 0 do
begin
PField := Copy(tempstr,1,pos(',',tempstr)-1);
Delete(tempstr,1,pos(',',tempstr));
DsArr.Filter := DsArr.Filter + PField+'='''+DsArr[0].FieldByName(PField).asstring+''' AND ';
end;
DsArr.Filter := Copy(DsArr.Filter,1,Length(DsArr.Filter)-5);
DsArr.Filtered := True;
For n := 0 to MaxLen do
FieldStr := FieldStr + '-';
Writeln(Ftxt,FieldStr);
Writeln(Ftxt,FDisp);
DsArr.First;
While Not DsArr.Eof do
begin
FieldStr := '';
for j := 0 to DsArr.Fields.Count - 1 do
begin
if Pos(DsArr.Fields[j].FieldName,PriKey) = 0 then
begin
FieldStr := FieldStr + DsArr.Fields[j].AsString;
for n := 1 to DsArr.Fields[j].DataSize -
Length(DsArr.Fields[j].asstring) do
FieldStr := FieldStr + ' ';
end else
for n := 1 to DsArr.Fields[j].DataSize do
FieldStr := FieldStr + ' ';
begin
end;
end;
Writeln(Ftxt,FieldStr);
DsArr.Next;
end;
end;
DsArr[StartPos].Next;
FieldStr := '';
if Length(DsArr) > 1 then
begin
For n := 0 to MaxLen do
FieldStr := FieldStr + '=';
Writeln(ftxt,'');
Writeln(Ftxt,FieldStr);
Writeln(ftxt,'');
end;
end;
CloseFile(FTxt);
end;