-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUExcel.pas
146 lines (121 loc) · 3.76 KB
/
UExcel.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
unit UExcel;
// Native methods for writing a simple Excel file
// Based on source by Fatih Olcer published in this article:
// http://www.swissdelphicenter.ch/en/showcode.php?id=725
interface
uses
classes, SysUtils;
type
WriteXLS = class
private
XLSStream: TFileStream;
procedure XlsBeginStream(_XlsStream: TStream; const BuildNumber: Word);
procedure XlsEndStream(_XlsStream: TStream);
public
function xlsopen(filename: string):boolean;
procedure xlsclose;
// Integer
procedure XlsWriteCellRk(const ACol, ARow: Word;
const AValue: Integer);
// Floating point number
procedure XlsWriteCellNumber(const ACol, ARow: Word;
const AValue: Double);
// String
procedure XlsWriteCellLabel(const ACol, ARow: Word;
const AValue: AnsiString);
end;
implementation
var
CXlsBof: array[0..5] of Word = ($809, 8, 00, $10, 0, 0);
CXlsEof: array[0..1] of Word = ($0A, 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 = ($27E, 10, 0, 0, 0);
procedure WriteXLS.XlsBeginStream(_XlsStream: TStream; const BuildNumber: Word);
begin
CXlsBof[4] := BuildNumber;
_XlsStream.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
end;
procedure WriteXLS.XlsEndStream(_XlsStream: TStream);
begin
_XlsStream.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
end;
procedure WriteXLS.XlsWriteCellRk(const ACol, ARow: Word;
const AValue: Integer);
var
V: Integer;
begin
CXlsRk[2] := ARow;
CXlsRk[3] := ACol;
XlsStream.WriteBuffer(CXlsRk, SizeOf(CXlsRk));
V := (AValue shl 2) or 2;
XlsStream.WriteBuffer(V, 4);
end;
procedure WriteXLS.XlsWriteCellNumber(const ACol, ARow: Word;
const AValue: Double);
begin
CXlsNumber[2] := ARow;
CXlsNumber[3] := ACol;
XlsStream.WriteBuffer(CXlsNumber, SizeOf(CXlsNumber));
XlsStream.WriteBuffer(AValue, 8);
end;
procedure WriteXLS.XlsWriteCellLabel(const ACol, ARow: Word;
const AValue: AnsiString);
var
L: Word;
begin
L := Length(AValue);
CXlsLabel[1] := 8 + L;
CXlsLabel[2] := ARow;
CXlsLabel[3] := ACol;
CXlsLabel[5] := L;
XlsStream.WriteBuffer(CXlsLabel, SizeOf(CXlsLabel));
XlsStream.WriteBuffer(Pointer(AValue)^, L);
end;
function WriteXLS.xlsopen(filename: string): boolean;
begin
XlsStream := TFileStream.Create(filename, fmCreate);
try
XlsBeginStream(XlsStream, 0);
result := true;
except
freeandnil(XlsStream);
result := false;
end;
end;
procedure WriteXLS.xlsclose;
begin
XlsEndStream(XlsStream);
freeandnil(XlsStream);
end;
// Links for Excel file reading and writing in Delphi
// Delphi 3 and Automation with Excel. --> Includes wrapper class!
// http://vzone.virgin.net/graham.marshall/excel.htm
// How to use a variant array to write data to Excel in one go
// http://www.lmc-mediaagentur.de/dpool/tips/1485.htm
// Excel OLE Tips for Everyone
// http://www.undu.com/DN970501/00000021.htm
// create an Excel File without OLE?
// http://www.swissdelphicenter.ch/en/showcode.php?id=725
// control Excel with OLE?
// http://www.swissdelphicenter.ch/en/showcode.php?id=156
// Delphi and Microsoft Office: Automating Excel and Word - by Charles Calvert
// http://community.borland.com/article/0,1410,10126,00.html
// check for excel
{ var
ClassID: TCLSID;
strOLEObject: string;
begin
strOLEObject := 'Excel.Application';
if (CLSIDFromProgID(PWideChar(WideString(strOLEObject)), ClassID) = S_OK)
then
begin
// application is installed
end
else
begin
// application is not installed
end
end;
}
end.