-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCellActions.cs
466 lines (390 loc) · 19.4 KB
/
CellActions.cs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
using DevExpress.Spreadsheet;
using System;
using System.Drawing;
using System.IO;
namespace SpreadsheetControl_WPF_API
{
public static class CellActions
{
#region Actions
public static Action<IWorkbook> CreateSimpleAndComplexRangesAction = CreateSimpleAndComplexRanges;
public static Action<IWorkbook> ChangeCellValueAction = ChangeCellValue;
public static Action<IWorkbook> CellValueToFromObjectAction = CellValueToFromObject;
public static Action<IWorkbook> CellValueFromObjectViaCustomConverterAction = CellValueFromObjectViaCustomConverter;
public static Action<IWorkbook> AddHyperlinkAction = AddHyperlink;
public static Action<IWorkbook> AddCommentAction = AddComment;
public static Action<IWorkbook> CopyCellDataAndStyleAction = CopyCellDataAndStyle;
public static Action<IWorkbook> MergeAndSplitCellsAction = MergeAndSplitCells;
public static Action<IWorkbook> PlaceImageInCellAction = PlaceImageInCell;
public static Action<IWorkbook> ClearCellsAction = ClearCells;
#endregion
static void CreateSimpleAndComplexRanges(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets[0];
Color myColor1 = workbook.Styles["20% - Accent1"].Fill.BackgroundColor;
Color myColor2 = workbook.Styles["20% - Accent2"].Fill.BackgroundColor;
#region #SimpleRange
// A range that includes cells from the top left cell (A1) to the bottom right cell (B5).
CellRange rangeA1B5 = worksheet["A1:B5"];
// A rectangular range that includes cells from the top left cell (C5) to the bottom right cell (E7).
CellRange rangeC5E7 = worksheet["C5:E7"];
// The C4:E7 cell range located in the "Sheet3" worksheet.
CellRange rangeSheet3C4E7 = workbook.Range["Sheet3!C4:E7"];
// A range that contains a single cell (E7).
CellRange rangeE7 = worksheet["E7"];
// A range that includes the entire column A.
CellRange rangeColumnA = worksheet["A:A"];
// A range that includes the entire row 5.
CellRange rangeRow5 = worksheet["5:5"];
// A minimal rectangular range that includes all listed cells: C6, D9 and E7.
CellRange rangeC6D9E7 = worksheet.Range.Parse("C6:D9:E7");
// A rectangular range whose left column index is 0, top row index is 0,
// right column index is 3 and bottom row index is 2. This is the A1:D3 cell range.
CellRange rangeA1D3 = worksheet.Range.FromLTRB(0, 0, 3, 2);
// A range that includes the intersection of two ranges: C5:E10 and E9:G13.
// This is the E9:E10 cell range.
CellRange rangeE9E10 = worksheet["C5:E10 E9:G13"];
// Create a defined name for the D20:G23 cell range.
worksheet.DefinedNames.Add("MyNamedRange", "Sheet1!$D$20:$G$23");
// Access a range by its defined name.
CellRange rangeD20G23 = worksheet["MyNamedRange"];
#endregion #SimpleRange
#region #ComplexRange
CellRange rangeA1D4 = worksheet["A1:D4"];
CellRange rangeD5E7 = worksheet["D5:E7"];
CellRange rangeRow11 = worksheet["11:11"];
CellRange rangeF7 = worksheet["F7"];
// Create a complex range using the Range.Union method.
CellRange complexRange1 = worksheet["A7:A9"].Union(rangeD5E7);
// Create a complex range using the IRangeProvider.Union method.
CellRange complexRange2 = worksheet.Range.Union(new CellRange[] { rangeRow11, rangeA1D4, rangeF7 });
// Fill the ranges with different colors.
complexRange1.FillColor = myColor1;
complexRange2.FillColor = myColor2;
// Use the Areas property to get access to a component of a complex range.
complexRange2.Areas[2].FillColor = Color.Beige;
#endregion #ComplexRange
}
finally
{
workbook.EndUpdate();
}
}
static void ChangeCellValue(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].Value = "dateTime:";
worksheet.Cells["A2"].Value = "double:";
worksheet.Cells["A3"].Value = "string:";
worksheet.Cells["A4"].Value = "error constant:";
worksheet.Cells["A5"].Value = "boolean:";
worksheet.Cells["A6"].Value = "float:";
worksheet.Cells["A7"].Value = "char:";
worksheet.Cells["A8"].Value = "int32:";
worksheet.Cells["A10"].Value = "Fill a range of cells:";
worksheet.Columns["A"].WidthInCharacters = 20;
worksheet.Columns["B"].WidthInCharacters = 20;
worksheet.Range["A1:B8"].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Left;
#region #CellValue
// Add data of different types to cells.
worksheet.Cells["B1"].Value = DateTime.Now;
worksheet.Cells["B2"].Value = Math.PI;
worksheet.Cells["B3"].Value = "Have a nice day!";
worksheet.Cells["B4"].Value = CellValue.ErrorReference;
worksheet.Cells["B5"].Value = true;
worksheet.Cells["B6"].Value = float.MaxValue;
worksheet.Cells["B7"].Value = 'a';
worksheet.Cells["B8"].Value = Int32.MaxValue;
// Fill all cells in the range with 10.
worksheet.Range["B10:E10"].Value = 10;
#endregion #CellValue
}
finally
{
workbook.EndUpdate();
}
}
static void CellValueToFromObject(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets[0];
worksheet["A1"].Value = "Cell values converted to objects:";
worksheet["A5"].Value = "Cell values converted from objects:";
worksheet.Range["A1"].ColumnWidthInCharacters = 31;
worksheet.Range["B1:D5"].ColumnWidthInCharacters = 12;
#region #CellValueToFromObject
// Add data of different types to cells of the range.
CellRange sourceRange = worksheet["B1:B3"];
sourceRange[0].Value = "Text";
sourceRange[1].Formula = "=PI()";
sourceRange[2].Value = DateTime.Now;
sourceRange[2].NumberFormat = "d-mmm-yy";
// Get the number of cells in the range.
int cellCount = sourceRange.RowCount * sourceRange.ColumnCount;
// Declare an array to store elements of different types.
object[] array = new object[cellCount];
// Convert cell values to objects and add them to the array.
for (int i = 0; i < cellCount; i++)
{
array[i] = sourceRange[i].Value.ToObject();
}
// Convert array elements to cell values and assign them to cells in the fifth row.
for (int i = 0; i < array.Length; i++)
{
worksheet.Rows["5"][i + 1].SetValue(array[i]);
// An alternative way to do this is to use the CellValue.FromObject method.
// worksheet.Rows["5"][i+1].Value = CellValue.FromObject(array[i]);
}
#endregion #CellValueToFromObject
}
finally
{
workbook.EndUpdate();
}
}
static void CellValueFromObjectViaCustomConverter(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
#region #CustomCellValueConverter
Worksheet worksheet = workbook.Worksheets[0];
Cell cell = worksheet.Cells["A1"];
cell.FillColor = Color.Orange;
cell.Value = CellValue.FromObject(cell.FillColor, new ColorToNameConverter());
// ...
#endregion #CustomCellValueConverter
}
finally
{
workbook.EndUpdate();
}
}
#region #CustomCellValueConverter
class ColorToNameConverter : ICellValueConverter
{
object ICellValueConverter.ConvertToObject(CellValue value)
{
return null;
}
CellValue ICellValueConverter.TryConvertFromObject(object value)
{
bool isColor = value.GetType() == typeof(Color);
if (!isColor)
return null;
return ((Color)value).Name;
}
}
#endregion #CustomCellValueConverter
static void AddHyperlink(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A:C"].ColumnWidthInCharacters = 12;
#region #AddHyperlink
// Create a hyperlink to a web page.
Cell cell = worksheet.Cells["A1"];
worksheet.Hyperlinks.Add(cell, "http://www.devexpress.com/", true, "DevExpress");
// Create a hyperlink to a cell range in a workbook.
CellRange range = worksheet.Range["C3:D4"];
Hyperlink cellHyperlink = worksheet.Hyperlinks.Add(range, "Sheet2!B2:E7", false, "Select Range");
cellHyperlink.TooltipText = "Click Me";
#endregion #AddHyperlink
}
finally
{
workbook.EndUpdate();
}
}
static void CopyCellDataAndStyle(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
#region #CopyCell
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Columns["A"].WidthInCharacters = 32;
worksheet.Columns["B"].WidthInCharacters = 20;
Style style = workbook.Styles[BuiltInStyleId.Input];
// Specify the content and formatting for a source cell.
worksheet.Cells["A1"].Value = "Source Cell";
Cell sourceCell = worksheet.Cells["B1"];
sourceCell.Formula = "= PI()";
sourceCell.NumberFormat = "0.0000";
sourceCell.Style = style;
sourceCell.Font.Color = Color.Blue;
sourceCell.Font.Bold = true;
sourceCell.Borders.SetOutsideBorders(Color.Black, BorderLineStyle.Thin);
// Copy all information from the source cell to the "B3" cell.
worksheet.Cells["A3"].Value = "Copy All";
worksheet.Cells["B3"].CopyFrom(sourceCell);
// Copy only the source cell content (e.g., text, numbers, formula calculated values) to the "B4" cell.
worksheet.Cells["A4"].Value = "Copy Values";
worksheet.Cells["B4"].CopyFrom(sourceCell, PasteSpecial.Values);
// Copy the source cell content (e.g., text, numbers, formula calculated values)
// and number formats to the "B5" cell.
worksheet.Cells["A5"].Value = "Copy Values and Number Formats";
worksheet.Cells["B5"].CopyFrom(sourceCell, PasteSpecial.Values | PasteSpecial.NumberFormats);
// Copy only the formatting information from the source cell to the "B6" cell.
worksheet.Cells["A6"].Value = "Copy Formats";
worksheet.Cells["B6"].CopyFrom(sourceCell, PasteSpecial.Formats);
// Copy all information from the source cell to the "B7" cell except for border settings.
worksheet.Cells["A7"].Value = "Copy All Except Borders";
worksheet.Cells["B7"].CopyFrom(sourceCell, PasteSpecial.All & ~PasteSpecial.Borders);
// Copy information only about borders from the source cell to the "B8" cell.
worksheet.Cells["A8"].Value = "Copy Borders";
worksheet.Cells["B8"].CopyFrom(sourceCell, PasteSpecial.Borders);
#endregion #CopyCell
}
finally
{
workbook.EndUpdate();
}
}
static void MergeAndSplitCells(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A2"].FillColor = Color.LightGray;
worksheet.Cells["B2"].Value = "B2";
worksheet.Cells["B2"].FillColor = Color.LightGreen;
worksheet.Cells["C3"].Value = "C3";
worksheet.Cells["C3"].FillColor = Color.LightSalmon;
#region #MergeCells
// Merge cells contained in the range.
worksheet.MergeCells(worksheet.Range["A1:C5"]);
#endregion #MergeCells
}
finally
{
workbook.EndUpdate();
}
}
static void PlaceImageInCell(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
#region #PlaceImageInCell
Worksheet worksheet = workbook.Worksheets[0];
byte[] imageBytes = File.ReadAllBytes(@"Documents\x-spreadsheet.png");
MemoryStream imageStream = new MemoryStream(imageBytes);
worksheet.Cells["A2"].ColumnWidthInCharacters = 20;
// Insert cell images from a stream
worksheet.Cells["A2"].Value = imageStream;
// Specify image information
if (worksheet.Cells["A2"].Value.IsCellImage)
{
worksheet.Cells["A2"].ImageInfo.Decorative = true;
worksheet.Cells["A2"].ImageInfo.AlternativeText = "Image AltText";
}
#endregion PlaceImageInCell
}
finally
{
workbook.EndUpdate();
}
}
static void ClearCells(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A:D"].ColumnWidthInCharacters = 30;
worksheet.Range["B1:D6"].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
worksheet["B1"].Value = "Initial Cell Content and Formatting:";
worksheet.MergeCells(worksheet["C1:D1"]);
worksheet["C1:D1"].Value = "Cleared Cells:";
worksheet["A2"].Value = "Clear All:";
worksheet["A3"].Value = "Clear Cell Content Only:";
worksheet["A4"].Value = "Clear Cell Formatting Only:";
worksheet["A5"].Value = "Clear Cell Hyperlinks Only:";
worksheet["A6"].Value = "Clear Cell Comments Only:";
// Specify initial content and formatting for cells.
CellRange sourceCells = worksheet["B2:D6"];
sourceCells.Value = DateTime.Now;
sourceCells.Style = workbook.Styles[BuiltInStyleId.Accent3_40percent];
sourceCells.Font.Color = Color.LightSeaGreen;
sourceCells.Font.Bold = true;
sourceCells.Borders.SetAllBorders(Color.Blue, BorderLineStyle.Dashed);
worksheet.Hyperlinks.Add(worksheet["B5"], "http://www.devexpress.com/", true, "DevExpress");
worksheet.Hyperlinks.Add(worksheet["C5"], "http://www.devexpress.com/", true, "DevExpress");
worksheet.Hyperlinks.Add(worksheet["D5"], "http://www.devexpress.com/", true, "DevExpress");
worksheet.Comments.Add(worksheet["B6"], "Author", "Cell Comment");
worksheet.Comments.Add(worksheet["C6"], "Author", "Cell Comment");
worksheet.Comments.Add(worksheet["D6"], "Author", "Cell Comment");
#region #ClearCell
// Remove all cell information (content, formatting, hyperlinks and comments).
worksheet.Clear(worksheet["C2:D2"]);
// Remove cell content.
worksheet.ClearContents(worksheet["C3"]);
worksheet["D3"].Value = null;
// Remove cell formatting.
worksheet.ClearFormats(worksheet["C4"]);
worksheet["D4"].Style = workbook.Styles.DefaultStyle;
// Remove hyperlinks from cells.
worksheet.ClearHyperlinks(worksheet["C5"]);
Hyperlink hyperlinkD5 = worksheet.Hyperlinks.GetHyperlinks(worksheet["D5"])[0];
worksheet.Hyperlinks.Remove(hyperlinkD5);
// Remove comments from cells.
worksheet.ClearComments(worksheet["C6"]);
Comment commentD6 = worksheet.Comments.GetComments(worksheet["D6"])[0];
worksheet.Comments.Remove(commentD6);
#endregion #ClearCell
}
finally
{
workbook.EndUpdate();
}
}
static void AddComment(IWorkbook workbook)
{
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets[0];
// Specify initial content and formatting for cells.
worksheet.Cells["A2"].Value = "Original comment";
worksheet.Cells["E2"].Value = "Copied comment";
worksheet["A2"].Alignment.WrapText = true;
worksheet["E2"].Alignment.WrapText = true;
#region #AddComment
// Get the system username.
string author = workbook.CurrentAuthor;
// Add a comment to the "A2" cell.
Cell commentedCell = worksheet.Cells["A2"];
Comment commentA2 = worksheet.Comments.Add(commentedCell, author, "This is a comment");
commentA2.Visible = true;
// Insert the author's name at the beginning of the comment.
CommentRunCollection commentRunsA2 = commentA2.Runs;
commentRunsA2.Insert(0, author + ": \r\n");
// Copy the comment to the "E2" cell.
worksheet.Cells["E2"].CopyFrom(commentedCell, PasteSpecial.Comments);
// Get the added comment and make it visible.
Comment commentE2 = worksheet.Comments.GetComments(worksheet["E2"])[0];
commentE2.Visible = true;
// Modify text of the copied comment.
CommentRunCollection commentRunsE2 = commentE2.Runs;
commentRunsE2[1].Text = "This comment is copied from the cell " + commentedCell.GetReferenceA1();
#endregion #AddComment
}
finally
{
workbook.EndUpdate();
}
}
}
}