-
Notifications
You must be signed in to change notification settings - Fork 4
/
uExcelDemo.pas
204 lines (167 loc) · 5.26 KB
/
uExcelDemo.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
unit uExcelDemo;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, uExcelDocument, Vcl.ComCtrls,
Vcl.ExtCtrls;
type
TMainForm = class(TForm)
OpenFileBtn: TButton;
PageControl: TPageControl;
SaveFileBtn: TButton;
SaveDlg: TSaveDialog;
MainLabel: TLabel;
CreateNewBtn: TButton;
ExcelPanel: TPanel;
AddSheetBtn: TButton;
AddRowBtn: TButton;
AddColBtn: TButton;
OpenDlg: TOpenDialog;
OpenDefaultFileBtn: TButton;
RemSheetBtn: TButton;
procedure OpenFileBtnClick(Sender: TObject);
procedure SaveFileBtnClick(Sender: TObject);
procedure AddSheetBtnClick(Sender: TObject);
procedure AddRowBtnClick(Sender: TObject);
procedure AddColBtnClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure OpenDefaultFileBtnClick(Sender: TObject);
procedure CreateNewBtnClick(Sender: TObject);
procedure RemSheetBtnClick(Sender: TObject);
private
FExcelDocument: TExcelDocument;
procedure OnEnterGrid(Sender: TObject; ACol, ARow: Integer; const Value: string);
procedure AddSheet(const ASheet: TExcelSheet);
procedure RemSheet(const AIndex: Integer);
public
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.AddColBtnClick(Sender: TObject);
begin
if not Assigned(FExcelDocument) then
begin
ShowMessage('You didn''t create document.');
Exit
end;
FExcelDocument.Pages[PageControl.ActivePageIndex].ColCount :=
FExcelDocument.Pages[PageControl.ActivePageIndex].ColCount + 1;
TStringGrid(PageControl.ActivePage.Controls[0]).ColCount :=
FExcelDocument.Pages[PageControl.ActivePageIndex].ColCount;
end;
procedure TMainForm.AddRowBtnClick(Sender: TObject);
begin
if not Assigned(FExcelDocument) then
begin
ShowMessage('You didn''t create document.');
Exit
end;
FExcelDocument.Pages[PageControl.ActivePageIndex].RowCount :=
FExcelDocument.Pages[PageControl.ActivePageIndex].RowCount + 1;
TStringGrid(PageControl.ActivePage.Controls[0]).RowCount :=
FExcelDocument.Pages[PageControl.ActivePageIndex].RowCount;
end;
procedure TMainForm.AddSheet(const ASheet: TExcelSheet);
var
vGrid: TStringGrid;
vTabSheet: TTabSheet;
vX, vY: Integer;
begin
vTabSheet := TTabSheet.Create(PageControl);
vTabSheet.PageControl := PageControl;
vGrid := TStringGrid.Create(vTabSheet);
vGrid.Parent := vTabSheet;
vGrid.Align := TAlign.alClient;
vGrid.ColCount := ASheet.ColCount;
vGrid.RowCount := ASheet.RowCount;
vGrid.OnSetEditText := OnEnterGrid;
vGrid.EditorMode := True;
vGrid.DrawingStyle := TGridDrawingStyle.gdsGradient;
vGrid.Options := [TGridOption.goEditing, goVertLine, goHorzLine];
for vX := 0 to ASheet.ColCount - 1 do
for vY := 0 to ASheet.RowCount - 1 do
vGrid.Cells[vX, vY] := ASheet.Cells[vX, vY];
vTabSheet.Caption := ASheet.Name;
end;
procedure TMainForm.AddSheetBtnClick(Sender: TObject);
begin
if not Assigned(FExcelDocument) then
begin
ShowMessage('You didn''t create document.');
Exit
end;
AddSheet(FExcelDocument.AddPage);
end;
procedure TMainForm.CreateNewBtnClick(Sender: TObject);
var
vPage: TExcelSheet;
begin
FExcelDocument := TExcelDocument.CreateNew;
vPage := FExcelDocument.Pages[0];
vPage.ColCount := 3;
vPage.RowCount := 3;
vPage[1, 1] := 'Hello World!';
AddSheet(vPage);
end;
procedure TMainForm.OpenDefaultFileBtnClick(Sender: TObject);
var
i: Integer;
begin
FExcelDocument := TExcelDocument.CreateFromFile(GetCurrentDir + '\Test.xlsx');
for i := 0 to FExcelDocument.PageCount - 1 do
AddSheet(FExcelDocument.Pages[i]);
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
if Assigned(FExcelDocument) then
FExcelDocument.Free;
end;
procedure TMainForm.OnEnterGrid(Sender: TObject; ACol, ARow: Integer; const Value: string);
begin
FExcelDocument.Pages[PageControl.ActivePageIndex].Cells[ACol, ARow] := Value;
end;
procedure TMainForm.OpenFileBtnClick(Sender: TObject);
var
i: Integer;
begin
if OpenDlg.Execute then
begin
FExcelDocument := TExcelDocument.CreateFromFile(OpenDlg.FileName);
for i := 0 to FExcelDocument.PageCount - 1 do
AddSheet(FExcelDocument.Pages[i]);
end;
end;
procedure TMainForm.RemSheet(const AIndex: Integer);
var
vGrid: TStringGrid;
vTabSheet: TTabSheet;
begin
vTabSheet := PageControl.ActivePage;
vGrid := TStringGrid(PageControl.ActivePage.Controls[0]);
vGrid.Free;
PageControl.RemoveControl(PageControl.ActivePage);
vTabSheet.Free;
end;
procedure TMainForm.RemSheetBtnClick(Sender: TObject);
begin
if not Assigned(FExcelDocument) then
begin
ShowMessage('You didn''t create document.');
Exit
end;
FExcelDocument.RemPage(PageControl.ActivePageIndex);
RemSheet(PageControl.ActivePageIndex);
end;
procedure TMainForm.SaveFileBtnClick(Sender: TObject);
begin
if not Assigned(FExcelDocument) then
begin
ShowMessage('You didn''t create document.');
Exit
end;
if SaveDlg.Execute then
FExcelDocument.Save(SaveDlg.FileName);
end;
end.