Home > Back-end >  Using Delphi database data export to excel method does not perform
Using Delphi database data export to excel method does not perform

Time:10-06

There is an error is:


Perform to the following page


Then write down the name, click on the print button is error

The unit Unit6.

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids StdCtrls, ExtCtrls;

Type
TForm6=class (TForm)
For: TButton;
Button2: TButton;
DBGrid1: TDBGrid;
SaveDialog1: TSaveDialog;
Procedure Button2Click (Sender: TObject);
Procedure Button1Click (Sender: TObject);
Private
{Private declarations}

Public
{Public declarations}
end;

Var
Form6: TForm6;
ArXlsBegin: array [0.. 5] of Word=($809, 8, 0, $10, 0, 0).
ArXlsEnd: array [0.. 1] of Word=($0. A, 00);
ArXlsString: array [0.. 5] of Word=($204, 0, 0, 0, 0, 0).
ArXlsNumber: array [0.. 4] of Word=($203, 14, 0, 0, 0).
ArXlsInteger: array [0.. 4] of Word=($27 e, 10, 0, 0, 0).
ArXlsBlank: array [0.. 4] of Word=($201, 6, 0, 0, $17);

Implementation

{$R *. DFM}
USES Unit2, Unit3, Unit5;

Procedure ExportExcelFile (FileName: string; BWriteTitle: Boolean; ADataSet: TDataSet);
Var
I, j: integer;
Col, row: word;
ABookMark: TBookMark;
AFileStream: TFileStream;
Procedure incColRow;//add ranks number
The begin
Then the if Col=ADataSet. FieldCount - 1
The begin
Inc (Row);
Col:=0;
End
The else
Inc (Col);
end;
Procedure WriteStringCell (AValue: string);//write string data
Var
L: Word;
The begin
L:=Length (AValue);
ArXlsString [1] :=8 + L;
ArXlsString [2] :=Row;
ArXlsString [3] :=Col;
ArXlsString [5] :=L;
ShowMessage (' 1111111111111111111111 ');
AFileStream. WriteBuffer (arXlsString, SizeOf (arXlsString));
AFileStream. WriteBuffer (Pointer (AValue) ^, L);
IncColRow;
end;
Procedure WriteIntegerCell (AValue: integer);//write integer
Var
V: Integer;
The begin
ArXlsInteger [2] :=Row;
ArXlsInteger [3] :=Col;
ShowMessage (' 2222222222222222222 ');
AFileStream. WriteBuffer (arXlsInteger, SizeOf (arXlsInteger));
V:=(AValue SHL 2) or 2;
AFileStream. WriteBuffer (V, 4);
IncColRow;
end;
Procedure WriteFloatCell (AValue: double);//write floating-point
The begin
ArXlsNumber [2] :=Row;
ArXlsNumber [3] :=Col;
ShowMessage (' 3333333333333333333333 ');
AFileStream. WriteBuffer (arXlsNumber, SizeOf (arXlsNumber));
AFileStream. WriteBuffer (AValue, 8);
IncColRow;
end;
The begin
If FileExists (FileName) then
The DeleteFile (FileName);//file exists, first remove
AFileStream:=TFileStream. Create (FileName, fmCreate);
Try
//write file header
AFileStream. WriteBuffer (arXlsBegin, SizeOf (arXlsBegin));
//write column head
Col:=0; Row:=0;
If bWriteTitle then
The begin
For I:=0 to aDataSet. Do FieldCount - 1
WriteStringCell (aDataSet Fields [I] FieldName);
end;
//write data centralized data
ADataSet. DisableControls;
ABookMark:=aDataSet. GetBookmark;
ADataSet. First;
While not aDataSet. Eof do
The begin
For I:=0 to aDataSet. Do FieldCount - 1
Case ADataSet. Fields [I]. The DataType of the
FtSmallint, ftInteger, ftWord ftAutoInc, ftBytes:
WriteIntegerCell (aDataSet Fields [I] AsInteger);
FtFloat ftCurrency, ftBCD:
WriteFloatCell (aDataSet Fields [I] AsFloat)
The else
WriteStringCell (aDataSet Fields [I] AsString) are identical.
end;
ADataSet. Next;
end;
//write end-of-file
AFileStream. WriteBuffer (arXlsEnd, SizeOf (arXlsEnd));
If ADataSet. BookmarkValid (ABookMark) then
ADataSet. GotoBookmark (ABookMark);
The finally
AFileStream. Free;
ADataSet. EnableControls;
end;
end;

Procedure TForm6. Button1Click (Sender: TObject);
The begin
DataModule2. ADOQuery1. Close;
DataModule2. ADOQuery1. SQL. The Clear;
DataModule2. ADOQuery1. SQL. The Add (' select * from the items');
DataModule2. ADOQuery1. Open;
If DataModule2. ADOQuery1. RecordCount<> 0 then
The begin
If SaveDialog1. Execute then
The begin
ExportExcelFile (SaveDialog1 FileName, True, DataModule2. ADOQuery1. The DataSource. The DataSet).
end;
end;

end;

Procedure TForm6. Button2Click (Sender: TObject);
The begin
Application CreateForm (TForm5 Form5);
Form6. Free;
end;

end.

CodePudding user response:

Single step debugging and see where it is wrong, save the XLS, data in English a semicolon, suffix is called *. XLS, then use excel to open, is ok,

CodePudding user response:

In fact you can directly use DBGRIDEH form controls, use the following statement, a code is to export, the premise is first USES DBGridEhImpExp;
Use the save dialog to do path:
SaveDBGridEhToExportFile (TDBGridEhExportAsXLS DBGridEh1, dlgSave1 FileName, True);

If it wants to achieve other export way, such as custom formatting of what, I'll be able to write you one.

CodePudding user response:

The best to be debugged

CodePudding user response:

nullnullnull
  • Related