CSV is an abbreviation for a Comma-Separated Value file that enables data to be saved in a loosely-structured text form. Because of its simple format, it is very easy to process the data inside the file and extremely portable too.
The great thing about the CSV files is that they are compatible with almost any text editor as well as a large number of popular applications such as Microsoft Excel or Google Sheets, making them easy to create, share, and edit.
A CSV file is a type of delimited text file because it typically utilizes a comma to separate values. Some files also use the TAB character (ASCII 9) instead of the comma to break up the fields. Files that use the TAB character in this way are often referred to as Tab-Separated Value TSV files. Fields can also be optionally ‘wrapped’ with double-quotes (“), especially if they might contain commas. However, these CSV/TSV files are formatted, we can easily read them using windows app development tools like Delphi and the FireDAC components.
Table of Contents
Are CSV files used in professional businesses?
The short answer is: yes. CSV files are so easy to use that almost every type of business makes use of them to transfer and exchange data.
One example is updating or downloading product data from an application. For example, you can bulk import new products to your system or store web app.
Because of its simplicity and straightforward format, millions of developers utilize CSV files. Often, tech savvy Business owners can quickly read, comprehend, and edit the information without specialist help.
Here is an example of a CSV file:
How to use CSV files in Delphi applications?
Embarcadero RAD Studio provides Delphi & C++ Builder development environment, and they have FireDAC components. FireDAC provides a common API for accessing different database back-ends with high performance.
Moreover, FireDAC components provide easy access to CSV files. It abstracts the data into tables, views and stored procedures can be used to process CSV file.
In this demonstration, we will utilize TFDBatchMove. We can use the TFDBatchMove component to move data between plain text files and tables using datasets.
This demo project shows how to utilize the TFDBatchMove component to move information between different sources.
- Plain text to Table
- Table to Text
- Table to Plain text
The TFDBatchMove implements the needed engine to move data between different types of data, and it uses reader and writer components. Here are some of the standard reader and writer components:
- TFDBatchMoveTextReader – Reader for plain text file
- TFDBatchMoveDataSetWriter – Writer for TDataSet
- TFDBatchMoveDataSetReader – Reader for TDataSet
- TFDBatchMoveTextWriter – Writer for plain text file
These components define the data source reader using plain text files. So after establishing a stream from where to move the data using TFDBatchMove, it is easy to work with files.
In this code, you can see that there are custom properties adjusted according to the plain text file format. So, the TFDBatchMove can automatically manage the reader instance.
// Text file -> DB table
procedure TfrmMain.btnASCToTabMoveClick(Sender: TObject);
// Create text reader and set FDBatchMode as owner. Then
// FDBatchMove will automatically manage the reader instance.
with TFDBatchMoveTextReader.Create(FDBatchMove) do
// Set text data file name
FileName := ExtractFilePath(Application.ExeName) + 'Data.csv';
// Setup file format
DataDef.Separator := ';';
DataDef.WithFieldNames := True;
// Create dataset writer and set FDBatchMode as owner. Then
// FDBatchMove will automatically manage the writer instance.
with TFDBatchMoveDataSetWriter.Create(FDBatchMove) do
// Set destination dataset
DataSet := qryLoaded;
// Do not set Optimise to True, if dataset is attached to UI
Optimise := False;
// Analyze source text file structure
// show data in dbgrid
Separator – Define the separator used to separate the fields in the plain text file (or CSV file)
WithFieldNames – Specify that the field name is included in the first row of the CSV file
Here you can see the Delphi demo CSV project in action
Check out the full source code here on Embarcadero Demos Repository.
As you can see in this demo project, we have used manual coding to read and write data. But you can access comma-separated files without coding by using dropping all the components into the form. So you can define component properties and link them together.
If you would like to learn about it, be sure to check out this tutorial written by Dr. Holger Flick who is one of Embarcadero Delphi MVPs.
To learn more about CSV data processing with Delphi or C++ Builder, be sure to check out these articles
RAD Studio Delphi is the driving force behind a huge collection of applications which rely on it to make the task of processing, transforming and rendering data as easy and as fast as possible. Why not download a free trial copy of RAD Studio today?