My favorites | Sign in
Project Home Issues Source
Checkout   Browse   Changes  
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
unit exportgrid;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus, ComCtrls, VirtualTrees, SynExportHTML;

type
TGridExportFormat = (efExcel, efCSV, efHTML, efXML, efSQLInsert, efSQLReplace, efLaTeX, efWiki, efPHPArray);

TfrmExportGrid = class(TForm)
btnOK: TButton;
btnCancel: TButton;
grpFormat: TRadioGroup;
grpSelection: TRadioGroup;
grpOutput: TGroupBox;
radioOutputCopyToClipboard: TRadioButton;
radioOutputFile: TRadioButton;
editFilename: TButtonedEdit;
grpOptions: TGroupBox;
chkIncludeColumnNames: TCheckBox;
editSeparator: TButtonedEdit;
editEncloser: TButtonedEdit;
editTerminator: TButtonedEdit;
lblSeparator: TLabel;
lblEncloser: TLabel;
lblTerminator: TLabel;
popupCSVchar: TPopupMenu;
menuCSVtab: TMenuItem;
menuCSVunixlinebreak: TMenuItem;
menuCSVmaclinebreak: TMenuItem;
menuCSVwinlinebreak: TMenuItem;
menuCSVnul: TMenuItem;
menuCSVbackspace: TMenuItem;
menuCSVcontrolz: TMenuItem;
comboEncoding: TComboBox;
lblEncoding: TLabel;
popupRecentFiles: TPopupMenu;
menuCSVsinglequote: TMenuItem;
menuCSVdoublequote: TMenuItem;
menuCSVcomma: TMenuItem;
menuCSVsemicolon: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
chkIncludeAutoIncrement: TCheckBox;
chkIncludeQuery: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure CalcSize(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure editFilenameRightButtonClick(Sender: TObject);
procedure editFilenameChange(Sender: TObject);
procedure SelectRecentFile(Sender: TObject);
procedure popupRecentFilesPopup(Sender: TObject);
procedure menuCSVClick(Sender: TObject);
procedure editCSVRightButtonClick(Sender: TObject);
procedure editCSVChange(Sender: TObject);
procedure ValidateControls(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure grpFormatClick(Sender: TObject);
private
{ Private declarations }
FCSVEditor: TButtonedEdit;
FCSVSeparator, FCSVEncloser, FCSVTerminator: String;
FGrid: TVirtualStringTree;
FRecentFiles: TStringList;
const FFormatToFileExtension: Array[TGridExportFormat] of String =
(('csv'), ('csv'), ('html'), ('xml'), ('sql'), ('sql'), ('LaTeX'), ('wiki'), ('php'));
const FFormatToDescription: Array[TGridExportFormat] of String =
(('Excel CSV'), ('Delimited text'), ('HTML table'), ('XML'), ('SQL INSERTs'), ('SQL REPLACEs'), ('LaTeX'), ('Wiki markup'), ('PHP Array'));
procedure SaveDialogTypeChange(Sender: TObject);
function GetExportFormat: TGridExportFormat;
procedure SetExportFormat(Value: TGridExportFormat);
procedure SetExportFormatByFilename;
public
{ Public declarations }
property Grid: TVirtualStringTree read FGrid write FGrid;
property ExportFormat: TGridExportFormat read GetExportFormat write SetExportFormat;
end;


implementation

uses main, helpers, dbconnection, mysql_structures;

{$R *.dfm}



procedure TfrmExportGrid.FormCreate(Sender: TObject);
var
FormatDesc: String;
begin
InheritFont(Font);
editFilename.Text := AppSettings.ReadString(asGridExportFilename);
radioOutputCopyToClipboard.Checked := AppSettings.ReadBool(asGridExportOutputCopy);
radioOutputFile.Checked := AppSettings.ReadBool(asGridExportOutputFile);
FRecentFiles := Explode(DELIM, AppSettings.ReadString(asGridExportRecentFiles));
comboEncoding.Items.Assign(MainForm.FileEncodings);
comboEncoding.Items.Delete(0); // Remove "Auto detect"
comboEncoding.ItemIndex := AppSettings.ReadInt(asGridExportEncoding);
grpFormat.Items.Clear;
for FormatDesc in FFormatToDescription do
grpFormat.Items.Add(FormatDesc);
grpFormat.ItemIndex := AppSettings.ReadInt(asGridExportFormat);
grpSelection.ItemIndex := AppSettings.ReadInt(asGridExportSelection);
chkIncludeColumnNames.Checked := AppSettings.ReadBool(asGridExportColumnNames);
chkIncludeQuery.Checked := AppSettings.ReadBool(asGridExportIncludeQuery);
FCSVSeparator := AppSettings.ReadString(asGridExportSeparator);
FCSVEncloser := AppSettings.ReadString(asGridExportEncloser);
FCSVTerminator := AppSettings.ReadString(asGridExportTerminator);
ValidateControls(Sender);
end;


procedure TfrmExportGrid.FormDestroy(Sender: TObject);
begin
// Store settings
if ModalResult = mrOK then begin
AppSettings.WriteBool(asGridExportOutputCopy, radioOutputCopyToClipboard.Checked);
AppSettings.WriteBool(asGridExportOutputFile, radioOutputFile.Checked);
AppSettings.WriteString(asGridExportFilename, editFilename.Text);
AppSettings.WriteString(asGridExportRecentFiles, ImplodeStr(DELIM, FRecentFiles));
AppSettings.WriteInt(asGridExportEncoding, comboEncoding.ItemIndex);
AppSettings.WriteInt(asGridExportFormat, grpFormat.ItemIndex);
AppSettings.WriteInt(asGridExportSelection, grpSelection.ItemIndex);
AppSettings.WriteBool(asGridExportColumnNames, chkIncludeColumnNames.Checked);
AppSettings.WriteBool(asGridExportIncludeAutoInc, chkIncludeAutoIncrement.Checked);
AppSettings.WriteBool(asGridExportIncludeQuery, chkIncludeQuery.Checked);
AppSettings.WriteString(asGridExportSeparator, FCSVSeparator);
AppSettings.WriteString(asGridExportEncloser, FCSVEncloser);
AppSettings.WriteString(asGridExportTerminator, FCSVTerminator);
end;
end;


procedure TfrmExportGrid.FormShow(Sender: TObject);
begin
// Show dialog. Expect "Grid" property to be set now by the caller.
chkIncludeAutoIncrement.Checked := AppSettings.ReadBool(asGridExportIncludeAutoInc);
CalcSize(Sender);
end;


procedure TfrmExportGrid.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// Destroy dialog - not cached
Action := caFree;
end;


procedure TfrmExportGrid.ValidateControls(Sender: TObject);
var
Enable: Boolean;
begin
// Display the actually used control characters, even if they cannot be changed
case ExportFormat of
efExcel: begin
// Tab for pasting, semicolon if comma is also the decimal separator, and comma for the rest
// see http://en.wikipedia.org/wiki/Comma-separated_values
if radioOutputCopyToClipboard.Checked then
editSeparator.Text := '\t'
else if FormatSettings.DecimalSeparator=',' then
editSeparator.Text := ';'
else
editSeparator.Text := ',';
editEncloser.Text := '"';
editTerminator.Text := '\r\n';
end;
efCSV: begin
editSeparator.Text := FCSVSeparator;
editEncloser.Text := FCSVEncloser;
editTerminator.Text := FCSVTerminator;
end;
else begin
editSeparator.Text := '';
editEncloser.Text := '';
editTerminator.Text := '';
end;
end;

chkIncludeQuery.Enabled := ExportFormat in [efHTML, efXML];
Enable := ExportFormat = efCSV;
lblSeparator.Enabled := Enable;
editSeparator.Enabled := Enable;
editSeparator.RightButton.Enabled := Enable;
lblEncloser.Enabled := Enable;
editEncloser.Enabled := Enable;
editEncloser.RightButton.Enabled := Enable;
lblTerminator.Enabled := Enable;
editTerminator.Enabled := Enable;
editTerminator.RightButton.Enabled := Enable;
btnOK.Enabled := radioOutputCopyToClipboard.Checked or (radioOutputFile.Checked and (editFilename.Text <> ''));
if radioOutputFile.Checked then
editFilename.Font.Color := clWindowText
else
editFilename.Font.Color := clGrayText;
comboEncoding.Enabled := radioOutputFile.Checked;
lblEncoding.Enabled := radioOutputFile.Checked;
if ExportFormat = efExcel then
comboEncoding.ItemIndex := comboEncoding.Items.IndexOf('ANSI');
end;


function TfrmExportGrid.GetExportFormat: TGridExportFormat;
begin
Result := TGridExportFormat(grpFormat.ItemIndex);
end;


procedure TfrmExportGrid.SetExportFormat(Value: TGridExportFormat);
begin
grpFormat.ItemIndex := Integer(Value);
end;


procedure TfrmExportGrid.grpFormatClick(Sender: TObject);
var
Filename: String;
begin
// Auto-modify file extension when selecting export format
// Be careful about triggering editFilename.OnChange event, as we may have come here from that event!
if radioOutputFile.Checked then begin
Filename := ExtractFilePath(editFilename.Text) +
ExtractBaseFileName(editFilename.Text) +
'.' + FFormatToFileExtension[ExportFormat];
if CompareText(Filename, editFilename.Text) <> 0 then
editFilename.Text := Filename;
end;
ValidateControls(Sender);
end;


procedure TfrmExportGrid.SetExportFormatByFilename;
var
ext: String;
efrm: TGridExportFormat;
begin
// Set format by file extension
ext := LowerCase(Copy(ExtractFileExt(editFilename.Text), 2, 10));
for efrm :=Low(TGridExportFormat) to High(TGridExportFormat) do begin
if ext = FFormatToFileExtension[ExportFormat] then
break;
if ext = FFormatToFileExtension[efrm] then begin
ExportFormat := efrm;
break;
end;
end;
end;


procedure TfrmExportGrid.editFilenameChange(Sender: TObject);
begin
radioOutputFile.Checked := True;
end;


procedure TfrmExportGrid.editFilenameRightButtonClick(Sender: TObject);
var
Dialog: TSaveDialog;
ef: TGridExportFormat;
begin
// Select file target
Dialog := TSaveDialog.Create(Self);
Dialog.InitialDir := ExtractFilePath(editFilename.Text);
Dialog.FileName := ExtractBaseFileName(editFilename.Text);
Dialog.Filter := '';
for ef:=Low(TGridExportFormat) to High(TGridExportFormat) do
Dialog.Filter := Dialog.Filter + FFormatToDescription[ef] + ' (*.'+FFormatToFileExtension[ef]+')|*.'+FFormatToFileExtension[ef]+'|';
Dialog.Filter := Dialog.Filter + 'All files (*.*)|*.*';
Dialog.OnTypeChange := SaveDialogTypeChange;
Dialog.FilterIndex := grpFormat.ItemIndex+1;
Dialog.OnTypeChange(Dialog);
if Dialog.Execute then begin
editFilename.Text := Dialog.FileName;
SetExportFormatByFilename;
end;
Dialog.Free;
end;


procedure TfrmExportGrid.popupRecentFilesPopup(Sender: TObject);
var
Filename: String;
Menu: TPopupMenu;
Item: TMenuItem;
begin
// Clear and populate drop down menu with recent files
Menu := Sender as TPopupMenu;
Menu.Items.Clear;
for Filename in FRecentFiles do begin
Item := TMenuItem.Create(Menu);
Menu.Items.Add(Item);
Item.Caption := Filename;
Item.Hint := Filename;
Item.OnClick := SelectRecentFile;
Item.Checked := Filename = editFilename.Text;
end;
end;


procedure TfrmExportGrid.SelectRecentFile(Sender: TObject);
begin
// Select file from recently used files
editFilename.Text := (Sender as TMenuItem).Hint;
SetExportFormatByFilename;
end;


procedure TfrmExportGrid.CalcSize(Sender: TObject);
var
GridData: TDBQuery;
Node: PVirtualNode;
Col, ExcludeCol: TColumnIndex;
RowNum: PCardinal;
SelectionSize, AllSize: Int64;
begin
GridData := Mainform.GridResult(Grid);
AllSize := 0;
SelectionSize := 0;
chkIncludeAutoIncrement.Enabled := GridData.AutoIncrementColumn > -1;
ExcludeCol := -1;
if chkIncludeAutoIncrement.Enabled and (not chkIncludeAutoIncrement.Checked) then
ExcludeCol := GridData.AutoIncrementColumn;

Node := GetNextNode(Grid, nil, False);
while Assigned(Node) do begin
RowNum := Grid.GetNodeData(Node);
GridData.RecNo := RowNum^;
Col := Grid.Header.Columns.GetFirstVisibleColumn;
while Col > NoColumn do begin
if Col <> ExcludeCol then begin
Inc(AllSize, GridData.ColumnLengths(Col));
if vsSelected in Node.States then
Inc(SelectionSize, GridData.ColumnLengths(Col));
end;
Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
end;
Node := GetNextNode(Grid, Node, False);
end;
grpSelection.Items[0] := 'Selection ('+FormatNumber(Grid.SelectedCount)+' rows, '+FormatByteNumber(SelectionSize)+')';
grpSelection.Items[1] := 'Complete ('+FormatNumber(Grid.RootNodeCount)+' rows, '+FormatByteNumber(AllSize)+')';
end;


procedure TfrmExportGrid.editCSVChange(Sender: TObject);
var
Edit: TButtonedEdit;
begin
// Remember csv settings
Edit := Sender as TButtonedEdit;
if ExportFormat = efCSV then begin
if Edit = editSeparator then
FCSVSeparator := Edit.Text
else if Edit = editEncloser then
FCSVEncloser := Edit.Text
else if Edit = editTerminator then
FCSVTerminator := Edit.Text;
end;
end;


procedure TfrmExportGrid.SaveDialogTypeChange(Sender: TObject);
var
Dialog: TSaveDialog;
ef: TGridExportFormat;
begin
// Set default file-extension of saved file and options on the dialog to show
Dialog := Sender as TSaveDialog;
for ef:=Low(TGridExportFormat) to High(TGridExportFormat) do begin
if Dialog.FilterIndex = Integer(ef)+1 then
Dialog.DefaultExt := FFormatToFileExtension[ef];
end;
end;


procedure TfrmExportGrid.editCSVRightButtonClick(Sender: TObject);
var
p: TPoint;
Item: TMenuItem;
begin
// Remember editor and prepare popup menu items
FCSVEditor := Sender as TButtonedEdit;
p := FCSVEditor.ClientToScreen(FCSVEditor.ClientRect.BottomRight);
for Item in popupCSVchar.Items do begin
Item.OnClick := menuCSVClick;
Item.Checked := FCSVEditor.Text = Item.Hint;
end;
popupCSVchar.Popup(p.X-16, p.Y);
end;


procedure TfrmExportGrid.menuCSVClick(Sender: TObject);
begin
// Insert char from menu
FCSVEditor.Text := TMenuItem(Sender).Hint;
end;


procedure TfrmExportGrid.btnOKClick(Sender: TObject);
var
Col, ExcludeCol: TColumnIndex;
Header, Data, tmp, Encloser, Separator, Terminator, TableName: String;
Node: PVirtualNode;
GridData: TDBQuery;
SelectionOnly: Boolean;
i: Integer;
NodeCount: Cardinal;
RowNum: PCardinal;
HTML: TStream;
S: TStringStream;
Exporter: TSynExporterHTML;
Encoding: TEncoding;
begin
// Confirmation dialog if file exists
if radioOutputFile.Checked
and FileExists(editFilename.Text)
and (MessageDialog('File exists', 'Overwrite file '+editFilename.Text+'?', mtConfirmation, [mbYes, mbCancel]) = mrCancel)
then begin
ModalResult := mrNone;
Exit;
end;

Screen.Cursor := crHourglass;

SelectionOnly := grpSelection.ItemIndex = 0;
Mainform.DataGridEnsureFullRows(Grid, SelectionOnly);
GridData := Mainform.GridResult(Grid);
if SelectionOnly then
NodeCount := Grid.SelectedCount
else
NodeCount := Grid.RootNodeCount;
MainForm.EnableProgress(NodeCount);
TableName := BestTableName(GridData);
ExcludeCol := -1;
if (not chkIncludeAutoIncrement.Checked) or (not chkIncludeAutoIncrement.Enabled) then
ExcludeCol := GridData.AutoIncrementColumn;

if radioOutputCopyToClipboard.Checked then
Encoding := TEncoding.UTF8
else begin
Encoding := MainForm.GetEncodingByName(comboEncoding.Text);
// Add selected file to file list, and sort it onto the top of the list
i := FRecentFiles.IndexOf(editFilename.Text);
if i > -1 then
FRecentFiles.Delete(i);
FRecentFiles.Insert(0, editFilename.Text);
for i:=FRecentFiles.Count-1 downto 10 do
FRecentFiles.Delete(i);
end;
S := TStringStream.Create(Header, Encoding);
Header := '';
case ExportFormat of
efHTML: begin
Header :=
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ' + CRLF +
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + CRLF + CRLF +
'<html>' + CRLF +
' <head>' + CRLF +
' <title>' + TableName + '</title>' + CRLF +
' <meta name="GENERATOR" content="'+ APPNAME+' '+Mainform.AppVersion + '">' + CRLF +
' <meta http-equiv="Content-Type" content="text/html; charset='+GetHTMLCharsetByEncoding(Encoding)+'" />' + CRLF +
' <style type="text/css">' + CRLF +
' thead tr {background-color: ActiveCaption; color: CaptionText;}' + CRLF +
' th, td {vertical-align: top; font-family: "'+Grid.Font.Name+'"; font-size: '+IntToStr(Grid.Font.Size)+'pt; padding: '+IntToStr(Grid.TextMargin-1)+'px; }' + CRLF +
' table, td {border: 1px solid silver;}' + CRLF +
' table {border-collapse: collapse;}' + CRLF;
Col := Grid.Header.Columns.GetFirstVisibleColumn;
while Col > NoColumn do begin
// Adjust preferred width of columns.
Header := Header +
' thead .col' + IntToStr(Col) + ' {width: ' + IntToStr(Grid.Header.Columns[Col].Width) + 'px;}' + CRLF;
// Right-justify all cells to match the grid on screen.
if Grid.Header.Columns[Col].Alignment = taRightJustify then
Header := Header + ' .col' + IntToStr(Col) + ' {text-align: right;}' + CRLF;
Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
end;
Header := Header +
' </style>' + CRLF +
' </head>' + CRLF + CRLF +
' <body>' + CRLF + CRLF;
if chkIncludeQuery.Checked then
Header := Header + '<p style="font-family: monospace; white-space: pre;">' + GridData.SQL + '</p>' + CRLF + CRLF;
Header := Header + ' <table caption="' + TableName + ' (' + inttostr(NodeCount) + ' rows)">' + CRLF;
if chkIncludeColumnNames.Checked then begin
Header := Header +
' <thead>' + CRLF +
' <tr>' + CRLF;
Col := Grid.Header.Columns.GetFirstVisibleColumn;
while Col > NoColumn do begin
if Col <> ExcludeCol then
Header := Header + ' <th class="col' + IntToStr(Col) + '">' + Grid.Header.Columns[Col].Text + '</th>' + CRLF;
Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
end;
Header := Header +
' </tr>' + CRLF +
' </thead>' + CRLF;
end;
Header := Header +
' <tbody>' + CRLF;
end;

efExcel, efCSV: begin
Separator := GridData.Connection.UnescapeString(editSeparator.Text);
Encloser := GridData.Connection.UnescapeString(editEncloser.Text);
Terminator := GridData.Connection.UnescapeString(editTerminator.Text);
if chkIncludeColumnNames.Checked then begin
Col := Grid.Header.Columns.GetFirstVisibleColumn;
while Col > NoColumn do begin
// Alter column name in header if data is not raw.
if Col <> ExcludeCol then begin
Data := Grid.Header.Columns[Col].Text;
if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then
Data := 'HEX(' + Data + ')';
// Add header item.
if Header <> '' then
Header := Header + Separator;
Header := Header + Encloser + Data + Encloser;
end;
Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
end;
Header := Header + Terminator;
end;
end;

efXML: begin
// Imitate mysqldump's XML style
Header := '<?xml version="1.0" encoding="'+MainForm.GetCharsetByEncoding(Encoding)+'"?>' + CRLF + CRLF;
if chkIncludeQuery.Checked then
Header := Header + '<resultset statement="'+HTMLSpecialChars(GridData.SQL)+'" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' + CRLF
else
Header := Header + '<table_data name="'+HTMLSpecialChars(TableName)+'">' + CRLF;
end;

efLaTeX: begin
Header := '\begin{tabular}';
Separator := ' & ';
Encloser := '';
Terminator := '\\ '+CRLF;
Header := Header + '{';
Col := Grid.Header.Columns.GetFirstVisibleColumn;
while Col > NoColumn do begin
if Col <> ExcludeCol then
Header := Header + ' c ';
Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
end;
Header := Header + '}' + CRLF;
if chkIncludeColumnNames.Checked then begin
Col := Grid.Header.Columns.GetFirstVisibleColumn;
while Col > NoColumn do begin
if Col <> ExcludeCol then
Header := Header + Grid.Header.Columns[Col].Text + Separator;
Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
end;
Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator));
Header := Header + Terminator;
end;
end;

efWiki: begin
Separator := ' || ';
Encloser := '';
Terminator := ' ||'+CRLF;
if chkIncludeColumnNames.Checked then begin
Header := '|| ';
Col := Grid.Header.Columns.GetFirstVisibleColumn;
while Col > NoColumn do begin
if Col <> ExcludeCol then
Header := Header + '*' + Grid.Header.Columns[Col].Text + '*' + Separator;
Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
end;
Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator));
Header := Header + Terminator;
end;
end;

efPHPArray: begin
if radioOutputFile.Checked then
Header := '<?php'+CRLF+'$'+TableName+' = array('+CRLF
else
Header := '$'+TableName+' = array('+CRLF;
end;

end;
S.WriteString(Header);

Node := GetNextNode(Grid, nil, SelectionOnly);
while Assigned(Node) do begin
// Update status once in a while.
if (Node.Index+1) mod 100 = 0 then begin
Mainform.ShowStatusMsg('Exporting row '+FormatNumber(Node.Index+1)+' of '+FormatNumber(NodeCount)+
' ('+IntToStr(Trunc((Node.Index+1) / NodeCount *100))+'%, '+FormatByteNumber(S.Size)+')'
);
MainForm.ProgressStep;
end;
RowNum := Grid.GetNodeData(Node);
GridData.RecNo := RowNum^;

// Row preamble
case ExportFormat of
efHTML: tmp := ' <tr>' + CRLF;

efXML: tmp := #9'<row>' + CRLF;

efSQLInsert, efSQLReplace: begin
if ExportFormat = efSQLInsert then
tmp := 'INSERT'
else
tmp := 'REPLACE';
tmp := tmp + ' INTO '+GridData.Connection.QuoteIdent(Tablename);
if chkIncludeColumnNames.Checked then begin
tmp := tmp + ' (';
Col := Grid.Header.Columns.GetFirstVisibleColumn;
while Col > NoColumn do begin
if Col <> ExcludeCol then
tmp := tmp + GridData.Connection.QuoteIdent(Grid.Header.Columns[Col].Text)+', ';
Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
end;
Delete(tmp, Length(tmp)-1, 2);
tmp := tmp + ')';
end;
tmp := tmp + ' VALUES (';
end;

efWiki: tmp := TrimLeft(Separator);

efPHPArray: tmp := #9 + 'array( // row #'+FormatNumber(GridData.RecNo)+CRLF;

else tmp := '';
end;

Col := Grid.Header.Columns.GetFirstVisibleColumn;
while Col > NoColumn do begin
if Col <> ExcludeCol then begin
if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then
Data := GridData.HexValue(Col)
else
Data := GridData.Col(Col);
// Keep formatted numeric values
if (GridData.DataType(Col).Category in [dtcInteger, dtcReal])
and (ExportFormat in [efExcel, efHTML]) then
Data := FormatNumber(Data, False);

case ExportFormat of
efHTML: begin
// Escape HTML control characters in data.
Data := HTMLSpecialChars(Data);
tmp := tmp + ' <td class="col' + IntToStr(Col) + '">' + Data + '</td>' + CRLF;
end;

efExcel, efCSV, efLaTeX, efWiki: begin
// Escape encloser characters inside data per de-facto CSV.
Data := StringReplace(Data, Encloser, Encloser+Encloser, [rfReplaceAll]);
if GridData.IsNull(Col) and (ExportFormat = efCSV) then
Data := '\N'
else
Data := Encloser + Data + Encloser;
tmp := tmp + Data + Separator;
end;

efXML: begin
// Print cell start tag.
tmp := tmp + #9#9'<field';
if chkIncludeColumnNames.Checked then
tmp := tmp + ' name="' + HTMLSpecialChars(Grid.Header.Columns[Col].Text) + '"';
if GridData.IsNull(Col) then
tmp := tmp + ' xsi:nil="true" />' + CRLF
else begin
if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then
tmp := tmp + ' format="hex"';
tmp := tmp + '>' + HTMLSpecialChars(Data) + '</field>' + CRLF;
end;
end;

efSQLInsert, efSQLReplace: begin
if GridData.IsNull(Col) then
Data := 'NULL'
else if GridData.DataType(Col).Index = dtBit then
Data := 'b' + esc(Data)
else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal, dtcBinary, dtcSpatial]) then
Data := esc(Data);
tmp := tmp + Data + ', ';
end;

efPHPArray: begin
if GridData.IsNull(Col) then
Data := 'NULL'
else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal]) then
Data := esc(Data);
if chkIncludeColumnNames.Checked then
tmp := tmp + #9#9 + '''' + Grid.Header.Columns[Col].Text + ''' => ' + Data + ','+CRLF
else
tmp := tmp + #9#9 + Data + ','+CRLF;
end;

end;
end;

Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
end;

// Row epilogue
case ExportFormat of
efHTML:
tmp := tmp + ' </tr>' + CRLF;
efExcel, efCSV, efLaTeX, efWiki: begin
Delete(tmp, Length(tmp)-Length(Separator)+1, Length(Separator));
tmp := tmp + Terminator;
end;
efXML:
tmp := tmp + #9'</row>' + CRLF;
efSQLInsert, efSQLReplace: begin
Delete(tmp, Length(tmp)-1, 2);
tmp := tmp + ');' + CRLF;
end;
efPHPArray:
tmp := tmp + #9 + '),' + CRLF;
end;
S.WriteString(tmp);

Node := GetNextNode(Grid, Node, SelectionOnly);
end;

// Footer
case ExportFormat of
efHTML: begin
tmp :=
' </tbody>' + CRLF +
' </table>' + CRLF + CRLF +
' <p>' + CRLF +
' <em>generated ' + DateToStr(now) + ' ' + TimeToStr(now) +
' by <a href="'+APPDOMAIN+'">' + APPNAME + ' ' + Mainform.AppVersion + '</a></em>' + CRLF +
' </p>' + CRLF + CRLF +
' </body>' + CRLF +
'</html>' + CRLF;
end;
efXML: begin
if chkIncludeQuery.Checked then
tmp := '</resultset>' + CRLF
else
tmp := '</table_data>' + CRLF;
end;
efLaTeX:
tmp := '\end{tabular}' + CRLF;
efPHPArray: begin
tmp := ');' + CRLF;
if radioOutputFile.Checked then
tmp := tmp + '?>';
end;
else
tmp := '';
end;
S.WriteString(tmp);

if radioOutputCopyToClipboard.Checked then begin
// SynEdit's exporter is slow on large strings, see issue #2903
if S.Size < 100*SIZE_KB then begin
case ExportFormat of
efSQLInsert, efSQLReplace: begin
Exporter := TSynExporterHTML.Create(Self);
Exporter.Highlighter := MainForm.SynSQLSyn1;
Exporter.ExportAll(Explode(CRLF, S.DataString));
HTML := TMemoryStream.Create;
Exporter.SaveToStream(HTML);
Exporter.Free;
end;
efHTML: HTML := S;
else HTML := nil;
end;
end;
StreamToClipboard(S, HTML, (ExportFormat=efHTML) and (HTML <> nil));
end else begin
try
S.SaveToFile(editFilename.Text);
except
on E:EFCreateError do begin
// Keep form open if file cannot be created
ModalResult := mrNone;
MainForm.SetProgressState(pbsError);
ErrorDialog(E.Message);
end;
end;
end;

Mainform.DisableProgress;
Mainform.ShowStatusMsg('Freeing data...');
FreeAndNil(S);
Mainform.ShowStatusMsg;
Screen.Cursor := crDefault;
end;


end.

Change log

r4187 by ansgar.becker on Aug 31, 2012   Diff
Fix confusion in save-as-dialog. Fixes
 issue #2921 .
Go to: 

Older revisions

r4186 by ansgar.becker on Aug 31, 2012   Diff
Tweak grid export dialog with some
minor enhancements:
- Auto select ANSI encoding for Excel
output, see
http://en.wikipedia.org/wiki/Comma-
...
r4181 by ansgar.becker on Aug 26, 2012   Diff
Use "\N" without enclosing chars for
NULL values in CSV export, not an
empty string. Just like MySQL does in
a SELECT .. INTO OUTFILE.
r4180 by ansgar.becker on Aug 26, 2012   Diff
Add "Include query" checkbox option
onto grid export dialog. See http://ww
w.heidisql.com/forum.php?t=10853
All revisions of this file

File info

Size: 28751 bytes, 796 lines
Powered by Google Project Hosting