Dbgrideh export function (savedbgridehtoexportfile description)

Source: Internet
Author: User

Dbgrideh export function (savedbgridehtoexportfile description)
---
Users often require data export. In this regard, we always use the most primitive method to open the query, read the data of each field, and write the data one by one. 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 the 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 not EOF do begin
Pstr: = '';
For I: = 0 to a 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 the purpart list:' + pfobject; cpwritelog (x_plogfile, xmsg, 1); // v2033 add
Closefile (pmyfile );
End;

Later, we found that the dbgrideh control provides a process: savedbgridehtoexportfile, which provides a good export function to conveniently export the queried data to files in txt, CSV, HTML, and other formats.
Description:
Procedure savedbgridehtoexportfile (exportclass: tdbgridehexportclass; dbgrideh: tcustomdbgrideh; const filename: string; issaveall: Boolean );
Where:
Exportclass -- type of the exported file, which can be tdbgridehexportastext, tdbgridehexportascsv, tdbgridehexportashtml, tdbgridehexportasrtf, expires, tdbgridehexportasxls.
Dbgrideh -- dbgrideh Control
Filename -- name of the file path to be exported
Issaveall -- whether to export all data: Y-export all data; N: Export selected data.

Note:
A new reference unit must be added for use: 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 selected data

++
Example:
---
Environment: Delphi version6.0 [Build 6.163] + ehlibv3.4

The preparation is as follows:
1. In the form uses, add a new reference unit: dbgridehimpexp (the following two reference units must also exist: dbgrideh, ehlibbde ).
2. Add a dbgrideh control to the form and set the name to dbgmain (which can be named at will ).
3. Set the dbgrideh control to multiple options: Set options-dgmultiselect = true on the property page. In order to select a range and export some data.
4. Specify the datasource data source for the dbgrideh control.
5. Add the tsavedialog control savedialog1 to set the file type list and the default file type, as shown in the following figure (you can also set it directly on the property page ):
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 the following code to the Click Event of the button for all export and partial export respectively.

// All export processes
Procedure tform1.btnallexportclick (Sender: tobject );
VaR
Pexpclass: tdbgridehexportclass; // export file type
Pext: string; // export the file extension.
Pexpfile: string; // export file path and file name
Pexpfileext: string; // The object name extension returned by savedialog1
Begin
// Clear the default savedialog1 file name
Savedialog1.filename: = '';

If savedialog1.execute then begin // If savedialog1 is correctly executed
Case savedialog1.filterindex of // you can specify the file type and extension based on filterindex.
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: = 'text'; end;
5: Begin pexpclass: = tdbgridehexportasxls; pext: = 'xls '; end;
Else
Pexpclass: = nil; pext: = '';
End;
If pexpclass <> nil then begin // If the exported file type has been set correctly
Pexpfile: = trim (savedialog1.filename );
Pexpfileext: = copy (pexpfile, length (pexpfile)-2, 3 );
// Determine whether the returned file name contains the correct extension. If not, add the correct extension.
If uppercase (pexpfileext) <> uppercase (pext) then
Pexpfile: = pexpfile + '.' + pext;

Savedbgridehtoexportfile (pexpclass, dbgrideh1, pexpfile, true); // export all data according to the existing settings.
// X_pmsg: = 'the data have saved as '+ pexpfile;
// Stbform. Panels [0]. Text: = x_pmsg;
Messagedlg ('the data have saved as '+ pexpfile, mtinformation, [mbok], 0 );
End;
End;

End;

// Partial export process
Procedure tform1.btnpartexportclick (Sender: tobject );
VaR
Pexpclass: tdbgridehexportclass; // export file type
Pext: string; // export the file extension.
Pexpfile: string; // export file path and file name
Begin
Savedialog1.filename: = '';
If (activecontrol is tdbgrideh) then

If savedialog1.execute then begin
Case savedialog1.filterindex
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: = 'text'; end;
5: Begin pexpclass: = tdbgridehexportasxls; pext: = 'xls '; end;
Else
Pexpclass: = nil; pext: = '';
End;

If pexpclass <> nil then begin
Pexpfile: = trim (savedialog1.filename );
If uppercase (copy (pexpfile, length (pexpfile)-2, 3) <> uppercase (pext) then
Pexpfile: = pexpfile + '.' + pext;

Savedbgridehtoexportfile (pexpclass, tdbgrideh (activecontrol), pexpfile, false );
X_pmsg: = 'the data have saved as '+ pexpfile;
Stbform. Panels [0]. Text: = x_pmsg;
Messagedlg (x_pmsg, mtinformation, [mbok], 0 );
End;
End;

End;

 


-- This article comes from [TTT blog]: http://www.taoyoyo.net/ttt/post/114.html

 

(Note: You must retain the copyright information or indicate the source when reprinting .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.