By using TFieldedText to write the CSV file, you do not have to worry about formatting. TFieldedText will ensure that the data in the CSV file is laid out as specified by the Meta.
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: 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>
uses
Xilytix.FieldedText.Main;
procedure TForm1.Button1Click(Sender: TObject);
var
FieldedText: TFieldedText;
PetNameField: TFieldedTextField;
AgeField: TFieldedTextField;
ColorField: TFieldedTextField;
DateReceivedField: TFieldedTextField;
PriceField: TFieldedTextField;
NeedsWalkingField: TFieldedTextField;
TypeField: TFieldedTextField;
Bldr: TStringBuilder;
Writer: TStringWriter;
begin
FieldedText := TFieldedText.Create;
try
// Load the Meta which describes the text file
// A Fielded Text Editor is used to create the Meta File.
FieldedText.LoadMeta('BasicExample.ftm');
// Find Fields
PetNameField := FieldedText.FindField('PetName');
AgeField := FieldedText.FindField('Age');
ColorField := FieldedText.FindField('Color');
DateReceivedField := FieldedText.FindField('DateReceived');
PriceField := FieldedText.FindField('Price');
NeedsWalkingField := FieldedText.FindField('NeedsWalking');
TypeField := FieldedText.FindField('Type');
// Set up Headers
PetNameField.Headings[0] := 'Pet Name';
AgeField.Headings[0] := 'Age';
AgeField.Headings[1] := '(Years)';
ColorField.Headings[0] := 'Color';
DateReceivedField.Headings[0] := 'Date Received';
PriceField.Headings[0] := 'Price';
PriceField.Headings[1] := '(Dollars)';
NeedsWalkingField.Headings[0] := 'Needs Walking';
TypeField.Headings[0] := 'Type';
// Create a writer
Bldr := TStringBuilder.Create;
try
Writer := TStringWriter.Create(Bldr);
try
// Start writing. Header will automatically be written
FieldedText.BeginWrite(Writer);
try
// Set up values for first record
PetNameField.AsString := 'Rover';
AgeField.AsDouble := 4.5;
ColorField.AsString := 'Brown';
DateReceivedField.AsDateTime := EncodeDate(2004, 2, 12);
PriceField.AsCurrency := 80.0;
NeedsWalkingField.AsBoolean := True;
TypeField.AsString := 'Dog';
// Write First Record
FieldedText.WriteRecord;
// Repeat for next 3 records
PetNameField.AsString := 'Charlie';
AgeField.Null := True;
ColorField.AsString := 'Gold';
DateReceivedField.AsDateTime := EncodeDate(2007, 4, 5);
PriceField.AsCurrency := 12.3;
NeedsWalkingField.AsBoolean := False;
TypeField.AsString := 'Fish';
FieldedText.WriteRecord;
PetNameField.AsString := 'Molly';
AgeField.AsDouble := 2.0;
ColorField.AsString := 'Black';
DateReceivedField.AsDateTime := EncodeDate(2006, 12, 12);
PriceField.AsCurrency := 25;
NeedsWalkingField.AsBoolean := False;
TypeField.AsString := 'Cat';
FieldedText.WriteRecord;
PetNameField.AsString := 'Gilly';
AgeField.Null := True;
ColorField.AsString := 'White';
DateReceivedField.AsDateTime := EncodeDate(2007, 4, 10);
PriceField.AsCurrency := 10.0;
NeedsWalkingField.AsBoolean := False;
TypeField.AsString := 'Guinea Pig';
FieldedText.WriteRecord;
// Finish Writing
finally
FieldedText.EndWrite;
end;
Writer.Close;
finally
Writer.Free;
end;
// Place in Memo
Memo1.Text := Bldr.ToString;
finally
Bldr.Free;
end;
finally
FieldedText.Free;
end;
end;