How to connect a CSV file as a DataSet in Delphi and build a report using FastReport VCL 6

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How to connect a CSV file as a DataSet in Delphi and build a report using FastReport VCL 6
Alexander Syrykh, May 12, 2020
[SHOWTOGROUPS=4,20]
How to connect a CSV file as a DataSet in Delphi and build a report using FastReport VCL 6
Alexander Syrykh, May 12, 2020

Probably many have encountered the problem of using a CSV file as a DataSet in Delphi.
Let's have a look at one of the solutions to this problem. We will connect the CSV file via TADOQuery and build a report from this data in FastReport VCL 6.
Create a project and add the following components.
Код:
OHLC_Query: TADOQuery;
OHLC_Source: TDataSource;
OHLC_DB: TfrxDBDataset;
frxReport1: TfrxReport;
frxDesigner1: TfrxDesigner;
frxChartObject1: TfrxChartObject;
ButtonShowReport: TButton;
ButtonDesignReport: TButton;


FormDemoCSV.PNG


To connect to the CSV file, we will use the OHLC_Query: TADOQuery component.
Set it up by setting the ConnectionString property:
Код:
OHLC_Query.ConnectionString :=’Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\;Extended Properties="text;";Persist Security Info=False’
Next, you will also need to write your Schema.ini file in accordance with the documentation on the Microsoft website:

Для просмотра ссылки Войди или Зарегистрируйся

And add it to the project folder.

Let's analyze the example of our demo CSV file (EURUSD_200201_200410.csv), it has the following structure:

FileCSV.PNG


Data is separated using the “;”
The data is presented in a more visual form, they have 9 columns:

FileCSV2.PNG


Let's create a text file and save it as schema.ini
This file should have the following structure according to the documentation on the Microsoft website:

Для просмотра ссылки Войди или Зарегистрируйся

Код:
[EURUSD_200201_200410.csv]
ColNameHeader=True
Format=Delimited(;)
DecimalSymbol=.
TextDelimiter='
CharacterSet=ANSI
DateTimeFormat=yyyymmdd
Col1=TICKER char
Col2=PER integer
Col3=DATE date
Col4=TIME char
Col5=OPEN float
Col6=HIGH float
Col7=LOW float
Col8=CLOSE float
Col9=VOL integer

Set the necessary settings for OHLC_Source and OHLC_DB:

Код:
OHLC_Source.DataSet := OHLC_Query;
OHLC_DB.DataSource := OHLC_Source;
OHLC_DB.UserName := 'OHLC';

Create a template and save it as DemoCSV.fr3

Design DemoCSV.fr3


And connect the DataSet to the template


Menu.PNG
SelectDB.PNG


Next, add the ButtonDesignReportClick, ButtonShowReportClick, and FormCreate events:

Код:
procedure TFormDemoCSV.ButtonDesignReportClick(Sender: TObject);
begin
 frxReport1.DesignReport;
end;
 
procedure TFormDemoCSV.ButtonShowReportClick(Sender: TObject);
begin
 frxReport1.ShowReport();
end;
 
procedure TFormDemoCSV.FormCreate(Sender: TObject);
begin
 frxReport1.LoadFromFile('./DemoCSV.fr3')
end;

Launch applications


DemoCSV.PNG



When you click on the ButtonShowReport button, a report is built.


ReportDemoCSV.PNG


Congratulations, you included the CSV file as a DataSet in Delphi and built a report from this data in FastReport VCL 6!

Download link: Для просмотра ссылки Войди или Зарегистрируйся
[/SHOWTOGROUPS]