My favorites | Sign in
Project Home Issues Source
Checkout   Browse   Changes  
Changes to /trunk/source/exportgrid.pas
r4178 vs. r4179 Compare: vs.  Format:
Revision r4179
Go to: 
/trunk/source/exportgrid.pas   r4178 /trunk/source/exportgrid.pas   r4179
1 unit exportgrid; 1 unit exportgrid;
2 2
3 interface 3 interface
4 4
5 uses 5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ExtCtrls, Menus, ComCtrls, VirtualTrees, SynExportHTML; 7 Dialogs, StdCtrls, ExtCtrls, Menus, ComCtrls, VirtualTrees, SynExportHTML;
8 8
9 type 9 type
10 TGridExportFormat = (efExcel, efCSV, efHTML, efXML, efSQLInsert, efSQLReplace, efLaTeX, efWiki, efPHPArray); 10 TGridExportFormat = (efExcel, efCSV, efHTML, efXML, efSQLInsert, efSQLReplace, efLaTeX, efWiki, efPHPArray);
11 11
12 TfrmExportGrid = class(TForm) 12 TfrmExportGrid = class(TForm)
13 btnOK: TButton; 13 btnOK: TButton;
14 btnCancel: TButton; 14 btnCancel: TButton;
15 grpFormat: TRadioGroup; 15 grpFormat: TRadioGroup;
16 grpSelection: TRadioGroup; 16 grpSelection: TRadioGroup;
17 grpOutput: TGroupBox; 17 grpOutput: TGroupBox;
18 radioOutputCopyToClipboard: TRadioButton; 18 radioOutputCopyToClipboard: TRadioButton;
19 radioOutputFile: TRadioButton; 19 radioOutputFile: TRadioButton;
20 editFilename: TButtonedEdit; 20 editFilename: TButtonedEdit;
21 grpOptions: TGroupBox; 21 grpOptions: TGroupBox;
22 chkColumnHeader: TCheckBox; 22 chkColumnHeader: TCheckBox;
23 editSeparator: TButtonedEdit; 23 editSeparator: TButtonedEdit;
24 editEncloser: TButtonedEdit; 24 editEncloser: TButtonedEdit;
25 editTerminator: TButtonedEdit; 25 editTerminator: TButtonedEdit;
26 lblSeparator: TLabel; 26 lblSeparator: TLabel;
27 lblEncloser: TLabel; 27 lblEncloser: TLabel;
28 lblTerminator: TLabel; 28 lblTerminator: TLabel;
29 popupCSVchar: TPopupMenu; 29 popupCSVchar: TPopupMenu;
30 menuCSVtab: TMenuItem; 30 menuCSVtab: TMenuItem;
31 menuCSVunixlinebreak: TMenuItem; 31 menuCSVunixlinebreak: TMenuItem;
32 menuCSVmaclinebreak: TMenuItem; 32 menuCSVmaclinebreak: TMenuItem;
33 menuCSVwinlinebreak: TMenuItem; 33 menuCSVwinlinebreak: TMenuItem;
34 menuCSVnul: TMenuItem; 34 menuCSVnul: TMenuItem;
35 menuCSVbackspace: TMenuItem; 35 menuCSVbackspace: TMenuItem;
36 menuCSVcontrolz: TMenuItem; 36 menuCSVcontrolz: TMenuItem;
37 comboEncoding: TComboBox; 37 comboEncoding: TComboBox;
38 lblEncoding: TLabel; 38 lblEncoding: TLabel;
39 popupRecentFiles: TPopupMenu; 39 popupRecentFiles: TPopupMenu;
40 menuCSVsinglequote: TMenuItem; 40 menuCSVsinglequote: TMenuItem;
41 menuCSVdoublequote: TMenuItem; 41 menuCSVdoublequote: TMenuItem;
42 menuCSVcomma: TMenuItem; 42 menuCSVcomma: TMenuItem;
43 menuCSVsemicolon: TMenuItem; 43 menuCSVsemicolon: TMenuItem;
44 N1: TMenuItem; 44 N1: TMenuItem;
45 N2: TMenuItem; 45 N2: TMenuItem;
46 N3: TMenuItem; 46 N3: TMenuItem;
47 chkIncludeAutoIncrement: TCheckBox; 47 chkIncludeAutoIncrement: TCheckBox;
48 procedure FormCreate(Sender: TObject); 48 procedure FormCreate(Sender: TObject);
49 procedure FormDestroy(Sender: TObject); 49 procedure FormDestroy(Sender: TObject);
50 procedure CalcSize(Sender: TObject); 50 procedure CalcSize(Sender: TObject);
51 procedure FormClose(Sender: TObject; var Action: TCloseAction); 51 procedure FormClose(Sender: TObject; var Action: TCloseAction);
52 procedure editFilenameRightButtonClick(Sender: TObject); 52 procedure editFilenameRightButtonClick(Sender: TObject);
53 procedure editFilenameChange(Sender: TObject); 53 procedure editFilenameChange(Sender: TObject);
54 procedure SelectRecentFile(Sender: TObject); 54 procedure SelectRecentFile(Sender: TObject);
55 procedure popupRecentFilesPopup(Sender: TObject); 55 procedure popupRecentFilesPopup(Sender: TObject);
56 procedure menuCSVClick(Sender: TObject); 56 procedure menuCSVClick(Sender: TObject);
57 procedure editCSVRightButtonClick(Sender: TObject); 57 procedure editCSVRightButtonClick(Sender: TObject);
58 procedure editCSVChange(Sender: TObject); 58 procedure editCSVChange(Sender: TObject);
59 procedure ValidateControls(Sender: TObject); 59 procedure ValidateControls(Sender: TObject);
60 procedure btnOKClick(Sender: TObject); 60 procedure btnOKClick(Sender: TObject);
61 procedure FormShow(Sender: TObject); 61 procedure FormShow(Sender: TObject);
62 private 62 private
63 { Private declarations } 63 { Private declarations }
64 FCSVEditor: TButtonedEdit; 64 FCSVEditor: TButtonedEdit;
65 FCSVSeparator, FCSVEncloser, FCSVTerminator: String; 65 FCSVSeparator, FCSVEncloser, FCSVTerminator: String;
66 FGrid: TVirtualStringTree; 66 FGrid: TVirtualStringTree;
67 FRecentFiles: TStringList; 67 FRecentFiles: TStringList;
68 procedure SaveDialogTypeChange(Sender: TObject); 68 procedure SaveDialogTypeChange(Sender: TObject);
69 function GetExportFormat: TGridExportFormat; 69 function GetExportFormat: TGridExportFormat;
70 procedure SetExportFormat(Value: TGridExportFormat); 70 procedure SetExportFormat(Value: TGridExportFormat);
71 procedure SetExportFormatByFilename; 71 procedure SetExportFormatByFilename;
72 public 72 public
73 { Public declarations } 73 { Public declarations }
74 property Grid: TVirtualStringTree read FGrid write FGrid; 74 property Grid: TVirtualStringTree read FGrid write FGrid;
75 property ExportFormat: TGridExportFormat read GetExportFormat write SetExportFormat; 75 property ExportFormat: TGridExportFormat read GetExportFormat write SetExportFormat;
76 end; 76 end;
77 77
78 78
79 implementation 79 implementation
80 80
81 uses main, helpers, dbconnection, mysql_structures; 81 uses main, helpers, dbconnection, mysql_structures;
82 82
83 {$R *.dfm} 83 {$R *.dfm}
84 84
85 85
86 86
87 procedure TfrmExportGrid.FormCreate(Sender: TObject); 87 procedure TfrmExportGrid.FormCreate(Sender: TObject);
88 begin 88 begin
89 InheritFont(Font); 89 InheritFont(Font);
90 editFilename.Text := AppSettings.ReadString(asGridExportFilename); 90 editFilename.Text := AppSettings.ReadString(asGridExportFilename);
91 radioOutputCopyToClipboard.Checked := AppSettings.ReadBool(asGridExportOutputCopy); 91 radioOutputCopyToClipboard.Checked := AppSettings.ReadBool(asGridExportOutputCopy);
92 radioOutputFile.Checked := AppSettings.ReadBool(asGridExportOutputFile); 92 radioOutputFile.Checked := AppSettings.ReadBool(asGridExportOutputFile);
93 FRecentFiles := Explode(DELIM, AppSettings.ReadString(asGridExportRecentFiles)); 93 FRecentFiles := Explode(DELIM, AppSettings.ReadString(asGridExportRecentFiles));
94 comboEncoding.Items.Assign(MainForm.FileEncodings); 94 comboEncoding.Items.Assign(MainForm.FileEncodings);
95 comboEncoding.Items.Delete(0); // Remove "Auto detect" 95 comboEncoding.Items.Delete(0); // Remove "Auto detect"
96 comboEncoding.ItemIndex := AppSettings.ReadInt(asGridExportEncoding); 96 comboEncoding.ItemIndex := AppSettings.ReadInt(asGridExportEncoding);
97 grpFormat.ItemIndex := AppSettings.ReadInt(asGridExportFormat); 97 grpFormat.ItemIndex := AppSettings.ReadInt(asGridExportFormat);
98 grpSelection.ItemIndex := AppSettings.ReadInt(asGridExportSelection); 98 grpSelection.ItemIndex := AppSettings.ReadInt(asGridExportSelection);
99 chkColumnHeader.Checked := AppSettings.ReadBool(asGridExportColumnNames); 99 chkColumnHeader.Checked := AppSettings.ReadBool(asGridExportColumnNames);
100 chkIncludeAutoIncrement.Checked := AppSettings.ReadBool(asGridExportIncludeAutoInc);
101 FCSVSeparator := AppSettings.ReadString(asGridExportSeparator); 100 FCSVSeparator := AppSettings.ReadString(asGridExportSeparator);
102 FCSVEncloser := AppSettings.ReadString(asGridExportEncloser); 101 FCSVEncloser := AppSettings.ReadString(asGridExportEncloser);
103 FCSVTerminator := AppSettings.ReadString(asGridExportTerminator); 102 FCSVTerminator := AppSettings.ReadString(asGridExportTerminator);
104 ValidateControls(Sender); 103 ValidateControls(Sender);
105 end; 104 end;
106 105
107 106
108 procedure TfrmExportGrid.FormDestroy(Sender: TObject); 107 procedure TfrmExportGrid.FormDestroy(Sender: TObject);
109 begin 108 begin
110 // Store settings 109 // Store settings
111 if ModalResult = mrOK then begin 110 if ModalResult = mrOK then begin
112 AppSettings.WriteBool(asGridExportOutputCopy, radioOutputCopyToClipboard.Checked); 111 AppSettings.WriteBool(asGridExportOutputCopy, radioOutputCopyToClipboard.Checked);
113 AppSettings.WriteBool(asGridExportOutputFile, radioOutputFile.Checked); 112 AppSettings.WriteBool(asGridExportOutputFile, radioOutputFile.Checked);
114 AppSettings.WriteString(asGridExportFilename, editFilename.Text); 113 AppSettings.WriteString(asGridExportFilename, editFilename.Text);
115 AppSettings.WriteString(asGridExportRecentFiles, ImplodeStr(DELIM, FRecentFiles)); 114 AppSettings.WriteString(asGridExportRecentFiles, ImplodeStr(DELIM, FRecentFiles));
116 AppSettings.WriteInt(asGridExportEncoding, comboEncoding.ItemIndex); 115 AppSettings.WriteInt(asGridExportEncoding, comboEncoding.ItemIndex);
117 AppSettings.WriteInt(asGridExportFormat, grpFormat.ItemIndex); 116 AppSettings.WriteInt(asGridExportFormat, grpFormat.ItemIndex);
118 AppSettings.WriteInt(asGridExportSelection, grpSelection.ItemIndex); 117 AppSettings.WriteInt(asGridExportSelection, grpSelection.ItemIndex);
119 AppSettings.WriteBool(asGridExportColumnNames, chkColumnHeader.Checked); 118 AppSettings.WriteBool(asGridExportColumnNames, chkColumnHeader.Checked);
120 AppSettings.WriteBool(asGridExportIncludeAutoInc, chkIncludeAutoIncrement.Checked); 119 AppSettings.WriteBool(asGridExportIncludeAutoInc, chkIncludeAutoIncrement.Checked);
121 AppSettings.WriteString(asGridExportSeparator, FCSVSeparator); 120 AppSettings.WriteString(asGridExportSeparator, FCSVSeparator);
122 AppSettings.WriteString(asGridExportEncloser, FCSVEncloser); 121 AppSettings.WriteString(asGridExportEncloser, FCSVEncloser);
123 AppSettings.WriteString(asGridExportTerminator, FCSVTerminator); 122 AppSettings.WriteString(asGridExportTerminator, FCSVTerminator);
124 end; 123 end;
125 end; 124 end;
126 125
127 126
128 procedure TfrmExportGrid.FormShow(Sender: TObject); 127 procedure TfrmExportGrid.FormShow(Sender: TObject);
129 begin 128 begin
130 // Show dialog. Expect "Grid" property to be set now by the caller. 129 // Show dialog. Expect "Grid" property to be set now by the caller.
130 chkIncludeAutoIncrement.Checked := AppSettings.ReadBool(asGridExportIncludeAutoInc);
131 CalcSize(Sender); 131 CalcSize(Sender);
132 end; 132 end;
133 133
134 134
135 procedure TfrmExportGrid.FormClose(Sender: TObject; var Action: TCloseAction); 135 procedure TfrmExportGrid.FormClose(Sender: TObject; var Action: TCloseAction);
136 begin 136 begin
137 // Destroy dialog - not cached 137 // Destroy dialog - not cached
138 Action := caFree; 138 Action := caFree;
139 end; 139 end;
140 140
141 141
142 procedure TfrmExportGrid.ValidateControls(Sender: TObject); 142 procedure TfrmExportGrid.ValidateControls(Sender: TObject);
143 var 143 var
144 Enable: Boolean; 144 Enable: Boolean;
145 begin 145 begin
146 // Display the actually used control characters, even if they cannot be changed 146 // Display the actually used control characters, even if they cannot be changed
147 case ExportFormat of 147 case ExportFormat of
148 efExcel: begin 148 efExcel: begin
149 // Tab for pasting, semicolon if comma is also the decimal separator, and comma for the rest 149 // Tab for pasting, semicolon if comma is also the decimal separator, and comma for the rest
150 // see http://en.wikipedia.org/wiki/Comma-separated_values 150 // see http://en.wikipedia.org/wiki/Comma-separated_values
151 if radioOutputCopyToClipboard.Checked then 151 if radioOutputCopyToClipboard.Checked then
152 editSeparator.Text := '\t' 152 editSeparator.Text := '\t'
153 else if FormatSettings.DecimalSeparator=',' then 153 else if FormatSettings.DecimalSeparator=',' then
154 editSeparator.Text := ';' 154 editSeparator.Text := ';'
155 else 155 else
156 editSeparator.Text := ','; 156 editSeparator.Text := ',';
157 editEncloser.Text := '"'; 157 editEncloser.Text := '"';
158 editTerminator.Text := '\r\n'; 158 editTerminator.Text := '\r\n';
159 end; 159 end;
160 efCSV: begin 160 efCSV: begin
161 editSeparator.Text := FCSVSeparator; 161 editSeparator.Text := FCSVSeparator;
162 editEncloser.Text := FCSVEncloser; 162 editEncloser.Text := FCSVEncloser;
163 editTerminator.Text := FCSVTerminator; 163 editTerminator.Text := FCSVTerminator;
164 end; 164 end;
165 else begin 165 else begin
166 editSeparator.Text := ''; 166 editSeparator.Text := '';
167 editEncloser.Text := ''; 167 editEncloser.Text := '';
168 editTerminator.Text := ''; 168 editTerminator.Text := '';
169 end; 169 end;
170 end; 170 end;
171 171
172 Enable := ExportFormat = efCSV; 172 Enable := ExportFormat = efCSV;
173 lblSeparator.Enabled := Enable; 173 lblSeparator.Enabled := Enable;
174 editSeparator.Enabled := Enable; 174 editSeparator.Enabled := Enable;
175 editSeparator.RightButton.Enabled := Enable; 175 editSeparator.RightButton.Enabled := Enable;
176 lblEncloser.Enabled := Enable; 176 lblEncloser.Enabled := Enable;
177 editEncloser.Enabled := Enable; 177 editEncloser.Enabled := Enable;
178 editEncloser.RightButton.Enabled := Enable; 178 editEncloser.RightButton.Enabled := Enable;
179 lblTerminator.Enabled := Enable; 179 lblTerminator.Enabled := Enable;
180 editTerminator.Enabled := Enable; 180 editTerminator.Enabled := Enable;
181 editTerminator.RightButton.Enabled := Enable; 181 editTerminator.RightButton.Enabled := Enable;
182 btnOK.Enabled := radioOutputCopyToClipboard.Checked or (radioOutputFile.Checked and (editFilename.Text <> '')); 182 btnOK.Enabled := radioOutputCopyToClipboard.Checked or (radioOutputFile.Checked and (editFilename.Text <> ''));
183 if radioOutputFile.Checked then 183 if radioOutputFile.Checked then
184 editFilename.Font.Color := clWindowText 184 editFilename.Font.Color := clWindowText
185 else 185 else
186 editFilename.Font.Color := clGrayText; 186 editFilename.Font.Color := clGrayText;
187 comboEncoding.Enabled := radioOutputFile.Checked; 187 comboEncoding.Enabled := radioOutputFile.Checked;
188 lblEncoding.Enabled := radioOutputFile.Checked; 188 lblEncoding.Enabled := radioOutputFile.Checked;
189 end; 189 end;
190 190
191 191
192 function TfrmExportGrid.GetExportFormat: TGridExportFormat; 192 function TfrmExportGrid.GetExportFormat: TGridExportFormat;
193 begin 193 begin
194 Result := TGridExportFormat(grpFormat.ItemIndex); 194 Result := TGridExportFormat(grpFormat.ItemIndex);
195 end; 195 end;
196 196
197 197
198 procedure TfrmExportGrid.SetExportFormat(Value: TGridExportFormat); 198 procedure TfrmExportGrid.SetExportFormat(Value: TGridExportFormat);
199 begin 199 begin
200 grpFormat.ItemIndex := Integer(Value); 200 grpFormat.ItemIndex := Integer(Value);
201 end; 201 end;
202 202
203 203
204 procedure TfrmExportGrid.SetExportFormatByFilename; 204 procedure TfrmExportGrid.SetExportFormatByFilename;
205 var 205 var
206 ext: String; 206 ext: String;
207 begin 207 begin
208 // Set format by file extension 208 // Set format by file extension
209 ext := LowerCase(Copy(ExtractFileExt(editFilename.Text), 2, 10)); 209 ext := LowerCase(Copy(ExtractFileExt(editFilename.Text), 2, 10));
210 if (ext = 'csv') and (not (ExportFormat in [efExcel, efCSV])) 210 if (ext = 'csv') and (not (ExportFormat in [efExcel, efCSV]))
211 then ExportFormat := efCSV 211 then ExportFormat := efCSV
212 else if ext = 'html' then ExportFormat := efHTML 212 else if ext = 'html' then ExportFormat := efHTML
213 else if ext = 'xml' then ExportFormat := efXML 213 else if ext = 'xml' then ExportFormat := efXML
214 else if ext = 'sql' then ExportFormat := efSQLInsert 214 else if ext = 'sql' then ExportFormat := efSQLInsert
215 else if ext = 'latex' then ExportFormat := efLaTeX 215 else if ext = 'latex' then ExportFormat := efLaTeX
216 else if ext = 'wiki' then ExportFormat := efWiki 216 else if ext = 'wiki' then ExportFormat := efWiki
217 else if ext = 'php' then ExportFormat := efPHPArray; 217 else if ext = 'php' then ExportFormat := efPHPArray;
218 end; 218 end;
219 219
220 220
221 procedure TfrmExportGrid.editFilenameChange(Sender: TObject); 221 procedure TfrmExportGrid.editFilenameChange(Sender: TObject);
222 begin 222 begin
223 radioOutputFile.Checked := True; 223 radioOutputFile.Checked := True;
224 end; 224 end;
225 225
226 226
227 procedure TfrmExportGrid.editFilenameRightButtonClick(Sender: TObject); 227 procedure TfrmExportGrid.editFilenameRightButtonClick(Sender: TObject);
228 var 228 var
229 Dialog: TSaveDialog; 229 Dialog: TSaveDialog;
230 begin 230 begin
231 // Select file target 231 // Select file target
232 Dialog := TSaveDialog.Create(Self); 232 Dialog := TSaveDialog.Create(Self);
233 Dialog.InitialDir := ExtractFilePath(editFilename.Text); 233 Dialog.InitialDir := ExtractFilePath(editFilename.Text);
234 Dialog.FileName := ExtractFileName(editFilename.Text); 234 Dialog.FileName := ExtractFileName(editFilename.Text);
235 Dialog.FileName := Copy(Dialog.FileName, 1, Length(Dialog.FileName)-Length(ExtractFileExt(Dialog.FileName))); 235 Dialog.FileName := Copy(Dialog.FileName, 1, Length(Dialog.FileName)-Length(ExtractFileExt(Dialog.FileName)));
236 Dialog.OnTypeChange := SaveDialogTypeChange; 236 Dialog.OnTypeChange := SaveDialogTypeChange;
237 Dialog.OnTypeChange(Dialog); 237 Dialog.OnTypeChange(Dialog);
238 Dialog.FilterIndex := grpFormat.ItemIndex+1; 238 Dialog.FilterIndex := grpFormat.ItemIndex+1;
239 Dialog.Filter := 'Tab separated values (*.csv)|*.csv|'+ 239 Dialog.Filter := 'Tab separated values (*.csv)|*.csv|'+
240 'Comma separated values (*.csv)|*.csv|'+ 240 'Comma separated values (*.csv)|*.csv|'+
241 'Hypertext markup language (*.html)|*.html|'+ 241 'Hypertext markup language (*.html)|*.html|'+
242 'Extensible markup language (*.xml)|*.xml|'+ 242 'Extensible markup language (*.xml)|*.xml|'+
243 'Structured query language (*.sql)|*.sql|'+ 243 'Structured query language (*.sql)|*.sql|'+
244 'LaTeX table (*.latex)|*.latex|'+ 244 'LaTeX table (*.latex)|*.latex|'+
245 'Wiki markup table (*.wiki)|*.wiki|'+ 245 'Wiki markup table (*.wiki)|*.wiki|'+
246 'PHP script (*.php)|*.php|'+ 246 'PHP script (*.php)|*.php|'+
247 'All files (*.*)|*.*'; 247 'All files (*.*)|*.*';
248 if Dialog.Execute then begin 248 if Dialog.Execute then begin
249 editFilename.Text := Dialog.FileName; 249 editFilename.Text := Dialog.FileName;
250 SetExportFormatByFilename; 250 SetExportFormatByFilename;
251 ValidateControls(Sender); 251 ValidateControls(Sender);
252 end; 252 end;
253 Dialog.Free; 253 Dialog.Free;
254 end; 254 end;
255 255
256 256
257 procedure TfrmExportGrid.popupRecentFilesPopup(Sender: TObject); 257 procedure TfrmExportGrid.popupRecentFilesPopup(Sender: TObject);
258 var 258 var
259 Filename: String; 259 Filename: String;
260 Menu: TPopupMenu; 260 Menu: TPopupMenu;
261 Item: TMenuItem; 261 Item: TMenuItem;
262 begin 262 begin
263 // Clear and populate drop down menu with recent files 263 // Clear and populate drop down menu with recent files
264 Menu := Sender as TPopupMenu; 264 Menu := Sender as TPopupMenu;
265 Menu.Items.Clear; 265 Menu.Items.Clear;
266 for Filename in FRecentFiles do begin 266 for Filename in FRecentFiles do begin
267 Item := TMenuItem.Create(Menu); 267 Item := TMenuItem.Create(Menu);
268 Menu.Items.Add(Item); 268 Menu.Items.Add(Item);
269 Item.Caption := Filename; 269 Item.Caption := Filename;
270 Item.Hint := Filename; 270 Item.Hint := Filename;
271 Item.OnClick := SelectRecentFile; 271 Item.OnClick := SelectRecentFile;
272 Item.Checked := Filename = editFilename.Text; 272 Item.Checked := Filename = editFilename.Text;
273 end; 273 end;
274 end; 274 end;
275 275
276 276
277 procedure TfrmExportGrid.SelectRecentFile(Sender: TObject); 277 procedure TfrmExportGrid.SelectRecentFile(Sender: TObject);
278 begin 278 begin
279 // Select file from recently used files 279 // Select file from recently used files
280 editFilename.Text := (Sender as TMenuItem).Hint; 280 editFilename.Text := (Sender as TMenuItem).Hint;
281 SetExportFormatByFilename; 281 SetExportFormatByFilename;
282 end; 282 end;
283 283
284 284
285 procedure TfrmExportGrid.CalcSize(Sender: TObject); 285 procedure TfrmExportGrid.CalcSize(Sender: TObject);
286 var 286 var
287 GridData: TDBQuery; 287 GridData: TDBQuery;
288 Node: PVirtualNode; 288 Node: PVirtualNode;
289 Col, ExcludeCol: TColumnIndex; 289 Col, ExcludeCol: TColumnIndex;
290 RowNum: PCardinal; 290 RowNum: PCardinal;
291 SelectionSize, AllSize: Int64; 291 SelectionSize, AllSize: Int64;
292 begin 292 begin
293 GridData := Mainform.GridResult(Grid); 293 GridData := Mainform.GridResult(Grid);
294 AllSize := 0; 294 AllSize := 0;
295 SelectionSize := 0; 295 SelectionSize := 0;
296 296 chkIncludeAutoIncrement.Enabled := GridData.AutoIncrementColumn > -1;
297 ExcludeCol := NoColumn; 297 ExcludeCol := -1;
298 Col := Grid.Header.Columns.GetFirstVisibleColumn; 298 if chkIncludeAutoIncrement.Enabled and (not chkIncludeAutoIncrement.Checked) then
299 while Col > NoColumn do begin 299 ExcludeCol := GridData.AutoIncrementColumn;
300 if (not chkIncludeAutoIncrement.Checked) and GridData.ColIsAutoIncrement(Col) then
301 ExcludeCol := Col;
302 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
303 end;
304 chkIncludeAutoIncrement.Enabled := ExcludeCol <> NoColumn;
305 300
306 Node := GetNextNode(Grid, nil, False); 301 Node := GetNextNode(Grid, nil, False);
307 while Assigned(Node) do begin 302 while Assigned(Node) do begin
308 RowNum := Grid.GetNodeData(Node); 303 RowNum := Grid.GetNodeData(Node);
309 GridData.RecNo := RowNum^; 304 GridData.RecNo := RowNum^;
310 Col := Grid.Header.Columns.GetFirstVisibleColumn; 305 Col := Grid.Header.Columns.GetFirstVisibleColumn;
311 while Col > NoColumn do begin 306 while Col > NoColumn do begin
312 if Col <> ExcludeCol then begin 307 if Col <> ExcludeCol then begin
313 Inc(AllSize, GridData.ColumnLengths(Col)); 308 Inc(AllSize, GridData.ColumnLengths(Col));
314 if vsSelected in Node.States then 309 if vsSelected in Node.States then
315 Inc(SelectionSize, GridData.ColumnLengths(Col)); 310 Inc(SelectionSize, GridData.ColumnLengths(Col));
316 end; 311 end;
317 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 312 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
318 end; 313 end;
319 Node := GetNextNode(Grid, Node, False); 314 Node := GetNextNode(Grid, Node, False);
320 end; 315 end;
321 grpSelection.Items[0] := 'Selection ('+FormatNumber(Grid.SelectedCount)+' rows, '+FormatByteNumber(SelectionSize)+')'; 316 grpSelection.Items[0] := 'Selection ('+FormatNumber(Grid.SelectedCount)+' rows, '+FormatByteNumber(SelectionSize)+')';
322 grpSelection.Items[1] := 'Complete ('+FormatNumber(Grid.RootNodeCount)+' rows, '+FormatByteNumber(AllSize)+')'; 317 grpSelection.Items[1] := 'Complete ('+FormatNumber(Grid.RootNodeCount)+' rows, '+FormatByteNumber(AllSize)+')';
323 end; 318 end;
324 319
325 320
326 procedure TfrmExportGrid.editCSVChange(Sender: TObject); 321 procedure TfrmExportGrid.editCSVChange(Sender: TObject);
327 var 322 var
328 Edit: TButtonedEdit; 323 Edit: TButtonedEdit;
329 begin 324 begin
330 // Remember csv settings 325 // Remember csv settings
331 Edit := Sender as TButtonedEdit; 326 Edit := Sender as TButtonedEdit;
332 if ExportFormat = efCSV then begin 327 if ExportFormat = efCSV then begin
333 if Edit = editSeparator then 328 if Edit = editSeparator then
334 FCSVSeparator := Edit.Text 329 FCSVSeparator := Edit.Text
335 else if Edit = editEncloser then 330 else if Edit = editEncloser then
336 FCSVEncloser := Edit.Text 331 FCSVEncloser := Edit.Text
337 else if Edit = editTerminator then 332 else if Edit = editTerminator then
338 FCSVTerminator := Edit.Text; 333 FCSVTerminator := Edit.Text;
339 end; 334 end;
340 end; 335 end;
341 336
342 337
343 procedure TfrmExportGrid.SaveDialogTypeChange(Sender: TObject); 338 procedure TfrmExportGrid.SaveDialogTypeChange(Sender: TObject);
344 var 339 var
345 Dialog: TSaveDialog; 340 Dialog: TSaveDialog;
346 begin 341 begin
347 // Set default file-extension of saved file and options on the dialog to show 342 // Set default file-extension of saved file and options on the dialog to show
348 Dialog := Sender as TSaveDialog; 343 Dialog := Sender as TSaveDialog;
349 case Dialog.FilterIndex of 344 case Dialog.FilterIndex of
350 1: Dialog.DefaultExt := 'csv'; 345 1: Dialog.DefaultExt := 'csv';
351 2: Dialog.DefaultExt := 'html'; 346 2: Dialog.DefaultExt := 'html';
352 3: Dialog.DefaultExt := 'xml'; 347 3: Dialog.DefaultExt := 'xml';
353 4: Dialog.DefaultExt := 'sql'; 348 4: Dialog.DefaultExt := 'sql';
354 5: Dialog.DefaultExt := 'LaTeX'; 349 5: Dialog.DefaultExt := 'LaTeX';
355 6: Dialog.DefaultExt := 'wiki'; 350 6: Dialog.DefaultExt := 'wiki';
356 end; 351 end;
357 end; 352 end;
358 353
359 354
360 procedure TfrmExportGrid.editCSVRightButtonClick(Sender: TObject); 355 procedure TfrmExportGrid.editCSVRightButtonClick(Sender: TObject);
361 var 356 var
362 p: TPoint; 357 p: TPoint;
363 Item: TMenuItem; 358 Item: TMenuItem;
364 begin 359 begin
365 // Remember editor and prepare popup menu items 360 // Remember editor and prepare popup menu items
366 FCSVEditor := Sender as TButtonedEdit; 361 FCSVEditor := Sender as TButtonedEdit;
367 p := FCSVEditor.ClientToScreen(FCSVEditor.ClientRect.BottomRight); 362 p := FCSVEditor.ClientToScreen(FCSVEditor.ClientRect.BottomRight);
368 for Item in popupCSVchar.Items do begin 363 for Item in popupCSVchar.Items do begin
369 Item.OnClick := menuCSVClick; 364 Item.OnClick := menuCSVClick;
370 Item.Checked := FCSVEditor.Text = Item.Hint; 365 Item.Checked := FCSVEditor.Text = Item.Hint;
371 end; 366 end;
372 popupCSVchar.Popup(p.X-16, p.Y); 367 popupCSVchar.Popup(p.X-16, p.Y);
373 end; 368 end;
374 369
375 370
376 procedure TfrmExportGrid.menuCSVClick(Sender: TObject); 371 procedure TfrmExportGrid.menuCSVClick(Sender: TObject);
377 begin 372 begin
378 // Insert char from menu 373 // Insert char from menu
379 FCSVEditor.Text := TMenuItem(Sender).Hint; 374 FCSVEditor.Text := TMenuItem(Sender).Hint;
380 end; 375 end;
381 376
382 377
383 procedure TfrmExportGrid.btnOKClick(Sender: TObject); 378 procedure TfrmExportGrid.btnOKClick(Sender: TObject);
384 var 379 var
385 Col, ExcludeCol: TColumnIndex; 380 Col, ExcludeCol: TColumnIndex;
386 Header, Data, tmp, Encloser, Separator, Terminator, TableName: String; 381 Header, Data, tmp, Encloser, Separator, Terminator, TableName: String;
387 Node: PVirtualNode; 382 Node: PVirtualNode;
388 GridData: TDBQuery; 383 GridData: TDBQuery;
389 SelectionOnly: Boolean; 384 SelectionOnly: Boolean;
390 i: Integer; 385 i: Integer;
391 NodeCount: Cardinal; 386 NodeCount: Cardinal;
392 RowNum: PCardinal; 387 RowNum: PCardinal;
393 HTML: TStream; 388 HTML: TStream;
394 S: TStringStream; 389 S: TStringStream;
395 Exporter: TSynExporterHTML; 390 Exporter: TSynExporterHTML;
396 Encoding: TEncoding; 391 Encoding: TEncoding;
397 begin 392 begin
398 // Confirmation dialog if file exists 393 // Confirmation dialog if file exists
399 if radioOutputFile.Checked 394 if radioOutputFile.Checked
400 and FileExists(editFilename.Text) 395 and FileExists(editFilename.Text)
401 and (MessageDialog('File exists', 'Overwrite file '+editFilename.Text+'?', mtConfirmation, [mbYes, mbCancel]) = mrCancel) 396 and (MessageDialog('File exists', 'Overwrite file '+editFilename.Text+'?', mtConfirmation, [mbYes, mbCancel]) = mrCancel)
402 then begin 397 then begin
403 ModalResult := mrNone; 398 ModalResult := mrNone;
404 Exit; 399 Exit;
405 end; 400 end;
406 401
407 Screen.Cursor := crHourglass; 402 Screen.Cursor := crHourglass;
408 403
409 SelectionOnly := grpSelection.ItemIndex = 0; 404 SelectionOnly := grpSelection.ItemIndex = 0;
410 Mainform.DataGridEnsureFullRows(Grid, SelectionOnly); 405 Mainform.DataGridEnsureFullRows(Grid, SelectionOnly);
411 GridData := Mainform.GridResult(Grid); 406 GridData := Mainform.GridResult(Grid);
412 if SelectionOnly then 407 if SelectionOnly then
413 NodeCount := Grid.SelectedCount 408 NodeCount := Grid.SelectedCount
414 else 409 else
415 NodeCount := Grid.RootNodeCount; 410 NodeCount := Grid.RootNodeCount;
416 MainForm.EnableProgress(NodeCount); 411 MainForm.EnableProgress(NodeCount);
417 TableName := BestTableName(GridData); 412 TableName := BestTableName(GridData);
418 ExcludeCol := NoColumn; 413 ExcludeCol := -1;
419 Col := Grid.Header.Columns.GetFirstVisibleColumn; 414 if (not chkIncludeAutoIncrement.Checked) or (not chkIncludeAutoIncrement.Enabled) then
420 while Col > NoColumn do begin 415 ExcludeCol := GridData.AutoIncrementColumn;
421 if (not chkIncludeAutoIncrement.Checked) and GridData.ColIsAutoIncrement(Col) then
422 ExcludeCol := Col;
423 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
424 end;
425 416
426 if radioOutputCopyToClipboard.Checked then 417 if radioOutputCopyToClipboard.Checked then
427 Encoding := TEncoding.UTF8 418 Encoding := TEncoding.UTF8
428 else begin 419 else begin
429 Encoding := MainForm.GetEncodingByName(comboEncoding.Text); 420 Encoding := MainForm.GetEncodingByName(comboEncoding.Text);
430 // Add selected file to file list, and sort it onto the top of the list 421 // Add selected file to file list, and sort it onto the top of the list
431 i := FRecentFiles.IndexOf(editFilename.Text); 422 i := FRecentFiles.IndexOf(editFilename.Text);
432 if i > -1 then 423 if i > -1 then
433 FRecentFiles.Delete(i); 424 FRecentFiles.Delete(i);
434 FRecentFiles.Insert(0, editFilename.Text); 425 FRecentFiles.Insert(0, editFilename.Text);
435 for i:=FRecentFiles.Count-1 downto 10 do 426 for i:=FRecentFiles.Count-1 downto 10 do
436 FRecentFiles.Delete(i); 427 FRecentFiles.Delete(i);
437 end; 428 end;
438 S := TStringStream.Create(Header, Encoding); 429 S := TStringStream.Create(Header, Encoding);
439 Header := ''; 430 Header := '';
440 case ExportFormat of 431 case ExportFormat of
441 efHTML: begin 432 efHTML: begin
442 Header := 433 Header :=
443 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ' + CRLF + 434 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ' + CRLF +
444 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + CRLF + CRLF + 435 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + CRLF + CRLF +
445 '<html>' + CRLF + 436 '<html>' + CRLF +
446 ' <head>' + CRLF + 437 ' <head>' + CRLF +
447 ' <title>' + TableName + '</title>' + CRLF + 438 ' <title>' + TableName + '</title>' + CRLF +
448 ' <meta name="GENERATOR" content="'+ APPNAME+' '+Mainform.AppVersion + '">' + CRLF + 439 ' <meta name="GENERATOR" content="'+ APPNAME+' '+Mainform.AppVersion + '">' + CRLF +
449 ' <meta http-equiv="Content-Type" content="text/html; charset='+GetHTMLCharsetByEncoding(Encoding)+'" />' + CRLF + 440 ' <meta http-equiv="Content-Type" content="text/html; charset='+GetHTMLCharsetByEncoding(Encoding)+'" />' + CRLF +
450 ' <style type="text/css">' + CRLF + 441 ' <style type="text/css">' + CRLF +
451 ' thead tr {background-color: ActiveCaption; color: CaptionText;}' + CRLF + 442 ' thead tr {background-color: ActiveCaption; color: CaptionText;}' + CRLF +
452 ' th, td {vertical-align: top; font-family: "'+Grid.Font.Name+'"; font-size: '+IntToStr(Grid.Font.Size)+'pt; padding: '+IntToStr(Grid.TextMargin-1)+'px; }' + CRLF + 443 ' th, td {vertical-align: top; font-family: "'+Grid.Font.Name+'"; font-size: '+IntToStr(Grid.Font.Size)+'pt; padding: '+IntToStr(Grid.TextMargin-1)+'px; }' + CRLF +
453 ' table, td {border: 1px solid silver;}' + CRLF + 444 ' table, td {border: 1px solid silver;}' + CRLF +
454 ' table {border-collapse: collapse;}' + CRLF; 445 ' table {border-collapse: collapse;}' + CRLF;
455 Col := Grid.Header.Columns.GetFirstVisibleColumn; 446 Col := Grid.Header.Columns.GetFirstVisibleColumn;
456 while Col > NoColumn do begin 447 while Col > NoColumn do begin
457 // Adjust preferred width of columns. 448 // Adjust preferred width of columns.
458 Header := Header + 449 Header := Header +
459 ' thead .col' + IntToStr(Col) + ' {width: ' + IntToStr(Grid.Header.Columns[Col].Width) + 'px;}' + CRLF; 450 ' thead .col' + IntToStr(Col) + ' {width: ' + IntToStr(Grid.Header.Columns[Col].Width) + 'px;}' + CRLF;
460 // Right-justify all cells to match the grid on screen. 451 // Right-justify all cells to match the grid on screen.
461 if Grid.Header.Columns[Col].Alignment = taRightJustify then 452 if Grid.Header.Columns[Col].Alignment = taRightJustify then
462 Header := Header + ' .col' + IntToStr(Col) + ' {text-align: right;}' + CRLF; 453 Header := Header + ' .col' + IntToStr(Col) + ' {text-align: right;}' + CRLF;
463 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 454 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
464 end; 455 end;
465 Header := Header + 456 Header := Header +
466 ' </style>' + CRLF + 457 ' </style>' + CRLF +
467 ' </head>' + CRLF + CRLF + 458 ' </head>' + CRLF + CRLF +
468 ' <body>' + CRLF + CRLF + 459 ' <body>' + CRLF + CRLF +
469 ' <table caption="' + TableName + ' (' + inttostr(NodeCount) + ' rows)">' + CRLF; 460 ' <table caption="' + TableName + ' (' + inttostr(NodeCount) + ' rows)">' + CRLF;
470 if chkColumnHeader.Checked then begin 461 if chkColumnHeader.Checked then begin
471 Header := Header + 462 Header := Header +
472 ' <thead>' + CRLF + 463 ' <thead>' + CRLF +
473 ' <tr>' + CRLF; 464 ' <tr>' + CRLF;
474 Col := Grid.Header.Columns.GetFirstVisibleColumn; 465 Col := Grid.Header.Columns.GetFirstVisibleColumn;
475 while Col > NoColumn do begin 466 while Col > NoColumn do begin
476 if Col <> ExcludeCol then 467 if Col <> ExcludeCol then
477 Header := Header + ' <th class="col' + IntToStr(Col) + '">' + Grid.Header.Columns[Col].Text + '</th>' + CRLF; 468 Header := Header + ' <th class="col' + IntToStr(Col) + '">' + Grid.Header.Columns[Col].Text + '</th>' + CRLF;
478 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 469 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
479 end; 470 end;
480 Header := Header + 471 Header := Header +
481 ' </tr>' + CRLF + 472 ' </tr>' + CRLF +
482 ' </thead>' + CRLF; 473 ' </thead>' + CRLF;
483 end; 474 end;
484 Header := Header + 475 Header := Header +
485 ' <tbody>' + CRLF; 476 ' <tbody>' + CRLF;
486 end; 477 end;
487 478
488 efExcel, efCSV: begin 479 efExcel, efCSV: begin
489 Separator := GridData.Connection.UnescapeString(editSeparator.Text); 480 Separator := GridData.Connection.UnescapeString(editSeparator.Text);
490 Encloser := GridData.Connection.UnescapeString(editEncloser.Text); 481 Encloser := GridData.Connection.UnescapeString(editEncloser.Text);
491 Terminator := GridData.Connection.UnescapeString(editTerminator.Text); 482 Terminator := GridData.Connection.UnescapeString(editTerminator.Text);
492 if chkColumnHeader.Checked then begin 483 if chkColumnHeader.Checked then begin
493 Col := Grid.Header.Columns.GetFirstVisibleColumn; 484 Col := Grid.Header.Columns.GetFirstVisibleColumn;
494 while Col > NoColumn do begin 485 while Col > NoColumn do begin
495 // Alter column name in header if data is not raw. 486 // Alter column name in header if data is not raw.
496 if Col <> ExcludeCol then begin 487 if Col <> ExcludeCol then begin
497 Data := Grid.Header.Columns[Col].Text; 488 Data := Grid.Header.Columns[Col].Text;
498 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then 489 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then
499 Data := 'HEX(' + Data + ')'; 490 Data := 'HEX(' + Data + ')';
500 // Add header item. 491 // Add header item.
501 if Header <> '' then 492 if Header <> '' then
502 Header := Header + Separator; 493 Header := Header + Separator;
503 Header := Header + Encloser + Data + Encloser; 494 Header := Header + Encloser + Data + Encloser;
504 end; 495 end;
505 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 496 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
506 end; 497 end;
507 Header := Header + Terminator; 498 Header := Header + Terminator;
508 end; 499 end;
509 end; 500 end;
510 501
511 efXML: begin 502 efXML: begin
512 Header := '<?xml version="1.0" encoding="'+MainForm.GetCharsetByEncoding(Encoding)+'"?>' + CRLF + CRLF + 503 Header := '<?xml version="1.0" encoding="'+MainForm.GetCharsetByEncoding(Encoding)+'"?>' + CRLF + CRLF +
513 '<table name="'+TableName+'">' + CRLF; 504 '<table name="'+TableName+'">' + CRLF;
514 end; 505 end;
515 506
516 efLaTeX: begin 507 efLaTeX: begin
517 Header := '\begin{tabular}'; 508 Header := '\begin{tabular}';
518 Separator := ' & '; 509 Separator := ' & ';
519 Encloser := ''; 510 Encloser := '';
520 Terminator := '\\ '+CRLF; 511 Terminator := '\\ '+CRLF;
521 Header := Header + '{'; 512 Header := Header + '{';
522 Col := Grid.Header.Columns.GetFirstVisibleColumn; 513 Col := Grid.Header.Columns.GetFirstVisibleColumn;
523 while Col > NoColumn do begin 514 while Col > NoColumn do begin
524 if Col <> ExcludeCol then 515 if Col <> ExcludeCol then
525 Header := Header + ' c '; 516 Header := Header + ' c ';
526 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 517 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
527 end; 518 end;
528 Header := Header + '}' + CRLF; 519 Header := Header + '}' + CRLF;
529 if chkColumnHeader.Checked then begin 520 if chkColumnHeader.Checked then begin
530 Col := Grid.Header.Columns.GetFirstVisibleColumn; 521 Col := Grid.Header.Columns.GetFirstVisibleColumn;
531 while Col > NoColumn do begin 522 while Col > NoColumn do begin
532 if Col <> ExcludeCol then 523 if Col <> ExcludeCol then
533 Header := Header + Grid.Header.Columns[Col].Text + Separator; 524 Header := Header + Grid.Header.Columns[Col].Text + Separator;
534 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 525 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
535 end; 526 end;
536 Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator)); 527 Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator));
537 Header := Header + Terminator; 528 Header := Header + Terminator;
538 end; 529 end;
539 end; 530 end;
540 531
541 efWiki: begin 532 efWiki: begin
542 Separator := ' || '; 533 Separator := ' || ';
543 Encloser := ''; 534 Encloser := '';
544 Terminator := ' ||'+CRLF; 535 Terminator := ' ||'+CRLF;
545 if chkColumnHeader.Checked then begin 536 if chkColumnHeader.Checked then begin
546 Header := '|| '; 537 Header := '|| ';
547 Col := Grid.Header.Columns.GetFirstVisibleColumn; 538 Col := Grid.Header.Columns.GetFirstVisibleColumn;
548 while Col > NoColumn do begin 539 while Col > NoColumn do begin
549 if Col <> ExcludeCol then 540 if Col <> ExcludeCol then
550 Header := Header + '*' + Grid.Header.Columns[Col].Text + '*' + Separator; 541 Header := Header + '*' + Grid.Header.Columns[Col].Text + '*' + Separator;
551 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 542 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
552 end; 543 end;
553 Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator)); 544 Delete(Header, Length(Header)-Length(Separator)+1, Length(Separator));
554 Header := Header + Terminator; 545 Header := Header + Terminator;
555 end; 546 end;
556 end; 547 end;
557 548
558 efPHPArray: begin 549 efPHPArray: begin
559 if radioOutputFile.Checked then 550 if radioOutputFile.Checked then
560 Header := '<?php'+CRLF+'$'+TableName+' = array('+CRLF 551 Header := '<?php'+CRLF+'$'+TableName+' = array('+CRLF
561 else 552 else
562 Header := '$'+TableName+' = array('+CRLF; 553 Header := '$'+TableName+' = array('+CRLF;
563 end; 554 end;
564 555
565 end; 556 end;
566 S.WriteString(Header); 557 S.WriteString(Header);
567 558
568 Node := GetNextNode(Grid, nil, SelectionOnly); 559 Node := GetNextNode(Grid, nil, SelectionOnly);
569 while Assigned(Node) do begin 560 while Assigned(Node) do begin
570 // Update status once in a while. 561 // Update status once in a while.
571 if (Node.Index+1) mod 100 = 0 then begin 562 if (Node.Index+1) mod 100 = 0 then begin
572 Mainform.ShowStatusMsg('Exporting row '+FormatNumber(Node.Index+1)+' of '+FormatNumber(NodeCount)+ 563 Mainform.ShowStatusMsg('Exporting row '+FormatNumber(Node.Index+1)+' of '+FormatNumber(NodeCount)+
573 ' ('+IntToStr(Trunc((Node.Index+1) / NodeCount *100))+'%, '+FormatByteNumber(S.Size)+')' 564 ' ('+IntToStr(Trunc((Node.Index+1) / NodeCount *100))+'%, '+FormatByteNumber(S.Size)+')'
574 ); 565 );
575 MainForm.ProgressStep; 566 MainForm.ProgressStep;
576 end; 567 end;
577 RowNum := Grid.GetNodeData(Node); 568 RowNum := Grid.GetNodeData(Node);
578 GridData.RecNo := RowNum^; 569 GridData.RecNo := RowNum^;
579 570
580 // Row preamble 571 // Row preamble
581 case ExportFormat of 572 case ExportFormat of
582 efHTML: tmp := ' <tr>' + CRLF; 573 efHTML: tmp := ' <tr>' + CRLF;
583 574
584 efXML: tmp := #9'<row>' + CRLF; 575 efXML: tmp := #9'<row>' + CRLF;
585 576
586 efSQLInsert, efSQLReplace: begin 577 efSQLInsert, efSQLReplace: begin
587 if ExportFormat = efSQLInsert then 578 if ExportFormat = efSQLInsert then
588 tmp := 'INSERT' 579 tmp := 'INSERT'
589 else 580 else
590 tmp := 'REPLACE'; 581 tmp := 'REPLACE';
591 tmp := tmp + ' INTO '+GridData.Connection.QuoteIdent(Tablename); 582 tmp := tmp + ' INTO '+GridData.Connection.QuoteIdent(Tablename);
592 if chkColumnHeader.Checked then begin 583 if chkColumnHeader.Checked then begin
593 tmp := tmp + ' ('; 584 tmp := tmp + ' (';
594 Col := Grid.Header.Columns.GetFirstVisibleColumn; 585 Col := Grid.Header.Columns.GetFirstVisibleColumn;
595 while Col > NoColumn do begin 586 while Col > NoColumn do begin
596 if Col <> ExcludeCol then 587 if Col <> ExcludeCol then
597 tmp := tmp + GridData.Connection.QuoteIdent(Grid.Header.Columns[Col].Text)+', '; 588 tmp := tmp + GridData.Connection.QuoteIdent(Grid.Header.Columns[Col].Text)+', ';
598 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 589 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
599 end; 590 end;
600 Delete(tmp, Length(tmp)-1, 2); 591 Delete(tmp, Length(tmp)-1, 2);
601 tmp := tmp + ')'; 592 tmp := tmp + ')';
602 end; 593 end;
603 tmp := tmp + ' VALUES ('; 594 tmp := tmp + ' VALUES (';
604 end; 595 end;
605 596
606 efWiki: tmp := TrimLeft(Separator); 597 efWiki: tmp := TrimLeft(Separator);
607 598
608 efPHPArray: tmp := #9 + 'array( // row #'+FormatNumber(GridData.RecNo)+CRLF; 599 efPHPArray: tmp := #9 + 'array( // row #'+FormatNumber(GridData.RecNo)+CRLF;
609 600
610 else tmp := ''; 601 else tmp := '';
611 end; 602 end;
612 603
613 Col := Grid.Header.Columns.GetFirstVisibleColumn; 604 Col := Grid.Header.Columns.GetFirstVisibleColumn;
614 while Col > NoColumn do begin 605 while Col > NoColumn do begin
615 if Col <> ExcludeCol then begin 606 if Col <> ExcludeCol then begin
616 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then 607 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then
617 Data := GridData.HexValue(Col) 608 Data := GridData.HexValue(Col)
618 else 609 else
619 Data := GridData.Col(Col); 610 Data := GridData.Col(Col);
620 // Keep formatted numeric values 611 // Keep formatted numeric values
621 if (GridData.DataType(Col).Category in [dtcInteger, dtcReal]) 612 if (GridData.DataType(Col).Category in [dtcInteger, dtcReal])
622 and (ExportFormat in [efExcel, efHTML]) then 613 and (ExportFormat in [efExcel, efHTML]) then
623 Data := FormatNumber(Data, False); 614 Data := FormatNumber(Data, False);
624 615
625 case ExportFormat of 616 case ExportFormat of
626 efHTML: begin 617 efHTML: begin
627 // Escape HTML control characters in data. 618 // Escape HTML control characters in data.
628 Data := htmlentities(Data); 619 Data := htmlentities(Data);
629 tmp := tmp + ' <td class="col' + IntToStr(Col) + '">' + Data + '</td>' + CRLF; 620 tmp := tmp + ' <td class="col' + IntToStr(Col) + '">' + Data + '</td>' + CRLF;
630 end; 621 end;
631 622
632 efExcel, efCSV, efLaTeX, efWiki: begin 623 efExcel, efCSV, efLaTeX, efWiki: begin
633 // Escape encloser characters inside data per de-facto CSV. 624 // Escape encloser characters inside data per de-facto CSV.
634 Data := StringReplace(Data, Encloser, Encloser+Encloser, [rfReplaceAll]); 625 Data := StringReplace(Data, Encloser, Encloser+Encloser, [rfReplaceAll]);
635 Data := Encloser + Data + Encloser; 626 Data := Encloser + Data + Encloser;
636 tmp := tmp + Data + Separator; 627 tmp := tmp + Data + Separator;
637 end; 628 end;
638 629
639 efXML: begin 630 efXML: begin
640 // Print cell start tag. 631 // Print cell start tag.
641 tmp := tmp + #9#9'<' + Grid.Header.Columns[Col].Text; 632 tmp := tmp + #9#9'<' + Grid.Header.Columns[Col].Text;
642 if GridData.IsNull(Col) then 633 if GridData.IsNull(Col) then
643 tmp := tmp + ' isnull="true" />' + CRLF 634 tmp := tmp + ' isnull="true" />' + CRLF
644 else begin 635 else begin
645 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then 636 if (GridData.DataType(Col).Category in [dtcBinary, dtcSpatial]) and (not Mainform.actBlobAsText.Checked) then
646 tmp := tmp + ' format="hex"'; 637 tmp := tmp + ' format="hex"';
647 tmp := tmp + '>' + htmlentities(Data) + '</' + Grid.Header.Columns[Col].Text + '>' + CRLF; 638 tmp := tmp + '>' + htmlentities(Data) + '</' + Grid.Header.Columns[Col].Text + '>' + CRLF;
648 end; 639 end;
649 end; 640 end;
650 641
651 efSQLInsert, efSQLReplace: begin 642 efSQLInsert, efSQLReplace: begin
652 if GridData.IsNull(Col) then 643 if GridData.IsNull(Col) then
653 Data := 'NULL' 644 Data := 'NULL'
654 else if GridData.DataType(Col).Index = dtBit then 645 else if GridData.DataType(Col).Index = dtBit then
655 Data := 'b' + esc(Data) 646 Data := 'b' + esc(Data)
656 else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal, dtcBinary, dtcSpatial]) then 647 else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal, dtcBinary, dtcSpatial]) then
657 Data := esc(Data); 648 Data := esc(Data);
658 tmp := tmp + Data + ', '; 649 tmp := tmp + Data + ', ';
659 end; 650 end;
660 651
661 efPHPArray: begin 652 efPHPArray: begin
662 if GridData.IsNull(Col) then 653 if GridData.IsNull(Col) then
663 Data := 'NULL' 654 Data := 'NULL'
664 else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal]) then 655 else if not (GridData.DataType(Col).Category in [dtcInteger, dtcReal]) then
665 Data := esc(Data); 656 Data := esc(Data);
666 if chkColumnHeader.Checked then 657 if chkColumnHeader.Checked then
667 tmp := tmp + #9#9 + '''' + Grid.Header.Columns[Col].Text + ''' => ' + Data + ','+CRLF 658 tmp := tmp + #9#9 + '''' + Grid.Header.Columns[Col].Text + ''' => ' + Data + ','+CRLF
668 else 659 else
669 tmp := tmp + #9#9 + Data + ','+CRLF; 660 tmp := tmp + #9#9 + Data + ','+CRLF;
670 end; 661 end;
671 662
672 end; 663 end;
673 end; 664 end;
674 665
675 Col := Grid.Header.Columns.GetNextVisibleColumn(Col); 666 Col := Grid.Header.Columns.GetNextVisibleColumn(Col);
676 end; 667 end;
677 668
678 // Row epilogue 669 // Row epilogue
679 case ExportFormat of 670 case ExportFormat of
680 efHTML: 671 efHTML:
681 tmp := tmp + ' </tr>' + CRLF; 672 tmp := tmp + ' </tr>' + CRLF;
682 efExcel, efCSV, efLaTeX, efWiki: begin 673 efExcel, efCSV, efLaTeX, efWiki: begin
683 Delete(tmp, Length(tmp)-Length(Separator)+1, Length(Separator)); 674 Delete(tmp, Length(tmp)-Length(Separator)+1, Length(Separator));
684 tmp := tmp + Terminator; 675 tmp := tmp + Terminator;
685 end; 676 end;
686 efXML: 677 efXML:
687 tmp := tmp + #9'</row>' + CRLF; 678 tmp := tmp + #9'</row>' + CRLF;
688 efSQLInsert, efSQLReplace: begin 679 efSQLInsert, efSQLReplace: begin
689 Delete(tmp, Length(tmp)-1, 2); 680 Delete(tmp, Length(tmp)-1, 2);
690 tmp := tmp + ');' + CRLF; 681 tmp := tmp + ');' + CRLF;
691 end; 682 end;
692 efPHPArray: 683 efPHPArray:
693 tmp := tmp + #9 + '),' + CRLF; 684 tmp := tmp + #9 + '),' + CRLF;
694 end; 685 end;
695 S.WriteString(tmp); 686 S.WriteString(tmp);
696 687
697 Node := GetNextNode(Grid, Node, SelectionOnly); 688 Node := GetNextNode(Grid, Node, SelectionOnly);
698 end; 689 end;
699 690
700 // Footer 691 // Footer
701 case ExportFormat of 692 case ExportFormat of
702 efHTML: begin 693 efHTML: begin
703 tmp := 694 tmp :=
704 ' </tbody>' + CRLF + 695 ' </tbody>' + CRLF +
705 ' </table>' + CRLF + CRLF + 696 ' </table>' + CRLF + CRLF +
706 ' <p>' + CRLF + 697 ' <p>' + CRLF +
707 ' <em>generated ' + DateToStr(now) + ' ' + TimeToStr(now) + 698 ' <em>generated ' + DateToStr(now) + ' ' + TimeToStr(now) +
708 ' by <a href="'+APPDOMAIN+'">' + APPNAME + ' ' + Mainform.AppVersion + '</a></em>' + CRLF + 699 ' by <a href="'+APPDOMAIN+'">' + APPNAME + ' ' + Mainform.AppVersion + '</a></em>' + CRLF +
709 ' </p>' + CRLF + CRLF + 700 ' </p>' + CRLF + CRLF +
710 ' </body>' + CRLF + 701 ' </body>' + CRLF +
711 '</html>' + CRLF; 702 '</html>' + CRLF;
712 end; 703 end;
713 efXML: 704 efXML:
714 tmp := '</table>' + CRLF; 705 tmp := '</table>' + CRLF;
715 efLaTeX: 706 efLaTeX:
716 tmp := '\end{tabular}' + CRLF; 707 tmp := '\end{tabular}' + CRLF;
717 efPHPArray: begin 708 efPHPArray: begin
718 tmp := ');' + CRLF; 709 tmp := ');' + CRLF;
719 if radioOutputFile.Checked then 710 if radioOutputFile.Checked then
720 tmp := tmp + '?>'; 711 tmp := tmp + '?>';
721 end; 712 end;
722 else 713 else
723 tmp := ''; 714 tmp := '';
724 end; 715 end;
725 S.WriteString(tmp); 716 S.WriteString(tmp);
726 717
727 if radioOutputCopyToClipboard.Checked then begin 718 if radioOutputCopyToClipboard.Checked then begin
728 // SynEdit's exporter is slow on large strings, see issue #2903 719 // SynEdit's exporter is slow on large strings, see issue #2903
729 if S.Size < 100*SIZE_KB then begin 720 if S.Size < 100*SIZE_KB then begin
730 case ExportFormat of 721 case ExportFormat of
731 efSQLInsert, efSQLReplace: begin 722 efSQLInsert, efSQLReplace: begin
732 Exporter := TSynExporterHTML.Create(Self); 723 Exporter := TSynExporterHTML.Create(Self);
733 Exporter.Highlighter := MainForm.SynSQLSyn1; 724 Exporter.Highlighter := MainForm.SynSQLSyn1;
734 Exporter.ExportAll(Explode(CRLF, S.DataString)); 725 Exporter.ExportAll(Explode(CRLF, S.DataString));
735 HTML := TMemoryStream.Create; 726 HTML := TMemoryStream.Create;
736 Exporter.SaveToStream(HTML); 727 Exporter.SaveToStream(HTML);
737 Exporter.Free; 728 Exporter.Free;
738 end; 729 end;
739 efHTML: HTML := S; 730 efHTML: HTML := S;
740 else HTML := nil; 731 else HTML := nil;
741 end; 732 end;
742 end; 733 end;
743 StreamToClipboard(S, HTML, (ExportFormat=efHTML) and (HTML <> nil)); 734 StreamToClipboard(S, HTML, (ExportFormat=efHTML) and (HTML <> nil));
744 end else begin 735 end else begin
745 try 736 try
746 S.SaveToFile(editFilename.Text); 737 S.SaveToFile(editFilename.Text);
747 except 738 except
748 on E:EFCreateError do begin 739 on E:EFCreateError do begin
749 // Keep form open if file cannot be created 740 // Keep form open if file cannot be created
750 ModalResult := mrNone; 741 ModalResult := mrNone;
751 MainForm.SetProgressState(pbsError); 742 MainForm.SetProgressState(pbsError);
752 ErrorDialog(E.Message); 743 ErrorDialog(E.Message);
753 end; 744 end;
754 end; 745 end;
755 end; 746 end;
756 747
757 Mainform.DisableProgress; 748 Mainform.DisableProgress;
758 Mainform.ShowStatusMsg('Freeing data...'); 749 Mainform.ShowStatusMsg('Freeing data...');
759 FreeAndNil(S); 750 FreeAndNil(S);
760 Mainform.ShowStatusMsg; 751 Mainform.ShowStatusMsg;
761 Screen.Cursor := crDefault; 752 Screen.Cursor := crDefault;
762 end; 753 end;
763 754
764 755
765 end. 756 end.
Powered by Google Project Hosting