unit CsvCiewer.Forms.MainForm;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils, System.Variants, System.Classes, System.Actions,
System.ImageList, System.Diagnostics,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.ComCtrls,
Vcl.ActnList,
Vcl.StdActns,
Vcl.ImgList,
Vcl.ToolWin,
Spring, CsvViewer.Classes;
type
TMainForm =
class( TForm )
ListView1: TListView;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ActionList1: TActionList;
ImageList1: TImageList;
FileOpen1: TFileOpen;
StatusBar1: TStatusBar;
procedure FileOpen1_Accept( Sender: TObject );
procedure ListView1_Data( Sender: TObject; Item: TListItem );
private
FCsvReader: IShared<TCsvReader>;
FCsvContent: IShared<TCsvContent>;
public
procedure AfterConstruction;
override;
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
{ TMainForm }
procedure TMainForm.AfterConstruction;
begin
inherited;
FCsvReader := Shared.New( TCsvReader.Create );
FCsvReader.FirstRowContainsHeader := True;
end;
procedure TMainForm.FileOpen1_Accept( Sender: TObject );
var
col: TListColumn;
loadingStopwatch, presentingStopwatch: TStopwatch;
begin
loadingStopwatch := TStopwatch.StartNew( );
FCsvContent := FCsvReader.
Read( FileOpen1.Dialog.FileName );
loadingStopwatch.Stop( );
presentingStopwatch := TStopwatch.StartNew();
ListView1.Items.BeginUpdate( );
try
ListView1.Items.Count := 0;
ListView1.Columns.Clear;
while ListView1.Columns.Count < Length( FCsvContent.Items[0] )
do
begin
col := ListView1.Columns.Add;
if FCsvContent.HasHeader
then
col.Caption := FCsvContent.Header[col.
Index]
else
col.Caption := '
Field ' + ( col.
Index + 1 ).ToString( );
end;
ListView1.Items.Count := FCsvContent.Count;
finally
ListView1.Items.EndUpdate( );
end;
presentingStopwatch.Stop();
StatusBar1.SimpleText := loadingStopwatch.ElapsedMilliseconds.ToString() + '
ms - '+presentingStopwatch.ElapsedMilliseconds.ToString()+'
ms';
end;
procedure TMainForm.ListView1_Data( Sender: TObject; Item: TListItem );
var
rowData: TArray<
string>;
idx: Integer;
begin
rowData := FCsvContent.Items[Item.
Index];
Item.Caption := rowData[0];
for idx := 1
to High( rowData )
do
begin
Item.SubItems.Add( rowData[idx] );
end;
end;
end.