Home > Back-end >  How is the result of Delphi database query export to excel
How is the result of Delphi database query export to excel

Time:09-18

Hello, everyone, I am a little white, never use the Delphi, but what do you want to be a fool any specific database query software, use the Delphi, now learn a day, to execute the query, but, what I need now is to these queries to the data export to EXCEL in the table, I am a little white, totally do not understand, so I hope can detailed, if you can by tutorial is best, thank you,




This is the figure is ready, I can connect to the database, and click on the query also can query to the data,
Now the only problem is to realize the click of the realization of the function of the export to EXCEL, the query to the data direct export to EXCEL table, excuse me, how to do?
I am a little white, no learned the language,



And then I downloaded a uExportXls elsewhere. Pas files, but don't know how to
Use, also can not find the corresponding online video, tutorials, so only ran to ask everyone here, thanks again!!!!!
 {background: today the DataSet into Excel, query the previous posts, part with Ole, too slow, 
Part of using ADO to connect to the Excel file, also slowly, in a pet changed DBGrigEh export parts,
Welcome to teach and improve,
Function: the data sets of data into Excel;
Usage: With ExportXls. Create (TDataSet (ADOQuery1) do
Try
Save2File (SaveDialog1 FileName, True);
The finally
Free;
end;
Author: Caidao (core code from Ehlib)
Time: 2003-04-09
Location: shantou
}

The unit uExportXls;

Interface

USES the
The DB, Classes;

Var
CXlsBof: array [0.. 5] of Word=($809, 8, 0, $10, 0, 0).
CXlsEof: array [0.. 1] of Word=($0. A, 00);
CXlsLabel: array [0.. 5] of Word=($204, 0, 0, 0, 0, 0).
CXlsNumber: array [0.. 4] of Word=($203, 14, 0, 0, 0).
CXlsRk: array [0.. 4] of Word=($27 e, 10, 0, 0, 0).
CXlsBlank: array [0.. 4] of Word=($201, 6, 0, 0, $17);

Type
TFldRec=record
Title: the string;
Width: Integer;
end;

ExportXls=class (TObject)
Private
FCol: word;
FRow: word;
FDataSet: TDataSet;
Stream: TStream;
FWillWriteHead: Boolean;
FBookMark: TBookmark;
Procedure IncColRow;
Procedure WriteBlankCell;
Procedure WriteFloatCell (const AValue: Double);
Procedure WriteIntegerCell (const AValue: Integer);
Procedure WriteStringCell (const AValue: string);
Procedure WritePrefix;
Procedure WriteSuffix;
Procedure WriteTitle;
Procedure WriteDataCell;

Procedure Save2Stream (aStream: TStream);
Public
Procedure Save2File (FileName: string; WillWriteHead: Boolean);
The constructor Create (aDataSet: TDataSet);
end;
The function ExportToXLS (const FileName: string; The DataSet: TDataSet) : Boolean;
Implementation

USES the SysUtils;

The function ExportToXLS (const FileName: string; The DataSet: TDataSet) : Boolean;
The begin
Result:=False;
With ExportXls. Create (DataSet) do try
Save2File (FileName, True);
Result:=True;
The finally
Free;
end;
end;

The constructor ExportXls. Create (aDataSet: TDataSet);
The begin
Inherited the Create;
FDataSet:=aDataSet;
end;

Procedure ExportXls. IncColRow;
The begin
If FCol=FDataSet. FieldCount - 1 then the begin
Inc (FRow);
FCol:=0;
End
The else
Inc (FCol);
end;

Procedure ExportXls. WriteBlankCell;
The begin
CXlsBlank [2] :=FRow;
CXlsBlank [3] :=FCol;
Stream. WriteBuffer (CXlsBlank, SizeOf (CXlsBlank));
IncColRow;
end;

Procedure ExportXls. WriteFloatCell (const AValue: Double);
The begin
CXlsNumber [2] :=FRow;
CXlsNumber [3] :=FCol;
Stream. WriteBuffer (CXlsNumber, SizeOf (CXlsNumber));
Stream. WriteBuffer (AValue, 8);
IncColRow;
end;

Procedure ExportXls. WriteIntegerCell (const AValue: Integer);
Var
V: Integer;
The begin
CXlsRk [2] :=FRow;
CXlsRk [3] :=FCol;
Stream. WriteBuffer (CXlsRk, SizeOf (CXlsRk));
V:=(AValue SHL 2) or 2;
Stream. WriteBuffer (V, 4);
IncColRow;
end;

Procedure ExportXls. WriteStringCell (const AValue: string).
Var
L: Word;
The begin
L:=Length (AValue);
CXlsLabel [1] :=8 + L;
CXlsLabel [2] :=FRow;
CXlsLabel [3] :=FCol;
CXlsLabel [5] :=L;
Stream. WriteBuffer (CXlsLabel, SizeOf (CXlsLabel));
Stream. WriteBuffer (Pointer (AValue) ^, L);
IncColRow;
end;

Procedure ExportXls. WritePrefix;
The begin
Stream. WriteBuffer (CXlsBof, SizeOf (CXlsBof));
end;

Procedure ExportXls. WriteSuffix;
The begin
Stream. WriteBuffer (CXlsEof, SizeOf (CXlsEof));
end;

Procedure ExportXls. WriteTitle;
Var
N: word;
The begin
For n:=0 to FDataSet. Do FieldCount - 1
WriteStringCell (FDataSet Fields [n]. DisplayLabel);//show the tag name
end;

Procedure ExportXls. WriteDataCell;
Var
Independence Idx: word;
The begin
WritePrefix;
If FWillWriteHead then WriteTitle;
FDataSet. DisableControls;
FBookMark:=FDataSet. GetBookmark;
FDataSet. First;
While not FDataSet. Eof do begin
For independence Idx:=0 to FDataSet. Do the begin FieldCount - 1
If FDataSet. Fields [independence Idx] IsNull then
WriteBlankCell
The else begin
Case FDataSet. Fields [independence Idx]. The DataType of the
FtSmallint, ftInteger, ftWord ftAutoInc, ftBytes:
WriteIntegerCell (FDataSet Fields [r]. Independence Idx AsInteger);
FtFloat ftCurrency, ftBCD:
WriteFloatCell (FDataSet Fields [r]. Independence Idx AsFloat);
The else
If Assigned (FDataSet Fields [r]. Independence Idx OnGetText) then
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related