Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
Delphi 10 Seattle Enterprise
|
Re: DBGrid -> CSV
10. Feb 2010, 18:13
Das sollte eigentlich das machen was du wünschst:
Delphi-Quellcode:
procedure DataSetExportToCSV( const DataSet : TDataSet; const FileName : string; const Header : boolean = False; const Delim : string = ' ;'; const Quote : string = ' "' );
var
bm : TBytes;
idx : integer;
row, val : string;
csv : Text;
begin
if DataSet.Active then
begin
bm := DataSet.GetBookmark;
DataSet.DisableControls;
AssignFile( csv, FileName );
try
AssignFile( csv, FileName );
ReWrite( csv );
try
if Header then
begin
row := ' ';
for idx := 0 to DataSet.FieldCount - 1 do
begin
val := DataSet.Fields.Fields[ idx ].FieldName;
val := Quote + StringReplace( val, Quote, Quote+Quote, [ rfReplaceAll ] ) + Quote;
if idx > 0 then
row := row + Delim;
row := row + val;
end;
WriteLn( csv, row );
end;
DataSet.First;
while not DataSet.Eof do
begin
row := ' ';
for idx := 0 to DataSet.FieldCount - 1 do
begin
val := DataSet.Fields.Fields[ idx ].AsString;
if Pos( Delim, val ) + Pos( Quote, val ) > 0 then
val := Quote + StringReplace( val, Quote, Quote+Quote, [ rfReplaceAll ] ) + Quote;
if idx > 0 then
row := row + Delim;
row := row + val;
end;
WriteLn( csv, row );
DataSet.Next;
end;
finally
CloseFile( csv );
end;
finally
DataSet.EnableControls;
end;
if DataSet.BookmarkValid( bm ) then
DataSet.GotoBookmark( bm );
end;
end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
|
|
Zitat
|