![]() |
Export eines TDataset in eine CSV Datei
Die nachfolgende Funktion exportiert den Inhalt eines TDataSet in die im Parameter ExpFileName angegebene Datei. ExpFileName erwartet einen vollständigen Dateinamen inklusive Pfad.
Über die optionalen Parameter newfile kann festgelegt werden, ob eine bereits existierende Datei mit dem selben Namen überschrieben oder ergänzt werden soll. Der Parameter ReplaceCrLf legt fest, ob Zeilenumbrüche entfernt werden sollen.
Delphi-Quellcode:
procedure ExportTbltoCsv(Source: TDataSet; ExpFileName: string; const newfile:
Boolean = False;const ReplaceCrLf: Boolean = False); var ExportFile: TextFile; ExportString: string; Counter: Integer; MyBookmark: TBookmark; begin if not DirectoryExists(ExtractFilePath(ExpFileName)) then ForceDirectories(ExtractFilePath(ExpFileName)); AssignFile(ExportFile, ExpFileName); if newfile or not FileExists(ExpFileName) then Rewrite(ExportFile) else Append(ExportFile); for Counter := 0 to (Source.Fields.Count - 1) do ExportString := ExportString + Trim(Source.Fields[Counter].FieldName) + ';'; WriteLn(ExportFile, ExportString); ExportString := ''; MyBookmark := Source.GetBookmark; Source.DisableControls; Source.First; while not Source.Eof do begin ExportString := ''; for Counter := 0 to (Source.Fields.Count - 1) do ExportString := ExportString + Trim(VarToStr(Source.Fields[Counter].Value)) + ';'; if ReplaceCrLf then begin ExportString := StringReplace(ExportString, #13 + #10, ' | ', [rfReplaceAll]); ExportString := StringReplace(ExportString, #10 + #13, ' | ', [rfReplaceAll]); end; WriteLn(ExportFile, ExportString); Source.Next; end; if Source.BookmarkValid(MyBookmark) then Source.GotoBookmark(MyBookmark); Source.FreeBookmark(MyBookmark); Source.EnableControls; CloseFile(ExportFile); end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:41 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz