Tutorial: Write a Basic CSV File with Delphi.Net
This tutorial demonstrates how to use the Xilytix TFieldedText component to write headings and records to a CSV file.
The 2 headings and 4 records to be written to the file, are shown in the table below:
| 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 |
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 (normally in the bin\Debug folder beneath the project folder).
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 3: 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 4: Reference the FieldedText Assembly
Add a reference to the Xilytix.FieldedText.dll assembly. The assembly can be downloaded from here.
Step 5: Place controls on Form
Add the following items to a form in the project:
- A TMemo with Name = "Memo1"
- A Button
Step 6: 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,
Borland.Vcl.StdCtrls,
System.ComponentModel;
...
implementation
uses
System.Text,
System.IO,
Xilytix.FieldedText.Main;
Step 7: Button Click event
Add the following code to the Click event of the button:
var
FieldedText: TFieldedText;
PetNameIdx: Integer;
AgeIdx: Integer;
ColorIdx: Integer;
DateReceivedIdx: Integer;
PriceIdx: Integer;
NeedsWalkingIdx: Integer;
TypeIdx: Integer;
Bldr: StringBuilder;
Writer: StringWriter;
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');
// Find Field Indices
PetNameIdx := FieldedText.IndexOfField('PetName');
AgeIdx := FieldedText.IndexOfField('Age');
ColorIdx := FieldedText.IndexOfField('Color');
DateReceivedIdx := FieldedText.IndexOfField('DateReceived');
PriceIdx := FieldedText.IndexOfField('Price');
NeedsWalkingIdx := FieldedText.IndexOfField('NeedsWalking');
TypeIdx := FieldedText.IndexOfField('Type');
// Set up Headers
FieldedText.Fields[PetNameIdx].Headings[0] := 'Pet Name';
FieldedText.Fields[AgeIdx].Headings[0] := 'Age';
FieldedText.Fields[AgeIdx].Headings[1] := '(Years)';
FieldedText.Fields[ColorIdx].Headings[0] := 'Color';
FieldedText.Fields[DateReceivedIdx].Headings[0] := 'Date Received';
FieldedText.Fields[PriceIdx].Headings[0] := 'Price';
FieldedText.Fields[PriceIdx].Headings[1] := '(Dollars)';
FieldedText.Fields[NeedsWalkingIdx].Headings[0] := 'Needs Walking';
FieldedText.Fields[TypeIdx].Headings[0] := 'Type';
// Create a writer
Bldr := StringBuilder.Create;
Writer := StringWriter.Create(Bldr);
// Start writing. Header will automatically be written
FieldedText.BeginWrite(Writer);
// Set up values for first record
FieldedText.Fields[PetNameIdx].AsString := 'Rover';
FieldedText.Fields[AgeIdx].AsDouble := 4.5;
FieldedText.Fields[ColorIdx].AsString := 'Brown';
FieldedText.Fields[DateReceivedIdx].AsDateTime := DateTime.Create(2004, 2, 12);
FieldedText.Fields[PriceIdx].AsDecimal := 80.0;
FieldedText.Fields[NeedsWalkingIdx].AsBoolean := True;
FieldedText.Fields[TypeIdx].AsString := 'Dog';
// Write First Record
FieldedText.WriteRecord;
// Repeat for next 3 records
FieldedText.Fields[PetNameIdx].AsString := 'Charlie';
FieldedText.Fields[AgeIdx].Null := True;
FieldedText.Fields[ColorIdx].AsString := 'Gold';
FieldedText.Fields[DateReceivedIdx].AsDateTime := DateTime.Create(2007, 4, 5);
FieldedText.Fields[PriceIdx].AsDecimal := 12.3;
FieldedText.Fields[NeedsWalkingIdx].AsBoolean := False;
FieldedText.Fields[TypeIdx].AsString := 'Fish';
FieldedText.WriteRecord;
FieldedText.Fields[PetNameIdx].AsString := 'Molly';
FieldedText.Fields[AgeIdx].AsDouble := 2.0;
FieldedText.Fields[ColorIdx].AsString := 'Black';
FieldedText.Fields[DateReceivedIdx].AsDateTime := DateTime.Create(2006, 12, 12);
FieldedText.Fields[PriceIdx].AsDecimal := 25;
FieldedText.Fields[NeedsWalkingIdx].AsBoolean := False;
FieldedText.Fields[TypeIdx].AsString := 'Cat';
FieldedText.WriteRecord;
FieldedText.Fields[PetNameIdx].AsString := 'Gilly';
FieldedText.Fields[AgeIdx].Null := True;
FieldedText.Fields[ColorIdx].AsString := 'White';
FieldedText.Fields[DateReceivedIdx].AsDateTime := DateTime.Create(2007, 4, 10);
FieldedText.Fields[PriceIdx].AsDecimal := 10.0;
FieldedText.Fields[NeedsWalkingIdx].AsBoolean := False;
FieldedText.Fields[TypeIdx].AsString := 'Guinea Pig';
FieldedText.WriteRecord;
// Finish Writing
FieldedText.EndWrite;
Writer.Close;
// Place in Memo
Memo1.Text := Bldr.ToString;
end;
Step 8: 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
- Find the indices of the fields
- Set up the headings in the FieldedText object
- Create a StringWriter to write text to a StringBuilder
- Use FieldedText.BeginWrite to initiate writing of text. This call will automatically write the headings.
- Write the records
- Use FieldedText.EndWrite to finish the writing process
- Place generated string in the Memo control