
The comma separated values format (CSV) has been used for exchanging and converting data between various spreadsheet programs for quite some time. It can be used as an Import/Export feature on multiple old and modern applications. And we could say is the de facto standard for data exchange between all architectures of computing.
Creating and manipulating CSV files in Delphi is quiete easy using powerfull CSV library from Winsoft
Installation

Download latest version here, and unzip downloaded file to your default components folder.
This is a non-visual library, so you don’t need to install it on IDE.
Add folder according your Delphi version and Platform in Tools->Options->Language->Delphi->Library to Library Path
Samples
In the following example we will implement a generic import/export functionality to our existing simple application for contacts management.
Creating a new CSV file from Dataset (Export function)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
procedure ExportToFile(ADataset : TDataset; AFileName : string) var CSV : TCSVWriter; i : integer; value : string; begin CSV:=TCsvWriter.Create(AFileName) ADataset.First; // first we need to create header for fieldnames for i := 0 to ADAtaSet.FieldList.Count - 1 do begin CSV.Write(ADataset.Fields[i].fieldName); end; // now we will looping over Dataset to set record values try while not ADataset.eof do begin // Append a new line CSV.NextLine; for i := 0 to ADataSet.FieldList.Count - 1 do begin // special case for date and datetime fields to avoid conflicts with date separators case ADataset.Fields[i].FieldType of ftDate : Value:=FormatDateTime('yyyymmdd', ADataset.Fields[i].AsDatetime); ftDateTime : Value:=FormatDateTime('yyyymmddhhnnsszzz', ADataset.Fields[i].AsDatetime); ftTime : Value:=FormatDateTime('hhnnsszzz', ADataset.Fields[i].AsDatetime); else Value:=ADataset.Fields[i].AsDatetime; end; end; ADataset.Next; end; finally CSV.Free; end; end; |
Loading an existing CSV file to Dataset (Import function)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
procedure ImportFromFile(ADataset : TDataset; AFilenanme ; string); var CSV : TCSV; i,j : integer; Value, FieldName : string; slFields : TStringList; fs: TFormatSettings; begin if not FileExists(AFileName) then Exception.Create('File '+AFileName +' does not exists'); CSV := TCSV.Create; slFields:=TStringList.Create; fs := TFormatSettings.Create; fs.DateSeparator := ''; fs.ShortDateFormat := 'yyyymmdd'; fs.TimeSeparator := ''; fs.ShortTimeFormat := 'hhmmss'; fs.LongTimeFormat := 'hhmmss'; try CSV.LoadUtf8File(AFileName); // mapping for proper fields names info on first line of file for i := CSV.FieldsCount[0] do begin slFields.Add(CSV.Fields[0, i]) end; for i:= 1 to CSV.LineCount - 1 do begin ADataset.Append; for j := 0 to CSV.FieldCount[i] do begin // get field value of current record Value:=CSV.Fields[i, j]; FieldName:=slFiels.Strings[j]; case ADataset.FieldByName(FieldName).FieldType of ftDate , ftDateTime, ftTime: Value:= StrToDateTime(Value); end; ADataset.FieldByName(FieldName).Value:=Value; end; ADataSet.Post; end; finally CSV.Free; slField.Free; fs.Free; end; end; |
Head over and check out the full WINSOFT CSV in Delphi and C++Builder
Like what you see? You can get the WinSoft CSV Library, along with over 100 other fantastic WinSoft components, with our Enterprise Component Pack. For a limited time, when you purchase RAD Studio Enterprise or Architect Edition at special Upgrade Pricing, you will also get this package of third-party software worth over $13,000, including the full WinSoft Component Library, at NO EXTRA COST! Step up to RAD Studio 10.4 today!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
