Home > Back-end >  Now projects using file stream to write to export EXCEL cell.
Now projects using file stream to write to export EXCEL cell.

Time:11-07

Online instance has many, but no operation of the cell, a little regret, online example: but the EXCEL cell how to write?
Var ExcelApp Variant;
ExcelApp. ActiveSheet. Range [sheet. Cells [1, 1], sheet. The cells [1, 19]], the Font, Bold:=True;//bold
ExcelApp. ActiveSheet. Range [sheet. Cells [1, 1], sheet. The cells [1, 19]], the Font, size:=18;//size
ExcelApp. Selection. HorizontalAlignment:=xlCenter;//center
ExcelApp. Selection. MergeCells:=True;
ExcelApp. ActiveSheet. Rows [1]. The Font, Bold:=True;
-- -- -- -- -- -- -- -- -- -- - more than how to use the file stream to write EXCEL cell ?
For online example below:

Procedure ExportExcelFile (FileName: string; BWriteTitle: Boolean; ADataSet: TDataSet);
Var I, j: integer;
Col, row: word;
ABookMark: TBookMark;
AFileStream: TFileStream;
Procedure incColRow;//increase ranks number to control the position of the row and column
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 arXlsString [2] :=Row; ArXlsString [3] :=Col;
Var: L Word;
The begin
L:=Length (AValue);
ArXlsString [1] :=8 + L;
ArXlsString [2] :=Row;
ArXlsString [3] :=Col;
ArXlsString [5] :=L;
AFileStream. WriteBuffer (arXlsString, SizeOf (arXlsString));
AFileStream. WriteBuffer (Pointer (AValue) ^, L);
IncColRow;
end;
Procedure WriteIntegerCell (AValue: integer);//write integer arXlsInteger [2] :=Row; ArXlsInteger [3] :=Col;
Var V: Integer;
The begin
ArXlsInteger [2] :=Row;
ArXlsInteger [3] :=Col;
AFileStream. WriteBuffer (arXlsInteger, SizeOf (arXlsInteger));
V:=(AValue SHL 2) or 2;
AFileStream. WriteBuffer (V, 4);
IncColRow;
end;
Procedure WriteFloatCell (AValue: double);//write floating-point arXlsNumber [2] :=Row; ArXlsNumber [3] :=Col;
The begin
ArXlsNumber [2] :=Row;
ArXlsNumber [3] :=Col;
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;//while
//write end-of-file
AFileStream. WriteBuffer (arXlsEnd, SizeOf (arXlsEnd));
If ADataSet. BookmarkValid (ABookMark) then
ADataSet. GotoBookmark (ABookMark);
The finally
AFileStream. Free;
ADataSet. EnableControls;
end;//try
end;
  • Related