-
Notifications
You must be signed in to change notification settings - Fork 43
/
Import_ByCode.pas
278 lines (228 loc) · 6.91 KB
/
Import_ByCode.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
unit Import_ByCode;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
Vcl.ComCtrls, VCLBI.DataViewer, BI.DataItem, Data.DB, Datasnap.DBClient,
Vcl.Grids, Vcl.DBGrids, VCLBI.DataControl, VCLBI.Grid;
type
TByCode = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Button4: TButton;
Panel3: TPanel;
PageControl1: TPageControl;
TabBIGrid: TTabSheet;
BIGrid1: TBIGrid;
TabStructure: TTabSheet;
LBFormat: TListBox;
Splitter1: TSplitter;
TabSource: TTabSheet;
PageExamples: TPageControl;
TabCSV: TTabSheet;
MemoCSV: TMemo;
TabJSON: TTabSheet;
MemoJSON: TMemo;
TabXML: TTabSheet;
MemoXML: TMemo;
TabDatabase: TTabSheet;
DBGrid1: TDBGrid;
ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
TabHTML: TTabSheet;
MemoHTML: TMemo;
procedure LBFormatClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
DataViewer : TDataViewer;
// Just a helper method
function CreateData(const AName:String; const AData:TDataArray):TDataItem;
procedure ImportCSV;
procedure ImportDatabase;
procedure ImportExcel;
procedure ImportHTML;
procedure ImportJSON;
procedure ImportXML;
public
{ Public declarations }
class procedure Show(const AOwner:TComponent); static;
end;
implementation
{$R *.dfm}
uses
BI.CSV, BI.JSON, BI.XMLData, BI.Excel, BI.DB,
BI.DataSource, BI.HTML,
FindSampleData;
{ TByCode }
// Helper method to create a TDataItem from an array of TDataItem
procedure TByCode.Button4Click(Sender: TObject);
begin
Close;
end;
function TByCode.CreateData(const AName:String; const AData:TDataArray):TDataItem;
begin
result:=TBISource.FromData(AData);
if result<>nil then
result.Name:=AName;
end;
procedure TByCode.FormCreate(Sender: TObject);
begin
PageControl1.ActivePage:=TabBIGrid;
PageExamples.ActivePage:=TabCSV;
// Create DataViewer
DataViewer:=TDataViewer.Embedd(Self,TabStructure,nil);
end;
procedure TByCode.FormDestroy(Sender: TObject);
begin
// Destroy data (to avoid memory leaks)
BIGrid1.DestroyData;
end;
procedure TByCode.ImportCSV;
var CSV : TBICSV;
begin
CSV:=TBICSV.Create;
try
CSV.Delimiter:=','; // <-- Field separator, default is comma
CSV.Quote:='"'; // <-- Text fields quotes, default is "
CSV.Header.Headers:=TTextHeaders.Auto; // <-- Detect first row as headers
// CSV.Header.Count:=1; // <-- Number of header lines when Headers=Yes
CSV.MissingValue:='NA'; // <-- Content that should be considered a "null" value
CSV.ZeroOneAsBoolean:=True; // <-- When True, CSV 0 and 1 are considered Booleans instead of Integers
// Class methods for one-line of code importing :
{
Data:=TBICSV.FromText('A,B,C'+#13#10+'1,2,3');
Data:=TBICSV.FromFile('test.csv');
Data:=TBICSV.FromStrings(Memo1.Lines);
}
// Choose the origin of the CSV content:
{
CSV.Import('c:\myfolder', True); // <-- recursive import from a folder files
CSV.Import(Memo1.Lines); // <-- import text from TStrings
CSV.ImportFile('test.csv'); // <-- import from a single file
CSV.ImportText('A,B'+#13#10+'1,2'); // <-- import from a string
CSV.ImportURL('http://acme.com/data.csv'); // <-- load from a web URL
}
BIGrid1.Data:=CSV.ImportText(MemoCSV.Text);
finally
CSV.Free;
end;
end;
procedure TByCode.ImportDatabase;
var DB : TBIDB;
begin
DB:=TBIDB.Create; // CreateEngine allows specifiying a different database (FireDAC, UniDAC, etc, etc)
try
// DB.Import(FDConnection1); // <-- Import all datasets from a database Connection
// Data:=TBIDB.FromDataSet( FDQuery1 ); <-- class method from any TDataset
DB.ExcludePattern:='SYS*'; // <-- To exclude datasets that match pattern
BIGrid1.Data:=DB.Import(ClientDataSet1);
finally
DB.Free;
end;
end;
procedure TByCode.ImportExcel;
var Excel : TBIExcel;
begin
Excel:=TBIExcel.Create;
try
Excel.HeaderCount:=2; // <-- Number of Rows that contain column headers
// Excel.Range:='A1:B14'; // <-- Optional, define portion of Excel sheet
// Excel.WorkSheet:='Another Sheet'; // <-- Optional, select a single sheet to import
BIGrid1.Data:=CreateData('Excel Book1', Excel.ImportFile(BISamplesFolder+'\Other\Excel Book1.xls'));
// Other methods:
{
Data:=Excel.Import('c:\myexcel',True); // <-- recursive import of Excel files
Data:=Excel.ImportURL('http://acme.com/myexcel.xlsx');
Data:=Excel.ImportFile('mysheet.xls');
// Class method
Data:=TBIExcel.FromFile('mysheet.xls');
}
finally
Excel.Free;
end;
end;
procedure TByCode.ImportJSON;
var JSON : TBIJSON;
begin
JSON:=TBIJSON.Create;
try
JSON.Format:=TBIJSONFormat.Normal; // <-- "Array" format for MongoDB special row-by-row json
JSON.MissingValue:='NA'; // <-- Content that should be considered a "null" value
JSON.ZeroOneAsBoolean:=True; // <-- When True, CSV 0 and 1 are considered Booleans instead of Integers
BIGrid1.Data:=CreateData('JSON Data',JSON.Import(MemoJSON.Lines));
finally
JSON.Free;
end;
end;
procedure TByCode.ImportXML;
var XML : TBIXML;
begin
XML:=TBIXML.Create; // <-- CreateEngine to pass a different XML DOM library
try
XML.ExcludePattern:='name';
XML.MissingValue:='NA'; // <-- Content that should be considered a "null" value
XML.ZeroOneAsBoolean:=True; // <-- When True, CSV 0 and 1 are considered Booleans instead of Integers
BIGrid1.Data:=XML.ImportText(MemoXML.Text);
finally
XML.Free;
end;
end;
procedure TByCode.ImportHTML;
var HTML : TBIHTML;
begin
HTML:=TBIHTML.Create;
try
BIGrid1.Data:=HTML.FromText(MemoHTML.Text);
finally
HTML.Free;
end;
end;
procedure TByCode.LBFormatClick(Sender: TObject);
begin
if LBFormat.ItemIndex<>-1 then
begin
// Destroy previous data (to avoid memory leak)
BIGrid1.DestroyData;
case LBFormat.ItemIndex of
0: begin
PageExamples.ActivePage:=TabCSV;
ImportCSV;
end;
1: begin
PageExamples.ActivePage:=TabJSON;
ImportJSON;
end;
2: begin
PageExamples.ActivePage:=TabXML;
ImportXML;
end;
3: begin
PageExamples.ActivePage:=TabDatabase;
ImportDatabase;
end;
4: begin
PageExamples.ActivePage:=TabHTML;
ImportHTML;
end;
else
ImportExcel;
end;
// Show new data structure at data viewer
if DataViewer<>nil then
DataViewer.Select(BIGrid1.Data);
end;
end;
// Show this form modal
class procedure TByCode.Show(const AOwner: TComponent);
begin
with TByCode.Create(AOwner) do
try
ShowModal;
finally
Free;
end;
end;
end.