-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTableActions.cs
200 lines (160 loc) · 8.01 KB
/
TableActions.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
using DevExpress.Spreadsheet;
using System;
using System.Drawing;
namespace SpreadsheetDocServerAPIPart2
{
public static class TableActions
{
public static Action<Workbook> CreateTableAction = CreateTable;
public static Action<Workbook> TableRangesAction = TableRanges;
public static Action<Workbook> FormatTableAction = FormatTable;
public static Action<Workbook> CustomTableStyleAction = CustomTableStyle;
public static Action<Workbook> DuplicateTableStyleAction = DuplicateTableStyle;
static void CreateTable(Workbook workbook)
{
#region #CreateTable
Worksheet worksheet = workbook.Worksheets[0];
// Insert a table in a worksheet.
Table table = worksheet.Tables.Add(worksheet["A1:F12"], false);
// Apply a built-in table style to the table.
table.Style = workbook.TableStyles[BuiltInTableStyleId.TableStyleMedium20];
#endregion #CreateTable
}
static void TableRanges(Workbook workbook)
{
#region #TableRanges
Worksheet worksheet = workbook.Worksheets["TableRanges"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Access a table.
Table table = worksheet.Tables[0];
// Obtain table columns.
TableColumn productColumn = table.Columns[0];
TableColumn priceColumn = table.Columns[1];
TableColumn quantityColumn = table.Columns[2];
TableColumn discountColumn = table.Columns[3];
// Add a new column to the end of the table .
TableColumn amountColumn = table.Columns.Add();
// Specify the column name.
amountColumn.Name = "Amount";
// Specify the formula to calculate the amount for each product
// and display the result in the "Amount" column.
amountColumn.Formula = "=[Price]*[Quantity]*(1-[Discount])";
// Display the total row for the table.
table.ShowTotals = true;
// Use the SUM function to calculate the total value for the "Amount" column.
discountColumn.TotalRowLabel = "Total:";
amountColumn.TotalRowFunction = TotalRowFunction.Sum;
// Specify the number format for each column.
priceColumn.DataRange.NumberFormat = "$#,##0.00";
discountColumn.DataRange.NumberFormat = "0.0%";
amountColumn.Range.NumberFormat = "$#,##0.00;$#,##0.00;\"\";@";
// Specify horizontal alignment for the header and total rows.
table.HeaderRowRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
table.TotalRowRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
// Specify horizontal alignment
// for all columns except the first column.
for (int i = 1; i < table.Columns.Count; i++)
{
table.Columns[i].DataRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
}
// Set the width of table columns.
table.Range.ColumnWidthInCharacters = 10;
worksheet.Visible = true;
#endregion #TableRanges
}
static void FormatTable(Workbook workbook)
{
#region #FormatTable
Worksheet worksheet = workbook.Worksheets["FormatTable"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Access a table.
Table table = worksheet.Tables[0];
// Access the workbook's collection of table styles.
TableStyleCollection tableStyles = workbook.TableStyles;
// Access the built-in table style by its name.
TableStyle tableStyle = tableStyles[BuiltInTableStyleId.TableStyleMedium16];
// Apply the style to the table.
table.Style = tableStyle;
// Show header and total rows.
table.ShowHeaders = true;
table.ShowTotals = true;
// Enable banded column formatting for the table.
table.ShowTableStyleRowStripes = false;
table.ShowTableStyleColumnStripes = true;
// Format the first column in the table.
table.ShowTableStyleFirstColumn = true;
worksheet.Visible = true;
#endregion #FormatTable
}
static void CustomTableStyle(Workbook workbook)
{
#region #CustomTableStyle
Worksheet worksheet = workbook.Worksheets["Custom Table Style"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Access a table.
Table table = worksheet.Tables[0];
String styleName = "testTableStyle";
// If a style with the specified name exists in the collection,
// apply this style to the table.
if (workbook.TableStyles.Contains(styleName))
{
table.Style = workbook.TableStyles[styleName];
}
else
{
// Add a new table style under the "testTableStyle" name
// to the table style collection.
TableStyle customTableStyle = workbook.TableStyles.Add("testTableStyle");
// Modify table style formatting.
// Specify format characteristics for different table elements.
customTableStyle.BeginUpdate();
try
{
customTableStyle.TableStyleElements[TableStyleElementType.WholeTable].Font.Color = Color.FromArgb(107, 107, 107);
// Format the header row.
TableStyleElement headerRowStyle = customTableStyle.TableStyleElements[TableStyleElementType.HeaderRow];
headerRowStyle.Fill.BackgroundColor = Color.FromArgb(64, 66, 166);
headerRowStyle.Font.Color = Color.White;
headerRowStyle.Font.Bold = true;
// Format the total row.
TableStyleElement totalRowStyle = customTableStyle.TableStyleElements[TableStyleElementType.TotalRow];
totalRowStyle.Fill.BackgroundColor = Color.FromArgb(115, 193, 211);
totalRowStyle.Font.Color = Color.White;
totalRowStyle.Font.Bold = true;
// Specify banded row formatting for the table.
TableStyleElement secondRowStripeStyle = customTableStyle.TableStyleElements[TableStyleElementType.SecondRowStripe];
secondRowStripeStyle.Fill.BackgroundColor = Color.FromArgb(234, 234, 234);
secondRowStripeStyle.StripeSize = 1;
}
finally
{
customTableStyle.EndUpdate();
}
// Apply the custom style to the table.
table.Style = customTableStyle;
}
worksheet.Visible = true;
#endregion #CustomTableStyle
}
static void DuplicateTableStyle(Workbook workbook)
{
#region #DuplicateTableStyle
Worksheet worksheet = workbook.Worksheets["Duplicate Table Style"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Access table.
Table table1 = worksheet.Tables[0];
Table table2 = worksheet.Tables[1];
// Obtain the built-in table style.
TableStyle sourceTableStyle = workbook.TableStyles[BuiltInTableStyleId.TableStyleMedium17];
// Duplicate the table style.
TableStyle newTableStyle = sourceTableStyle.Duplicate();
// Modify the duplicated table style's formatting.
newTableStyle.TableStyleElements[TableStyleElementType.HeaderRow].Fill.BackgroundColor = Color.FromArgb(0xA7, 0xEA, 0x52);
// Apply styles to tables.
table1.Style = sourceTableStyle;
table2.Style = newTableStyle;
worksheet.Visible = true;
#endregion #DuplicateTableStyle
}
}
}