Data Export Excel's EXCEL (no format requirements)

xiaoxiao2021-03-06  146

Export data to Excel with Strlist's SaveTofile method is implemented for unformatted requirements

//Implementation process

// DataSet is a data set

// Grid is used to get an Excel column title

// filename is the save path and file name

Procedure DatasettoExcel (DataSet: TDataSet; Grid: TDBGRID; FileName: String);

VAR

i: integer;

Str: string;

Strlist: tstringlist; // The character list for storing data

Begin

Try

Strlist: = TSTRINGLIST.CREATE;

// field

Str: = '';

For i: = 0 to dataset.fieldcount-1 do

Begin

Str: = Str Grid.columns [i] .title.caption # 9;

END;

Strlist.Add (STR);

With dataset do

Begin

First; / / pointer to the head

DisableControls;

While not Eof do

Begin

Str: = '';

For i: = 0 to Fieldcount-1 DO

Begin

Str: = Str Vartostr (dataset.fields [i] .value) # 9;

END;

Strlist.Add (STR);

NEXT;

END;

Strlist.savetofile (filename); // Storage path

EnableControls;

END;

Finally

Strlist.free; // Release memory

END;

END;

// call method

Procedure TaccountQueryFrm.ButtoneXportClick (Sender: TOBJECT);

VAR

Savedialog: TsaveDialog;

Begin

Try

Savedialog: = TsaveDialog.create (Self);

Savedialog.Filter: = '*. Xls';

Savedialog.FileName: = 'ExcelfileName';

Try

If Savedialog.execute THEN

Begin

DatasettoExcel (ClientDataSet1, DBGrid1, Savedialog.FileName);

Application.MessageBox (PCHAR ('Excel Table is saved!'), 'Operation Tips', MB_OK MB_ICONITIONFORMATION

END;

Except

Application.MessageBox (Pchar ('Save Failed!'), 'Error Tip', MB_OK MB_ICONERROR);

END;

Finally

Savedialog.Free;

END;

END;

转载请注明原文地址:https://www.9cbs.com/read-128462.html

New Post(0)