-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLMS.Helper.Excel.pas
76 lines (55 loc) · 1.29 KB
/
LMS.Helper.Excel.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
unit LMS.Helper.Excel;
interface
uses
Excel2010;
type
TExcelWorkSpace = class
private
LCID: Integer;
public
fXLA: TExcelApplication;
fXLWB: TExcelWorkbook;
fXLWS: TExcelWorkSheet;
constructor Create;
destructor Destroy; override;
procedure ShowAndFinally;
end;
implementation
uses
Winapi.Windows,
System.Variants;
{ TExcelWorkSpace }
constructor TExcelWorkSpace.Create;
begin
inherited;
fXLA := TExcelApplication.Create(nil);
fXLA.AutoConnect := True;
// Get "local user ID"
LCID := GetUserDefaultLCID;
// Hide Excel while working
fXLA.Visible[LCID] := False;
// no prompts
fXLA.DisplayAlerts[LCID] := False;
fXLWB := TExcelWorkbook.Create(nil);
// add new workbook and connect WB wrapper
fXLWB.ConnectTo(fXLA.Workbooks.Add(xlWBATWorksheet, LCID));
fXLWS := TExcelWorkSheet.Create(nil);
// connect to sheet 1
fXLWS.ConnectTo(fXLWB.Worksheets[1] as _Worksheet);
// select the first worksheet
(fXLWB.Worksheets[1] as _Worksheet).Select(EmptyParam, LCID);
end;
destructor TExcelWorkSpace.Destroy;
begin
fXLWS.free;
fXLWB.free;
fXLA.free;
inherited;
end;
procedure TExcelWorkSpace.ShowAndFinally;
begin
// failsafe always leave excel visible when we quit/disconnect.
fXLA.Visible[LCID] := True;
free;
end;
end.