1+ using Syncfusion . XlsIO ;
2+ using Syncfusion . Drawing ;
3+ using System . IO ;
4+
5+ namespace Form_Controls
6+ {
7+ class Program
8+ {
9+ static void Main ( )
10+ {
11+ using ( ExcelEngine excelEngine = new ExcelEngine ( ) )
12+ {
13+ IApplication application = excelEngine . Excel ;
14+ application . DefaultVersion = ExcelVersion . Excel2016 ;
15+
16+ // Create workbook with 2 worksheets
17+ IWorkbook workbook = application . Workbooks . Create ( 2 ) ;
18+ IWorksheet sheet = workbook . Worksheets [ 0 ] ;
19+ IWorksheet sheet2 = workbook . Worksheets [ 1 ] ;
20+
21+ // Payment options for combo box
22+ string [ ] onlinePayments = { "Credit Card" , "Net Banking" } ;
23+ for ( int i = 0 ; i < onlinePayments . Length ; i ++ )
24+ sheet2 . SetValue ( i + 1 , 1 , onlinePayments [ i ] ) ;
25+
26+ // CONTACT SALES section
27+ sheet [ 2 , 3 ] . Text = "CONTACT SALES" ;
28+ sheet [ 2 , 3 ] . CellStyle . Font . Bold = true ;
29+ sheet [ 2 , 3 ] . CellStyle . Font . Size = 14 ;
30+ sheet [ 2 , 3 ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignCenter ;
31+
32+ sheet [ 4 , 3 ] . Text = "Phone" ;
33+ sheet [ 4 , 3 ] . CellStyle . Font . Bold = true ;
34+ sheet [ 5 , 3 ] . Text = "Toll Free" ;
35+ sheet [ 5 , 5 ] . Text = "1-888-9DOTNET" ;
36+ sheet [ 6 , 5 ] . Text = "1-888-936-8638" ;
37+ sheet [ 7 , 5 ] . Text = "1-919-481-1974" ;
38+
39+ sheet [ 8 , 3 ] . Text = "Fax" ;
40+ sheet [ 8 , 5 ] . Text = "1-919-573-0306" ;
41+
42+ sheet [ 9 , 3 ] . Text = "Email" ;
43+ sheet [ 10 , 3 ] . Text = "Sales" ;
44+
45+ IHyperLink link = sheet . HyperLinks . Add ( sheet [ 10 , 5 ] ) ;
46+ link . Type = ExcelHyperLinkType . Url ;
47+ link . Address = "mailto:sales@syncfusion.com" ;
48+ sheet [ 10 , 5 ] . Text = "sales@syncfusion.com" ;
49+ sheet [ 10 , 5 ] . CellStyle . Font . Color = ExcelKnownColors . Blue ;
50+ sheet [ 10 , 5 ] . CellStyle . Font . Underline = ExcelUnderline . Single ;
51+
52+ sheet [ 12 , 3 ] . Text = "Please fill out all required fields." ;
53+ sheet [ 12 , 3 ] . CellStyle . Font . Italic = true ;
54+ sheet [ 12 , 3 ] . CellStyle . Font . Color = ExcelKnownColors . Grey_80_percent ;
55+
56+ // Form controls
57+ sheet [ 14 , 5 ] . Text = "First Name*" ; sheet [ 14 , 5 ] . CellStyle . Font . Bold = true ;
58+ sheet [ 14 , 8 ] . Text = "Last Name*" ; sheet [ 14 , 8 ] . CellStyle . Font . Bold = true ;
59+
60+ ITextBoxShape textBoxShape = sheet . TextBoxes . AddTextBox ( 15 , 5 , 23 , 190 ) ;
61+ textBoxShape . Fill . FillType = ExcelFillType . SolidColor ;
62+ textBoxShape . Fill . ForeColor = Color . LightYellow ;
63+
64+ textBoxShape = sheet . TextBoxes . AddTextBox ( 15 , 8 , 23 , 195 ) ;
65+ textBoxShape . Fill . FillType = ExcelFillType . SolidColor ;
66+ textBoxShape . Fill . ForeColor = Color . LightYellow ;
67+
68+ sheet [ 17 , 3 ] . Text = "Company*" ;
69+ textBoxShape = sheet . TextBoxes . AddTextBox ( 17 , 5 , 23 , 385 ) ;
70+ textBoxShape . Fill . FillType = ExcelFillType . SolidColor ;
71+ textBoxShape . Fill . ForeColor = Color . LightYellow ;
72+
73+ sheet [ 19 , 3 ] . Text = "Phone*" ;
74+ textBoxShape = sheet . TextBoxes . AddTextBox ( 19 , 5 , 23 , 385 ) ;
75+ textBoxShape . Fill . FillType = ExcelFillType . SolidColor ;
76+ textBoxShape . Fill . ForeColor = Color . LightYellow ;
77+
78+ sheet [ 21 , 3 ] . Text = "Email*" ;
79+ textBoxShape = sheet . TextBoxes . AddTextBox ( 21 , 5 , 23 , 385 ) ;
80+ textBoxShape . Fill . FillType = ExcelFillType . SolidColor ;
81+ textBoxShape . Fill . ForeColor = Color . LightYellow ;
82+
83+ sheet [ 23 , 3 ] . Text = "Website" ;
84+ textBoxShape = sheet . TextBoxes . AddTextBox ( 23 , 5 , 23 , 385 ) ;
85+ textBoxShape . Fill . FillType = ExcelFillType . SolidColor ;
86+ textBoxShape . Fill . ForeColor = Color . LightYellow ;
87+
88+ // Multiple products
89+ sheet [ 25 , 3 ] . Text = "Multiple products?" ;
90+ ICheckBoxShape chkBoxProducts = sheet . CheckBoxes . AddCheckBox ( 25 , 5 , 20 , 75 ) ;
91+ chkBoxProducts . CheckState = ExcelCheckState . Mixed ;
92+
93+ // Product(s) section
94+ sheet [ 27 , 3 , 28 , 3 ] . Merge ( ) ;
95+ sheet [ 27 , 3 ] . Text = "Product(s)*" ;
96+ sheet [ 27 , 3 ] . MergeArea . CellStyle . VerticalAlignment = ExcelVAlign . VAlignCenter ;
97+
98+ ICheckBoxShape chkBoxProduct ;
99+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 27 , 5 , 20 , 75 ) ; chkBoxProduct . Text = "Studio" ;
100+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 27 , 6 , 20 , 75 ) ; chkBoxProduct . Text = "Calculate" ;
101+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 27 , 7 , 20 , 75 ) ; chkBoxProduct . Text = "Chart" ;
102+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 27 , 8 , 20 , 75 ) ; chkBoxProduct . Text = "Diagram" ;
103+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 27 , 9 , 20 , 75 ) ; chkBoxProduct . Text = "Edit" ;
104+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 27 , 10 , 20 , 75 ) ; chkBoxProduct . Text = "XlsIO" ;
105+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 28 , 5 , 20 , 75 ) ; chkBoxProduct . Text = "Grid" ;
106+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 28 , 6 , 20 , 75 ) ; chkBoxProduct . Text = "Grouping" ;
107+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 28 , 7 , 20 , 75 ) ; chkBoxProduct . Text = "HTMLUI" ;
108+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 28 , 8 , 20 , 75 ) ; chkBoxProduct . Text = "PDF" ;
109+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 28 , 9 , 20 , 75 ) ; chkBoxProduct . Text = "Tools" ;
110+ chkBoxProduct = sheet . CheckBoxes . AddCheckBox ( 28 , 10 , 20 , 75 ) ; chkBoxProduct . Text = "DocIO" ;
111+
112+ // Link checkboxes to hidden cells and formulas
113+ GenerateFormula ( excelEngine ) ;
114+
115+ // Selected products count
116+ sheet [ 30 , 3 ] . Text = "Selected Products Count" ;
117+ sheet [ 30 , 5 ] . Formula = "SUM(AA2:AA13)" ;
118+ sheet [ 30 , 5 ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignLeft ;
119+
120+ // Additional Information
121+ sheet [ 35 , 3 ] . Text = "Additional Information" ;
122+ sheet . TextBoxes . AddTextBox ( 32 , 5 , 150 , 385 ) ;
123+
124+ // Combo box
125+ sheet [ 43 , 3 ] . Text = "Online Payment" ;
126+ IComboBoxShape comboBox1 = sheet . ComboBoxes . AddComboBox ( 43 , 5 , 20 , 100 ) ;
127+ comboBox1 . ListFillRange = sheet2 [ "A1:A2" ] ;
128+ comboBox1 . SelectedIndex = 1 ;
129+
130+ // Option buttons
131+ sheet [ 46 , 3 ] . Text = "Card Type" ;
132+ IOptionButtonShape optionButton1 = sheet . OptionButtons . AddOptionButton ( 46 , 5 ) ;
133+ optionButton1 . Text = "American Express" ;
134+ optionButton1 . CheckState = ExcelCheckState . Checked ;
135+
136+ optionButton1 = sheet . OptionButtons . AddOptionButton ( 46 , 7 ) ;
137+ optionButton1 . Text = "Master Card" ;
138+
139+ optionButton1 = sheet . OptionButtons . AddOptionButton ( 46 , 9 ) ;
140+ optionButton1 . Text = "Visa" ;
141+
142+ // Styling
143+ sheet . Columns [ 0 ] . AutofitColumns ( ) ;
144+ sheet . Columns [ 3 ] . ColumnWidth = 12 ;
145+ sheet . Columns [ 4 ] . ColumnWidth = 10 ;
146+ sheet . Columns [ 5 ] . ColumnWidth = 10 ;
147+ sheet . IsGridLinesVisible = false ;
148+
149+ // Delete unused rows
150+ sheet . DeleteRow ( 40 ) ;
151+ sheet . DeleteRow ( 41 ) ;
152+ sheet . DeleteRow ( 42 ) ;
153+ sheet . DeleteRow ( 45 ) ;
154+
155+ // Save workbook
156+ workbook . SaveAs ( Path . GetFullPath ( "Output/FormControls.xlsx" ) ) ;
157+ }
158+ }
159+
160+ private static void GenerateFormula ( ExcelEngine excelEngine )
161+ {
162+ IWorksheet worksheet = excelEngine . Excel . Workbooks [ 0 ] . Worksheets [ 0 ] ;
163+ ICheckBoxes checkBoxes = worksheet . CheckBoxes ;
164+ string formula ;
165+
166+ for ( int i = 1 ; i < checkBoxes . Count ; i ++ )
167+ {
168+ IRange range = worksheet [ "Z" + ( i + 1 ) ] ;
169+ checkBoxes [ i ] . LinkedCell = range ;
170+ formula = "IF(" + range . AddressLocal + ",1,0)" ;
171+ worksheet [ "AA" + ( i + 1 ) ] . Formula = formula ;
172+ }
173+ }
174+ }
175+ }
0 commit comments