-
Notifications
You must be signed in to change notification settings - Fork 1
/
uExportExcel.pas
210 lines (177 loc) · 5.78 KB
/
uExportExcel.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
Unit uExportExcel;
Interface
uses Classes, Dialogs, Db, ComObj, Forms, Controls, ComCtrls, SysUtils,
SkinCtrls, SkinBoxCtrls, DynamicSkinForm, ABSDlgWait, System.Variants;
procedure ToExcel(dsData : TDataSource);
procedure ExportToExcel(dsData : TDataSource; ShowDialog : boolean; StatusBar : TspSkinStatusBar);
const
VAR_MAXROWS = 2147483646;
Implementation
uses
uFieldSelection;
procedure ExportToExcel(dsData : TDataSource; ShowDialog : boolean; StatusBar : TspSkinStatusBar);
var
Ok : boolean;
StopExportToExcel : boolean;
MaxRows, HlpCnt : integer;
fWait: TfrmWait;
const
xlWBATWorksheet = -4167;
procedure MakeSheet;
var
V : Variant;
Cnt, FieldCnt, SheetColCnt, SheetRowCnt : Integer;
Sheet : Variant;
HlpStr : string;
BakDecimalSeparator : Char;
begin
if not VarIsEmpty(V) then VarClear(V);
V := CreateOleObject( 'Excel.Application' );
BakDecimalSeparator := FormatSettings.DecimalSeparator;
FormatSettings.DecimalSeparator := '.';
try
V.Workbooks.Add( xlWBATWorksheet );
V.WorkBooks[1].Worksheets[1].Name := 'AbsDb';
Sheet := V.WorkBooks[1].Worksheets[ 'AbsDb' ];
FieldCnt := 0;
SheetColCnt := 1;
SheetRowCnt := 1;
for Cnt := 0 to dsData.DataSet.Fields.Count -1 do
begin
if (dsData.DataSet.Fields[Cnt].Visible) then
begin
if (frmFieldSelection.FieldList.Checked[FieldCnt]) then
with dsData.DataSet.Fields[Cnt] do
begin
Sheet.Cells[SheetRowCnt, SheetColCnt] := DisplayName;
Sheet.Cells[SheetRowCnt, SheetColCnt].Font.Bold := true;
if DisplayWidth <= 50 then
Sheet.Columns.Columns[ SheetColCnt ].ColumnWidth := DisplayWidth + 2
else
Sheet.Columns.Columns[ SheetColCnt ].ColumnWidth := 50;
Sheet.Columns.Columns[ SheetColCnt ].Font.Name := 'Arial';
Sheet.Columns.Columns[ SheetColCnt ].Font.Size := 10;
if (dsData.DataSet.Fields[Cnt].DataType in [ftFloat, ftDate, ftDateTime]) then
Sheet.Cells[SheetRowCnt, SheetColCnt].HorizontalAlignment := -4152;
Inc(SheetColCnt);
end;
Inc(FieldCnt);
end;
end;
with dsData.DataSet do
begin
dsData.DataSet.first;
while not(dsData.DataSet.eof) and (SheetRowCnt<=MaxRows) do
begin
FieldCnt := 0;
SheetColCnt := 1;
inc(SheetRowCnt);
for Cnt := 0 to FieldCount - 1 do
begin
if dsData.DataSet.Fields[Cnt].Visible then
begin
if (frmFieldSelection.FieldList.Checked[FieldCnt]) then
begin
if Fields[Cnt].DataType in [ ftDate, ftDateTime] then
begin
if not(Fields[Cnt].IsNull) then
Sheet.Cells[SheetRowCnt, SheetColCnt] := FormatDateTime( 'mm-dd-yyyy',Fields[Cnt].asDateTime )
else
Sheet.Cells[SheetRowCnt, SheetColCnt] := '';
end else
begin
HlpStr := Fields[Cnt].asString;
if Fields[Cnt].DataType in [ ftString, ftMemo ] then
HlpStr := ''''+Hlpstr;
if (dsData.DataSet.Fields[Cnt].DataType = ftFloat) then
HlpStr := StringReplace(HlpStr,',','.',[rfIgnoreCase, rfReplaceAll]);
Sheet.Cells[SheetRowCnt, SheetColCnt] := HlpStr;
end;
Inc(SheetColCnt);
end;
Inc(FieldCnt);
end;
end;
Next;
fWait.pb.Position := fWait.pb.Position + 1;
if (SheetRowCnt - 1) mod 100 = 0 then
begin
Application.ProcessMessages;
if fWait.Terminated then break;
if (StatusBar<>nil) then
begin
StatusBar.Caption := ' Records exported : ' + IntToStr( SheetRowCnt - 1 );
end;
end;
if StopExportToExcel then break;
end;
end;
if (StatusBar<>nil) then
begin
StatusBar.Caption := ' Successfully exported ' + IntToStr( SheetRowCnt - 1 )+' records to Excel';
end;
finally
FormatSettings.DecimalSeparator := BakDecimalSeparator;
V.Visible := true;
end;
end;
begin
if not (dsData.DataSet.Active) then dsData.DataSet.Open;
Ok := true;
Application.CreateForm(TfrmFieldSelection, frmFieldSelection);
frmFieldSelection.ExportLimitCB.ItemIndex := 0;
frmFieldSelection.FieldList.Items.Clear;
for HlpCnt := 0 to dsData.DataSet.Fields.Count - 1 do
begin
if dsData.DataSet.Fields[HlpCnt].Visible then
begin
frmFieldSelection.FieldList.Items.Add(dsData.DataSet.Fields[HlpCnt].DisplayName);
frmFieldSelection.FieldList.Checked[frmFieldSelection.FieldList.Items.Count-1] := true;
end;
end;
if ShowDialog then
begin
Ok := (frmFieldSelection.ShowModal = mrOK);
end;
MaxRows := VAR_MAXROWS;
if (trim(uppercase(frmFieldSelection.ExportLimitCB.Text))<>'ALL') then
begin
try
try
MaxRows := StrToInt(trim(uppercase(frmFieldSelection.ExportLimitCB.Text)));
except
ShowMessage('You specified an invalid number of records to export!');
Ok := False;
end;
finally
end;
end;
StopExportToExcel := false;
if not dsData.Dataset.Active then Exit;
if ok then
begin
try
Screen.Cursor := crHourGlass;
dsData.DataSet.DisableControls;
fWait:=TfrmWait.Create(nil);
fWait.btnBgMode.Visible := False;
fWait.pb.Max := dsData.DataSet.RecordCount;
fWait.Show('Export to Excel');
Application.ProcessMessages;
fWait.ShowFormOnTimer(nil);
MakeSheet;
finally
fWait.Close;
fWait.Free;
dsData.DataSet.EnableControls;
dsData.DataSet.First;
Screen.Cursor := crDefault;
end;
end;
frmFieldSelection.Free;
end;
procedure ToExcel(dsData : TDataSource);
begin
ExportToExcel(dsData, False, nil);
end;
end.