Tutorial: Read a Basic CSV File with Delphi.Net
This tutorial demonstrates how to use the Xilytix TFieldedText component to read a CSV file and place the headings and records into a Delphi.NET StringGrid.
The contents of the CSV file to be imported are shown here
"Pet Name", "Age", "Color", "Date Received", "Price", "Needs Walking", "Type"
, (Years), , , (Dollars), ,
"Rover, 4.5, Brown, 12 Feb 2004, 80, True, "Dog"
"Charlie", , Gold, 5 Apr 2007, 12.3, False, "Fish"
"Molly", 2, Black, 12 Dec 2006, 25, False, "Cat"
"Gilly", , White, 10 Apr 2007, 10, False, "Guinea Pig"
This text file has 2 headings and 4 records. This tutorial will show how to read this file into a StringGrid on a Delphi.Net Form. The full source code for this tutorial lesson can be downloaded from the component's home page
Step 1: Select a folder
Create or select an empty folder for the program we are going to develop.
Step 2: Get text file
Create a text file called "BasicReadExample.txt" and place the contents of the above CSV file into it. Save this file to the folder where the compiled executable will run from (normally the project folder). Alternatively download the text file from here (Right Click | Save Link/Target As) and save to the folder.
Step 3: Create Fielded Text Meta file
The Meta file specifies the structure of the text file. In this step we will create a Meta file called "BasicExample.ftm" for the text file. There are 2 ways of doing this.
The easy way is to simply download the Meta file from here (Right Click | Save Link/Target As) and place in the folder where the compiled executable will run from.
Alternatively, you can create it with a Fielded Text Editor. This will provide a better understanding of what a Meta file actually is. Follow the steps here to create the "BasicExample.ftm" and then save it into the folder where the compiled executable will run from.
The contents of the Fielded Text Meta File should be similar to the text shown below
<?xml version="1.0" encoding="utf-8"?>
<FieldedText HeadingLineCount="2" HeadingWritePrefixSpace="True">
<Field Name="PetName" ValueQuotedType="Always" HeadingWritePrefixSpace="False"
Headings="Pet Name" />
<Field DataType="Float" Name="Age" ValueWritePrefixSpace="True" Headings="Age, "
/>
<Field Name="Color" ValueWritePrefixSpace="True" Headings="Color, (Years)" />
<Field DataType="DateTime" Name="DateReceived" ValueWritePrefixSpace="True"
Headings="Date Received, " Format="d MMM yyyy" />
<Field DataType="Decimal" Name="Price" ValueWritePrefixSpace="True"
Headings="Price, (Dollars)" />
<Field DataType="Boolean" Name="NeedsWalking" ValueWritePrefixSpace="True"
Headings="Needs Walking, " />
<Field Name="Type" ValueQuotedType="Always" ValueWritePrefixSpace="True"
Headings="Type" />
</FieldedText>
Step 4: Create a Delphi.Net VCL Forms Application project
Create a Delphi.Net VCL Forms Application project in the folder using one of: BDS 2005, BDS 2006 or Turbo Delphi.Net
Step 5: Reference the FieldedText Assembly
Add a reference to the Xilytix.FieldedText.dll assembly. This assembly can be downloaded from here.
Step 6: Place controls on Form
Add the following items to a form in the project:
- A StringGrid with Name = "StringGrid1"
- A Button
Step 7: Add units to uses clauses
Make sure that the following units are are included in the interface and implementation uses clauses.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,
System.ComponentModel,
Borland.Vcl.StdCtrls,
Borland.Vcl.Grids;
...
implementation
uses
System.Data,
Xilytix.FieldedText.Main;
Step 8: Button Click event
Add the following code to the Click event of the button:
var
FieldedText: TFieldedText;
LineIdx: Integer;
FieldIdx: Integer;
Reader: IDataReader;
PetNameOrd: Integer;
AgeOrd: Integer;
ColorOrd: Integer;
DateReceivedOrd: Integer;
PriceOrd: Integer;
NeedsWalkingOrd: Integer;
TypeOrd: Integer;
begin
FieldedText := TFieldedText.Create;
// Load the Meta which describes the text file
// A Fielded Text Editor is used to create the Meta File.
FieldedText.LoadMeta('BasicExample.ftm');
// Parse the file once to work out get headers and calculate number or records
FieldedText.Parse('BasicReadExample.txt', True);
// Set up grid
StringGrid1.ColCount := FieldedText.FieldCount;
StringGrid1.RowCount := FieldedText.HeadingLineCount + FieldedText.RecordCount;
StringGrid1.FixedRows := FieldedText.HeadingLineCount;
// Write Headers to Grid
for LineIdx := 0 to FieldedText.HeadingLineCount - 1 do
begin
for FieldIdx := 0 to FieldedText.FieldCount - 1 do
begin
StringGrid1.Cells[FieldIdx, LineIdx] := FieldedText.Fields[FieldIdx].Headings[LineIdx];
end;
end;
// Use a standard IDataReader to get information for records
Reader := FieldedText.ExecuteReader('BasicReadExample.txt');
// Use GetOrdinal to reference fields
PetNameOrd := Reader.GetOrdinal('PetName');
AgeOrd := Reader.GetOrdinal('Age');
ColorOrd := Reader.GetOrdinal('Color');
DateReceivedOrd := Reader.GetOrdinal('DateReceived');
PriceOrd := Reader.GetOrdinal('Price');
NeedsWalkingOrd := Reader.GetOrdinal('NeedsWalking');
TypeOrd := Reader.GetOrdinal('Type');
LineIdx := FieldedText.HeadingLineCount;
// Read records and write values to grid
while Reader.Read do
begin
StringGrid1.Cells[0, LineIdx] := Reader.GetString(PetNameOrd);
if Reader.IsDBNull(AgeOrd) then
StringGrid1.Cells[1, LineIdx] := '<Null>'
else
StringGrid1.Cells[1, LineIdx] := Reader.GetDouble(AgeOrd).ToString;
StringGrid1.Cells[2, LineIdx] := Reader.GetString(ColorOrd);
StringGrid1.Cells[3, LineIdx] := Reader.GetDateTime(DateReceivedOrd).ToString;
StringGrid1.Cells[4, LineIdx] := Reader.GetDecimal(PriceOrd).ToString;
StringGrid1.Cells[5, LineIdx] := Reader.GetBoolean(NeedsWalkingOrd).ToString;
StringGrid1.Cells[6, LineIdx] := Reader.GetString(TypeOrd);
Inc(LineIdx);
end;
end;
Step 9: Compile and Run
Compile and run the program. When the button is clicked, the program will do the following:
- Create a TFieldedText object
- Load the Meta
- Parse the file once to get headings
- Set up columns and rows in StringGrid
- Read the Headings into StringGrid
- Parse the file again with an IDataReader to get Records
- Place each record into a row in the StringGrid