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
Table of Contents
Installation
Menu Tools >Options >Language >Delphi >Library
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)
Exception.Create('File '+AFileName+' does not exist');
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
fori:=CSV.FieldsCount[0]dobegin
slFields.Add(CSV.Fields[0,i])
end;
fori:=1toCSV.LineCount-1dobegin
ADataset.Append;
forj:=0toCSV.FieldCount[i]dobegin
// get field value of current record
Value:=CSV.Fields[i,j];
FieldName:=slFields.Strings[j];
caseADataset.FieldByName(FieldName).FieldType of
ftDate,ftDateTime,ftTime:
Value:=StrToDateTime(Value,fs);
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!
Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder. Design. Code. Compile. Deploy. Start Free TrialUpgrade Today
Wow, this article was written just prior to me starting to edit for the blog. We do try to vet the content a lot more closely nowadays although things do still sneak through. It definitely had a few quality issues, sorry about that!
I’ve fixed the layout to match the preferred Embarcadero coding style, corrected the typos in the code and, yes, thanks to you Jeff I’ve also corrected the StrToDateTime call to be the overloaded version which uses the optional format settings argument which was the original point of the article in the first place… doh!
Is the call to StrToDateTime in ImportFromFile meant to be StrToDateTime(Value, fs)?
Wow, this article was written just prior to me starting to edit for the blog. We do try to vet the content a lot more closely nowadays although things do still sneak through. It definitely had a few quality issues, sorry about that!
I’ve fixed the layout to match the preferred Embarcadero coding style, corrected the typos in the code and, yes, thanks to you Jeff I’ve also corrected the
StrToDateTime
call to be the overloaded version which uses the optional format settings argument which was the original point of the article in the first place… doh!Thanks for letting us know. 😃👍