Dbgrideh Export Function Details (savedbgridehtoexportfile description)
---
There are often user requests for data export, for which we have been using the most primitive way, open the query, read each field of data, one by one, write the file. As follows:
Procedure Tform10.prexpdata (Pmode:byte);
Var
pfpath:string;
pfname:string; Exp Name of prefix
pfobject:string;
Pmyfile:textfile;
pstr:string;
I:integer;
Begin
pfobject:= ' C:\Exp_Sample.csv ';
AssignFile (Pmyfile,pfobject);
ReWrite (Pmyfile);
With DM. qr_enjbtr_250 DO begin
CLOSE;
Open
Write title
For I:=0 to DM. Qr_enjbtr_250.fieldcount-1 DO begin
PSTR:=PSTR+DM. Qr_enjbtr_250.fielddefs.items[i]. Name+ ', ';
End
Writeln (PMYFILE,PSTR);
Write content
While don't EOF do begin
Pstr:= ";
For i:=0 to FieldCount-1 do begin
Pstr:=pstr+fields.fields[i]. Asstring+ ', ';
End
Copy (Pstr,1,length (PSTR)-1);
Writeln (PMYFILE,PSTR);
Next;
End
End With end;
Xmsg:= '---> Successfully exported purpart schedule: ' +pfobject; Cpwritelog (x_plogfile,xmsg,1); V2033 ADD
CloseFile (Pmyfile);
End
later found that the Dbgrideh control provides a process: Savedbgridehtoexportfile, provides a good export function, You can easily export the data you have queried to a file in a format such as txt,csv,html.
procedure Savedbgridehtoexportfile (Exportclass: Tdbgridehexportclass; Dbgrideh:tcustomdbgrideh; Const filename:string; Issaveall:boolean);
where:
exportclass--export file type, can be the following values: Tdbgridehexportastext, Tdbgridehexportascsv, tdbgridehexportashtml, Tdbgridehexportasrtf, Tdbgridehexportasvcldbif,tdbgridehexportasxls.
Dbgrideh--dbgrideh control
filename--file path name to export
issaveall--whether to export all data: Y-export all data; N: Export Select part of the data.
Attention:
You must add a new reference unit when you use it: Dbgridehimpexp (The following two reference units must also exist: Dbgrideh, Ehlibbde).
Example:
Savedbgridehtoexportfile (pexpclass,dbgmain,pexpfile,true); Export all data.
Savedbgridehtoexportfile (Pexpclass,tdbgrideh (ActiveControl), pexpfile,false); Export Selection Data
+++
Use example:
---
environment: Delphi Version6.0[build 6.163]+ehlibv3.4
ready to be introduced as follows:
1, in form uses, Add a new reference cell: Dbgridehimpexp (The following two reference units must also exist:dbgrideh, Ehlibbde).
savedialog1.filter:= ' Text files (*.txt) |*. Txt| Comma Separated Values (*.csv) |*. csv| HTML file (*.htm) |*. htm| Rich Text Format (*.rtf) |*. Rtf| Microsoft Excel Workbook (*.xls) |*. XLS ';
savedialog1.filterindex:=0;
6, add two buttons, for all export and partial export, add the following code to the button's click event respectively.
All export process
Procedure Tform1.btnallexportclick (Sender:tobject);
Var
Pexpclass:tdbgridehexportclass; Export File types
pext:string; Export file name extension
pexpfile:string; Export file path and file name
pexpfileext:string; SaveDialog1 the name extension of the returned file
Begin
Empty SaveDialog1 Default file name
Savedialog1.filename: = ";
If Savedialog1.execute then BEGIN//If SAVEDIALOG1 is executed correctly
Case Savedialog1.filterindex of//export file type and extension according to FilterIndex settings
1:begin Pexpclass: = Tdbgridehexportastext; Pext: = ' txt '; End
2:begin Pexpclass: = tdbgridehexportascsv; Pext: = ' csv '; End
3:begin Pexpclass: = tdbgridehexportashtml; Pext: = ' htm '; End
4:begin Pexpclass: = tdbgridehexportasrtf; Pext: = ' rtf '; End
5:begin Pexpclass: = Tdbgridehexportasxls; Pext: = ' xls '; End
Else
Pexpclass: = nil; Pext: = ";
End
If Pexpclass <> nil then BEGIN//If the export file type has been set correctly
Pexpfile:= trim (savedialog1.filename);
pexpfileext:= Copy (Pexpfile,length (pexpfile) -2,3);
Determine whether the returned file name already contains the correct extension, and if not, add the correct extension
If uppercase (Pexpfileext) <> uppercase (Pext) then
Pexpfile: = Pexpfile + '. ' + pext;
Savedbgridehtoexportfile (pexpclass,dbgrideh1,pexpfile,true); Export all data by existing settings.
X_pmsg:= ' The data has saved as ' +pexpfile;
Stbform.panels[0]. text:=x_pmsg;
Messagedlg (' The data has saved as ' +pexpfile,mtinformation,[mbok],0);
End
End
End
/ /partial export process
procedure Tform1.btnallexportclick (Sender:tobject);
var
pexpclass:tdbgridehexportclass; //Export file type
pext:string; // Export file extension
pexpfile:string; //Export file path and file name
pexpfileext:string; //savedialog1 Returns the file name extension
begin
//empty SaveDialog1 Default file name
Savedialog1.filename: = ';
If Savedialog1.execute then BEGIN//If SAVEDIALOG1 is executed correctly
Case Savedialog1.filterindex of//export file type and extension according to FilterIndex settings
1:begin Pexpclass: = Tdbgridehexportastext; Pext: = ' txt '; End
2:begin Pexpclass: = tdbgridehexportascsv; Pext: = ' csv '; End
3:begin Pexpclass: = tdbgridehexportashtml; Pext: = ' htm '; End
4:begin Pexpclass: = tdbgridehexportasrtf; Pext: = ' rtf '; End
5:begin Pexpclass: = Tdbgridehexportasxls; Pext: = ' xls '; End
Else
Pexpclass: = nil; Pext: = ";
End
If Pexpclass <> nil then BEGIN//If the export file type has been set correctly
Pexpfile:= trim (savedialog1.filename);
pexpfileext:= Copy (Pexpfile,length (pexpfile) -2,3);
Determine whether the returned file name already contains the correct extension, and if not, add the correct extension
If uppercase (Pexpfileext) <> uppercase (Pext) then
Pexpfile: = Pexpfile + '. ' + pext;
Savedbgridehtoexportfile (Pexpclass,dbgrideh1,pexpfile,false); //export all data by existing settings.
//X_pMsg:= ' the Data has saved as ' +pexpfile;
// Stbform.panels[0]. text:=x_pmsg;
MessageDlg (' the Data has saved as ' +pexpfile,mtinformation,[mbok],0);
end;
end;
end;