-
Notifications
You must be signed in to change notification settings - Fork 2
/
GeneralActions.cs
342 lines (285 loc) · 14.2 KB
/
GeneralActions.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
using System;
using System.Globalization;
using System.IO;
using DevExpress.Export.Xl;
using DevExpress.Office.Crypto;
namespace XLExportExamples
{
public static class GeneralActions {
#region Actions
public static Action<Stream, XlDocumentFormat> CreateDocumentAction = CreateDocument;
public static Action<Stream, XlDocumentFormat> EncryptDocumentAction = EncryptDocument;
public static Action<Stream, XlDocumentFormat> CreateSheetAction = CreateSheet;
public static Action<Stream, XlDocumentFormat> CreateHiddenSheetAction = CreateHiddenSheet;
public static Action<Stream, XlDocumentFormat> HideGridlinesAction = HideGridlines;
public static Action<Stream, XlDocumentFormat> HideHeadersAction = HideHeaders;
public static Action<Stream, XlDocumentFormat> CreateColumnsAction = CreateColumns;
public static Action<Stream, XlDocumentFormat> CreateRowsAction = CreateRows;
public static Action<Stream, XlDocumentFormat> CreateCellsAction = CreateCells;
public static Action<Stream, XlDocumentFormat> MergeCellsAction = MergeCells;
#endregion
static void CreateDocument(Stream stream, XlDocumentFormat documentFormat) {
#region #CreateDocument
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Create a new document and write it to the specified stream.
using (IXlDocument document = exporter.CreateDocument(stream)) {
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
}
#endregion #CreateDocument
}
static void EncryptDocument(Stream stream, XlDocumentFormat documentFormat)
{
#region #EncryptDocument
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Specify encryption options.
// A workbook will be encrypted using the default encryption mechanism
// (agile encryption for XLSX files, and RC4 encryption for XLS files).
EncryptionOptions encryptionOptions = new EncryptionOptions();
// Specify the encryption password.
encryptionOptions.Password = "password";
// Create a new document and encrypt its contents.
using (IXlDocument document = exporter.CreateDocument(stream, encryptionOptions))
{
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
}
#endregion #EncryptDocument
}
static void CreateSheet(Stream stream, XlDocumentFormat documentFormat) {
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
#region #CreateSheet
// Create a new document.
using (IXlDocument document = exporter.CreateDocument(stream)) {
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
// Create a new worksheet under the specified name.
using (IXlSheet sheet = document.CreateSheet()) {
sheet.Name = "Sales report";
}
}
#endregion #CreateSheet
}
static void CreateHiddenSheet(Stream stream, XlDocumentFormat documentFormat) {
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Create a new document.
using(IXlDocument document = exporter.CreateDocument(stream)) {
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
#region #CreateHiddenSheet
// Create the first worksheet.
using (IXlSheet sheet = document.CreateSheet()) {
sheet.Name = "Sales report";
}
// Create the second worksheet and specify its visibility.
using (IXlSheet sheet = document.CreateSheet()) {
sheet.Name = "Sales data";
sheet.VisibleState = XlSheetVisibleState.Hidden;
}
#endregion #CreateHiddenSheet
}
}
static void HideHeaders(Stream stream, XlDocumentFormat documentFormat)
{
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Create a new document.
using (IXlDocument document = exporter.CreateDocument(stream))
{
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
#region #HideHeaders
// Create a worksheet.
using (IXlSheet sheet = document.CreateSheet())
{
// Hide row and column headers in the worksheet.
sheet.ViewOptions.ShowRowColumnHeaders = false;
}
#endregion #HideHeaders
}
}
static void HideGridlines(Stream stream, XlDocumentFormat documentFormat)
{
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Create a new document.
using (IXlDocument document = exporter.CreateDocument(stream))
{
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
#region #HideGridlines
// Create a worksheet.
using (IXlSheet sheet = document.CreateSheet())
{
// Hide gridlines on the worksheet.
sheet.ViewOptions.ShowGridLines = false;
}
#endregion #HideGridlines
}
}
static void CreateColumns(Stream stream, XlDocumentFormat documentFormat) {
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Create a new document.
using(IXlDocument document = exporter.CreateDocument(stream)) {
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
#region #CreateColumns
// Create a worksheet.
using (IXlSheet sheet = document.CreateSheet()) {
// Create the column A and set its width to 100 pixels.
using (IXlColumn column = sheet.CreateColumn()) {
column.WidthInPixels = 100;
}
// Hide the column B in the worksheet.
using (IXlColumn column = sheet.CreateColumn()) {
column.IsHidden = true;
}
// Create the column D and set its width to 24.5 characters.
using (IXlColumn column = sheet.CreateColumn(3)) {
column.WidthInCharacters = 24.5f;
}
}
#endregion #CreateColumns
}
}
static void CreateRows(Stream stream, XlDocumentFormat documentFormat) {
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Create a new document.
using(IXlDocument document = exporter.CreateDocument(stream)) {
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
#region #CreateRows
// Create a worksheet.
using (IXlSheet sheet = document.CreateSheet()) {
// Create the first row and set its height to 40 pixels.
using (IXlRow row = sheet.CreateRow()) {
row.HeightInPixels = 40;
}
// Hide the third row in the worksheet.
using (IXlRow row = sheet.CreateRow(2)) {
row.IsHidden = true;
}
}
#endregion #CreateRows
}
}
static void CreateCells(Stream stream, XlDocumentFormat documentFormat)
{
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Create a new document.
using(IXlDocument document = exporter.CreateDocument(stream)) {
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
#region #CreateCells
// Create a worksheet.
using (IXlSheet sheet = document.CreateSheet()) {
// Create the column A and set its width.
using (IXlColumn column = sheet.CreateColumn()) {
column.WidthInPixels = 150;
}
// Create the first row.
using (IXlRow row = sheet.CreateRow()) {
// Create the cell A1 and set its value.
using (IXlCell cell = row.CreateCell()) {
cell.Value = "Numeric value:";
}
// Create the cell B1 and assign the numeric value to it.
using (IXlCell cell = row.CreateCell()) {
cell.Value = 123.45;
}
}
// Create the second row.
using (IXlRow row = sheet.CreateRow()) {
// Create the cell A2 and set its value.
using (IXlCell cell = row.CreateCell()) {
cell.Value = "Text value:";
}
// Create the cell B2 and assign the text value to it.
using (IXlCell cell = row.CreateCell()) {
cell.Value = "abc";
}
}
// Create the third row.
using (IXlRow row = sheet.CreateRow()) {
// Create the cell A3 and set its value.
using (IXlCell cell = row.CreateCell()) {
cell.Value = "Boolean value:";
}
// Create the cell B3 and assign the boolean value to it.
using (IXlCell cell = row.CreateCell()) {
cell.Value = true;
}
}
// Create the fourth row.
using (IXlRow row = sheet.CreateRow()) {
// Create the cell A4 and set its value.
using (IXlCell cell = row.CreateCell()) {
cell.Value = "Error value:";
}
// Create the cell B4 and assign an error value to it.
using (IXlCell cell = row.CreateCell()) {
cell.Value = XlVariantValue.ErrorName;
}
}
}
#endregion #CreateCells
}
}
static void MergeCells(Stream stream, XlDocumentFormat documentFormat)
{
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Create a new document.
using(IXlDocument document = exporter.CreateDocument(stream)) {
document.Options.Culture = CultureInfo.CurrentCulture;
// Create a worksheet.
using(IXlSheet sheet = document.CreateSheet()) {
// Create the first row in the worksheet.
using(IXlRow row = sheet.CreateRow()) {
// Create a cell.
using(IXlCell cell = row.CreateCell()) {
// Set the cell value.
cell.Value = "Merged cells A1 to E1";
// Align the cell content.
cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Center));
}
}
// Create the second row in the worksheet.
using(IXlRow row = sheet.CreateRow()) {
// Create a cell.
using(IXlCell cell = row.CreateCell()) {
// Set the cell value.
cell.Value = "Merged cells A2 to A5";
// Align the cell content.
cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Center));
// Wrap the text within the cell.
cell.Formatting.Alignment.WrapText = true;
}
// Create a cell.
using(IXlCell cell = row.CreateCell()) {
// Set the cell value.
cell.Value = "Merged cells B2 to E5";
// Align the cell content.
cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Center));
}
}
#region #MergeCells
// Merge cells contained in the range A1:E1.
sheet.MergedCells.Add(XlCellRange.FromLTRB(0, 0, 4, 0));
// Merge cells contained in the range A2:A5.
sheet.MergedCells.Add(XlCellRange.FromLTRB(0, 1, 0, 4));
// Merge cells contained in the range B2:E5.
sheet.MergedCells.Add(XlCellRange.FromLTRB(1, 1, 4, 4));
#endregion #MergeCells
}
}
}
}
}