1 |
1 |
unit UGettingStarted; |
2 |
2 |
|
3 |
3 |
interface |
4 |
4 |
|
5 |
5 |
uses |
6 |
6 |
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, |
7 |
7 |
Dialogs, StdCtrls, ShellAPI, UPaths, |
8 |
|
VCL_FlexCel_Core, FlexCel_XlsAdapter, FlexCelDynEnums; |
|
8 |
{$IFDEF FPC} LResources,{$ENDIF} |
|
9 |
{$if CompilerVersion >= 23.0} System.UITypes, {$IFEND} |
|
10 |
VCL.FlexCel.Core, FlexCel.XlsAdapter; |
9 |
11 |
|
10 |
12 |
type |
11 |
13 |
TFGettingStarted = class(TForm) |
12 |
14 |
btnCreateFile: TButton; |
13 |
15 |
Memo1: TMemo; |
14 |
16 |
SaveDialog: TSaveDialog; |
15 |
17 |
procedure btnCreateFileClick(Sender: TObject); |
16 |
18 |
private |
17 |
19 |
procedure CreateFile; |
18 |
20 |
procedure AddData(const Xls: TExcelFile); |
19 |
21 |
procedure ShowOpenDialog(const Xls: TExcelFile); |
20 |
22 |
end; |
21 |
23 |
|
22 |
24 |
var |
23 |
25 |
FGettingStarted: TFGettingStarted; |
24 |
26 |
|
25 |
27 |
implementation |
26 |
28 |
|
|
29 |
{$IFNDEF FPC} |
27 |
30 |
{$R *.dfm} |
|
31 |
{$ENDIF} |
28 |
32 |
|
29 |
33 |
procedure TFGettingStarted.CreateFile; |
30 |
34 |
var |
31 |
35 |
Xls: TExcelFile; |
32 |
36 |
begin |
33 |
37 |
Xls := TXlsFile.Create(true); |
34 |
38 |
try |
35 |
39 |
AddData(Xls); |
36 |
40 |
ShowOpenDialog(Xls); |
37 |
41 |
finally |
38 |
42 |
FreeAndNil(Xls); |
39 |
43 |
end; |
40 |
44 |
end; |
41 |
45 |
|
42 |
46 |
procedure TFGettingStarted.AddData(const Xls: TExcelFile); |
43 |
47 |
var |
44 |
48 |
PathToImage : string; |
45 |
49 |
fmt: TFlxFormat; |
46 |
50 |
XF, XF2: integer; |
47 |
51 |
begin |
48 |
52 |
|
49 |
53 |
Xls.NewFile(1); |
50 |
54 |
|
51 |
55 |
|
52 |
56 |
Xls.SetCellValue(1, 1, 'Hello to the world'); |
53 |
57 |
Xls.SetCellValue(2, 1, 3); |
54 |
58 |
Xls.SetCellValue(3, 1, 2.1); |
55 |
|
Xls.SetCellValue(4, 1, TFormula_Create('=Sum(A2, A3)')); //Note that formulas always are in English. This means use "," to separate arguments, not ";". |
|
59 |
Xls.SetCellValue(4, 1, TFormula.Create('=Sum(A2, A3)')); //Note that formulas always are in English. This means use "," to separate arguments, not ";". |
56 |
60 |
|
57 |
61 |
|
58 |
62 |
PathToImage := DataFolder + 'poweredbyflexcel.png'; |
59 |
63 |
|
60 |
64 |
|
61 |
65 |
Xls.AddImage(PathToImage, |
62 |
66 |
TImageProperties_Create( |
63 |
|
TClientAnchor_Create(TFlxAnchorType_MoveAndResize, 2, 0, 6, 0, 5, 0, 8, 0), |
|
67 |
TClientAnchor.Create(TFlxAnchorType.MoveAndResize, 2, 0, 6, 0, 5, 0, 8, 0), |
64 |
68 |
PathToImage, 'My image')); |
65 |
69 |
|
66 |
70 |
|
67 |
71 |
Xls.SetComment(2, 1, 'This is a comment'); |
68 |
72 |
|
69 |
73 |
|
70 |
74 |
fmt := Xls.GetDefaultFormat; |
71 |
75 |
fmt.Font.Name := 'Times New Roman'; |
72 |
|
fmt.Font.Color := TExcelColor_FromTheme(TThemeColor_Accent2); |
73 |
|
fmt.FillPattern.Pattern := TFlxPatternStyle_LightDown; |
74 |
|
fmt.FillPattern.FgColor := TExcelColor_FromColor(clBlue); |
75 |
|
fmt.FillPattern.BgColor := TExcelColor_FromColor(clWhite); |
|
76 |
fmt.Font.Color := clRed; |
|
77 |
fmt.FillPattern.Pattern := TFlxPatternStyle.LightDown; |
|
78 |
fmt.FillPattern.FgColor := clBlue; |
|
79 |
fmt.FillPattern.BgColor := clWhite; |
76 |
80 |
|
77 |
81 |
|
78 |
82 |
|
79 |
83 |
|
80 |
84 |
XF := Xls.AddFormat(fmt); |
81 |
85 |
|
82 |
86 |
Xls.SetCellFormat(2, 1, XF); |
83 |
87 |
Xls.SetCellFormat(3, 1, XF); |
84 |
88 |
|
85 |
89 |
fmt.Rotation := 45; |
86 |
90 |
fmt.Font.Size20 := 400; |
87 |
|
fmt.FillPattern.Pattern := TFlxPatternStyle_Solid; |
|
91 |
fmt.FillPattern.Pattern := TFlxPatternStyle.Solid; |
88 |
92 |
XF2 := Xls.AddFormat(fmt); |
89 |
93 |
|
90 |
94 |
|
91 |
95 |
Xls.SetRowFormat(1, XF2); |
92 |
96 |
|
93 |
97 |
|
94 |
98 |
Xls.MergeCells(5, 1, 10, 6); |
95 |
99 |
|
96 |
100 |
Xls.MergeCells(10, 6, 15, 6); |
97 |
101 |
|
98 |
102 |
|
99 |
103 |
|
100 |
104 |
Xls.PrintLandscape := true; |
101 |
105 |
end; |
102 |
106 |
|
103 |
107 |
procedure TFGettingStarted.btnCreateFileClick(Sender: TObject); |
104 |
108 |
begin |
105 |
109 |
CreateFile; |
106 |
110 |
end; |
107 |
111 |
|
108 |
112 |
|
109 |
113 |
procedure TFGettingStarted.ShowOpenDialog(const Xls: TExcelFile); |
110 |
114 |
begin |
111 |
115 |
if not SaveDialog.Execute then exit; |
112 |
116 |
Xls.Save(SaveDialog.FileName); |
113 |
117 |
|
114 |
118 |
if MessageDlg('Do you want to open the generated file?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then |
115 |
119 |
begin |
116 |
120 |
ShellExecute(0, 'open', PCHAR(SaveDialog.FileName), nil, nil, SW_SHOWNORMAL); |
117 |
121 |
end; |
118 |
122 |
end; |
119 |
123 |
|
|
124 |
{$IFDEF FPC} |
|
125 |
initialization |
|
126 |
{$I UGettingStarted.lrs} |
|
127 |
{$ENDIF} |
120 |
128 |
end. |