1
+ //Create an instance of ExcelEngine
2
+ using Syncfusion . Drawing ;
3
+ using Syncfusion . XlsIO ;
4
+ using System . Globalization ;
5
+ using static System . Net . Mime . MediaTypeNames ;
6
+
7
+ namespace ExcelDocumentProperties
8
+ {
9
+
10
+ class Program
11
+ {
12
+ static void Main ( string [ ] args )
13
+ {
14
+ //Create an instance of ExcelEngine
15
+ using ( ExcelEngine excelEngine = new ExcelEngine ( ) )
16
+ {
17
+ IApplication application = excelEngine . Excel ;
18
+ application . DefaultVersion = ExcelVersion . Xlsx ;
19
+
20
+ //Create a workbook
21
+ IWorkbook workbook = application . Workbooks . Create ( 1 ) ;
22
+ IWorksheet worksheet = workbook . Worksheets [ 0 ] ;
23
+
24
+ //Generate Invoice
25
+ AddInvoiceDetails ( worksheet ) ;
26
+
27
+ //Apply built-in and custom document properties
28
+ ApplyDocumentProperties ( workbook ) ;
29
+
30
+ workbook . SaveAs ( Path . GetFullPath ( "DocumentProperties.xlsx" ) ) ;
31
+ }
32
+ }
33
+ /// <summary>
34
+ /// Apply built-in and custom document properties to the workbook
35
+ /// </summary>
36
+ /// <param name="workbook">IWorkbook</param>
37
+ public static void ApplyDocumentProperties ( IWorkbook workbook )
38
+ {
39
+ IWorksheet worksheet = workbook . Worksheets [ 0 ] ;
40
+
41
+ // Read key invoice details from the worksheet
42
+ int invoiceNumber = ( int ) worksheet . Range [ "D6" ] . Number ;
43
+ string invoiceDateText = worksheet . Range [ "E6" ] . DisplayText ;
44
+ int customerId = ( int ) worksheet . Range [ "D8" ] . Number ;
45
+ string terms = worksheet . Range [ "E8" ] . DisplayText ;
46
+ string customerName = worksheet . Range [ "A8" ] . DisplayText ;
47
+ string customerCompany = worksheet . Range [ "A9" ] . DisplayText ;
48
+ DateTime invoiceDate = DateTime . Now ;
49
+
50
+
51
+ // Add the document properties for the invoice
52
+ IBuiltInDocumentProperties builtInProperties = workbook . BuiltInDocumentProperties ;
53
+ builtInProperties . Title = $ "Invoice #{ invoiceNumber } ";
54
+ builtInProperties . Author = "Jim Halper" ;
55
+ builtInProperties . Subject = $ "Invoice for { customerName } ({ customerCompany } )";
56
+ builtInProperties . Keywords = $ "invoice;billing;customer:{ customerId } ;terms:{ terms } ";
57
+ builtInProperties . Company = "Great Lake Enterprises" ;
58
+ builtInProperties . Category = "Finance/Billing" ;
59
+ builtInProperties . Comments = $ "Issued { invoiceDate : yyyy-MM-dd} ";
60
+
61
+ // Add the custom document properties for the invoice
62
+ var customProperties = workbook . CustomDocumentProperties ;
63
+ customProperties [ "InvoiceNumber" ] . Value = invoiceNumber ;
64
+ customProperties [ "InvoiceDate" ] . Text = invoiceDate . ToString ( "yyyy-MM-dd" ) ;
65
+ customProperties [ "CustomerId" ] . Value = customerId ;
66
+ customProperties [ "CustomerName" ] . Text = customerName ;
67
+ customProperties [ "CustomerCompany" ] . Text = customerCompany ;
68
+ customProperties [ "Currency" ] . Text = "USD" ;
69
+ customProperties [ "PaymentStatus" ] . Text = "Completed" ;
70
+ customProperties [ "Confidential" ] . Value = true ;
71
+ }
72
+
73
+ /// <summary>
74
+ /// Populates the Invoice details to the worksheet
75
+ /// </summary>
76
+ /// <param name="worksheet">Worksheet</param>
77
+ public static void AddInvoiceDetails ( IWorksheet worksheet )
78
+ {
79
+ //Disable gridlines in the worksheet
80
+ worksheet . IsGridLinesVisible = false ;
81
+
82
+ //Enter text to the cell A1 and apply formatting.
83
+ worksheet . Range [ "A1:D1" ] . Merge ( ) ;
84
+ worksheet . Range [ "A1" ] . Text = "SALES INVOICE" ;
85
+ worksheet . Range [ "A1" ] . CellStyle . Font . Bold = true ;
86
+ worksheet . Range [ "A1" ] . CellStyle . Font . RGBColor = Color . FromArgb ( 42 , 118 , 189 ) ;
87
+ worksheet . Range [ "A1" ] . CellStyle . Font . Size = 35 ;
88
+
89
+ //Apply alignment in the cell D1
90
+ worksheet . Range [ "D1" ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignRight ;
91
+ worksheet . Range [ "D1" ] . CellStyle . VerticalAlignment = ExcelVAlign . VAlignTop ;
92
+
93
+ //Enter values to the cells from A2 to A5
94
+ worksheet . Range [ "A2" ] . Text = "Great Lake Bike Parts" ;
95
+ worksheet . Range [ "A3" ] . Text = "46036 Michigan Ave" ;
96
+ worksheet . Range [ "A4" ] . Text = "Canton, USA" ;
97
+ worksheet . Range [ "A5" ] . Text = "Phone: +1 231-231-2310" ;
98
+
99
+ //Make the text bold
100
+ worksheet . Range [ "A2:A5" ] . CellStyle . Font . Bold = true ;
101
+
102
+ //Merge cells
103
+ worksheet . Range [ "D1:E1" ] . Merge ( ) ;
104
+
105
+ //Enter values to the cells from D5 to E8
106
+ worksheet . Range [ "D5" ] . Text = "INVOICE#" ;
107
+ worksheet . Range [ "E5" ] . Text = "DATE" ;
108
+ worksheet . Range [ "D6" ] . Number = 1028 ;
109
+ worksheet . Range [ "E6" ] . Value = DateTime . Now . ToString ( "yyyy-MM-dd" ) ;
110
+ worksheet . Range [ "D7" ] . Text = "CUSTOMER ID" ;
111
+ worksheet . Range [ "E7" ] . Text = "Payment Status" ;
112
+ worksheet . Range [ "D8" ] . Number = 564 ;
113
+ worksheet . Range [ "E8" ] . Text = "Completed" ;
114
+
115
+ //Apply RGB backcolor to the cells from D5 to E8
116
+ worksheet . Range [ "D5:E5" ] . CellStyle . Color = Color . FromArgb ( 42 , 118 , 189 ) ;
117
+ worksheet . Range [ "D7:E7" ] . CellStyle . Color = Color . FromArgb ( 42 , 118 , 189 ) ;
118
+
119
+ //Apply known colors to the text in cells D5 to E8
120
+ worksheet . Range [ "D5:E5" ] . CellStyle . Font . Color = ExcelKnownColors . White ;
121
+ worksheet . Range [ "D7:E7" ] . CellStyle . Font . Color = ExcelKnownColors . White ;
122
+
123
+ //Make the text as bold from D5 to E8
124
+ worksheet . Range [ "D5:E8" ] . CellStyle . Font . Bold = true ;
125
+
126
+ //Apply alignment to the cells from D5 to E8
127
+ worksheet . Range [ "D5:E8" ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignCenter ;
128
+ worksheet . Range [ "D5:E5" ] . CellStyle . VerticalAlignment = ExcelVAlign . VAlignCenter ;
129
+ worksheet . Range [ "D7:E7" ] . CellStyle . VerticalAlignment = ExcelVAlign . VAlignCenter ;
130
+ worksheet . Range [ "D6:E6" ] . CellStyle . VerticalAlignment = ExcelVAlign . VAlignTop ;
131
+
132
+ //Enter value and applying formatting in the cell A7
133
+ worksheet . Range [ "A7" ] . Text = " BILL TO" ;
134
+ worksheet . Range [ "A7" ] . CellStyle . Color = Color . FromArgb ( 42 , 118 , 189 ) ;
135
+ worksheet . Range [ "A7" ] . CellStyle . Font . Bold = true ;
136
+ worksheet . Range [ "A7" ] . CellStyle . Font . Color = ExcelKnownColors . White ;
137
+
138
+ //Apply alignment
139
+ worksheet . Range [ "A7" ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignLeft ;
140
+ worksheet . Range [ "A7" ] . CellStyle . VerticalAlignment = ExcelVAlign . VAlignCenter ;
141
+
142
+ //Enter values in the cells A8 to A12
143
+ worksheet . Range [ "A8" ] . Text = "Steyn" ;
144
+ worksheet . Range [ "A9" ] . Text = "20 Whitehall Rd" ;
145
+ worksheet . Range [ "A10" ] . Text = "North Muskegon,USA" ;
146
+ worksheet . Range [ "A11" ] . Text = "+1 231-654-0000" ;
147
+
148
+ //Create a Hyperlink for e-mail in the cell A13
149
+ IHyperLink hyperlink = worksheet . HyperLinks . Add ( worksheet . Range [ "A12" ] ) ;
150
+ hyperlink . Type = ExcelHyperLinkType . Url ;
151
+ hyperlink . Address = "[email protected] " ;
152
+ hyperlink . ScreenTip = "Send Mail" ;
153
+
154
+ //Merge column A and B from row 15 to 22
155
+ worksheet . Range [ "A15:B15" ] . Merge ( ) ;
156
+ worksheet . Range [ "A16:B16" ] . Merge ( ) ;
157
+ worksheet . Range [ "A17:B17" ] . Merge ( ) ;
158
+ worksheet . Range [ "A18:B18" ] . Merge ( ) ;
159
+ worksheet . Range [ "A19:B19" ] . Merge ( ) ;
160
+ worksheet . Range [ "A20:B20" ] . Merge ( ) ;
161
+ worksheet . Range [ "A21:B21" ] . Merge ( ) ;
162
+ worksheet . Range [ "A22:B22" ] . Merge ( ) ;
163
+
164
+ // Headers
165
+ worksheet . Range [ "A15" ] . Text = " Items" ;
166
+ worksheet . Range [ "C15" ] . Text = "QTY" ;
167
+ worksheet . Range [ "D15" ] . Text = "UNIT PRICE" ;
168
+ worksheet . Range [ "E15" ] . Text = "AMOUNT" ;
169
+
170
+ // Bike spare parts
171
+ worksheet . Range [ "A16" ] . Text = "Brake Pads" ;
172
+ worksheet . Range [ "A17" ] . Text = "Chain" ;
173
+ worksheet . Range [ "A18" ] . Text = "Gear Cable Set" ;
174
+ worksheet . Range [ "A19" ] . Text = "Pedals" ;
175
+ worksheet . Range [ "A20" ] . Text = "Tyre (700x25C)" ;
176
+
177
+ // Quantities
178
+ worksheet . Range [ "C16" ] . Number = 2 ;
179
+ worksheet . Range [ "C17" ] . Number = 1 ;
180
+ worksheet . Range [ "C18" ] . Number = 2 ;
181
+ worksheet . Range [ "C19" ] . Number = 1 ;
182
+ worksheet . Range [ "C20" ] . Number = 2 ;
183
+
184
+ // Unit Prices (in USD)
185
+ worksheet . Range [ "D16" ] . Number = 15 ;
186
+ worksheet . Range [ "D17" ] . Number = 35 ;
187
+ worksheet . Range [ "D18" ] . Number = 12 ;
188
+ worksheet . Range [ "D19" ] . Number = 45 ;
189
+ worksheet . Range [ "D20" ] . Number = 30 ;
190
+
191
+ worksheet . Range [ "D23" ] . Text = "Total" ;
192
+
193
+ //Apply number format
194
+ worksheet . Range [ "D16:E22" ] . NumberFormat = "$0.00" ;
195
+ worksheet . Range [ "E23" ] . NumberFormat = "$0.00" ;
196
+
197
+ //Apply incremental formula for column Amount by multiplying Qty and UnitPrice
198
+ worksheet . Application . EnableIncrementalFormula = true ;
199
+ worksheet . Range [ "E16:E20" ] . Formula = "=C16*D16" ;
200
+
201
+ //Formula for Sum the total
202
+ worksheet . Range [ "E23" ] . Formula = "=SUM(E16:E22)" ;
203
+
204
+ //Apply borders
205
+ worksheet . Range [ "A16:E22" ] . CellStyle . Borders [ ExcelBordersIndex . EdgeTop ] . LineStyle = ExcelLineStyle . Thin ;
206
+ worksheet . Range [ "A16:E22" ] . CellStyle . Borders [ ExcelBordersIndex . EdgeBottom ] . LineStyle = ExcelLineStyle . Thin ;
207
+ worksheet . Range [ "A16:E22" ] . CellStyle . Borders [ ExcelBordersIndex . EdgeTop ] . Color = ExcelKnownColors . Grey_25_percent ;
208
+ worksheet . Range [ "A16:E22" ] . CellStyle . Borders [ ExcelBordersIndex . EdgeBottom ] . Color = ExcelKnownColors . Grey_25_percent ;
209
+ worksheet . Range [ "A23:E23" ] . CellStyle . Borders [ ExcelBordersIndex . EdgeTop ] . LineStyle = ExcelLineStyle . Thin ;
210
+ worksheet . Range [ "A23:E23" ] . CellStyle . Borders [ ExcelBordersIndex . EdgeBottom ] . LineStyle = ExcelLineStyle . Thin ;
211
+ worksheet . Range [ "A23:E23" ] . CellStyle . Borders [ ExcelBordersIndex . EdgeTop ] . Color = ExcelKnownColors . Black ;
212
+ worksheet . Range [ "A23:E23" ] . CellStyle . Borders [ ExcelBordersIndex . EdgeBottom ] . Color = ExcelKnownColors . Black ;
213
+
214
+ //Apply font setting for cells with product details
215
+ worksheet . Range [ "A3:E23" ] . CellStyle . Font . FontName = "Arial" ;
216
+ worksheet . Range [ "A3:E23" ] . CellStyle . Font . Size = 10 ;
217
+ worksheet . Range [ "A15:E15" ] . CellStyle . Font . Color = ExcelKnownColors . White ;
218
+ worksheet . Range [ "A15:E15" ] . CellStyle . Font . Bold = true ;
219
+ worksheet . Range [ "D23:E23" ] . CellStyle . Font . Bold = true ;
220
+
221
+ //Apply cell color
222
+ worksheet . Range [ "A15:E15" ] . CellStyle . Color = Color . FromArgb ( 42 , 118 , 189 ) ;
223
+
224
+ //Apply alignment to cells with product details
225
+ worksheet . Range [ "A15" ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignLeft ;
226
+ worksheet . Range [ "C15:C22" ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignCenter ;
227
+ worksheet . Range [ "D15:E15" ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignCenter ;
228
+
229
+ //Apply row height and column width to look good
230
+ worksheet . Range [ "A1" ] . ColumnWidth = 36 ;
231
+ worksheet . Range [ "B1" ] . ColumnWidth = 11 ;
232
+ worksheet . Range [ "C1" ] . ColumnWidth = 8 ;
233
+ worksheet . Range [ "D1:E1" ] . ColumnWidth = 18 ;
234
+ worksheet . Range [ "A1" ] . RowHeight = 47 ;
235
+ worksheet . Range [ "A2" ] . RowHeight = 15 ;
236
+ worksheet . Range [ "A3:A4" ] . RowHeight = 15 ;
237
+ worksheet . Range [ "A5" ] . RowHeight = 18 ;
238
+ worksheet . Range [ "A6" ] . RowHeight = 29 ;
239
+ worksheet . Range [ "A7" ] . RowHeight = 18 ;
240
+ worksheet . Range [ "A8" ] . RowHeight = 15 ;
241
+ worksheet . Range [ "A9:A14" ] . RowHeight = 15 ;
242
+ worksheet . Range [ "A15:A23" ] . RowHeight = 18 ;
243
+ }
244
+ }
245
+ }
0 commit comments