-
Notifications
You must be signed in to change notification settings - Fork 2
/
MiscellaneousActions.cs
244 lines (224 loc) · 12.2 KB
/
MiscellaneousActions.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
using System;
using System.Globalization;
using System.IO;
using System.Text;
using DevExpress.Export.Xl;
using DevExpress.XtraExport.Csv;
namespace XLExportExamples
{
public static class MiscellaneousActions {
#region Actions
public static Action<Stream, XlDocumentFormat> HyperlinksAction = Hyperlinks;
public static Action<Stream, XlDocumentFormat> DocumentPropertiesAction = DocumentProperties;
public static Action<Stream, XlDocumentFormat> DocumentOptionsAction = DocumentOptions;
public static Action<Stream, XlDocumentFormat> CsvExportOptionsAction = CsvExportOptions;
#endregion
static void Hyperlinks(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;
#region #Hyperlinks
// Create a worksheet.
using (IXlSheet sheet = document.CreateSheet()) {
using(IXlColumn column = sheet.CreateColumn()) {
column.WidthInPixels = 300;
}
// Create a hyperlink to a cell in the current workbook.
using (IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "Local link";
cell.Formatting = XlCellFormatting.Hyperlink;
XlHyperlink hyperlink = new XlHyperlink();
hyperlink.Reference = new XlCellRange(new XlCellPosition(cell.ColumnIndex, cell.RowIndex));
hyperlink.TargetUri = "#Sheet1!C5";
sheet.Hyperlinks.Add(hyperlink);
}
}
// Create a hyperlink to a cell located in the external workbook.
using (IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "External file link";
cell.Formatting = XlCellFormatting.Hyperlink;
XlHyperlink hyperlink = new XlHyperlink();
hyperlink.Reference = new XlCellRange(new XlCellPosition(cell.ColumnIndex, cell.RowIndex));
hyperlink.TargetUri = "linked.xlsx#Sheet1!C5";
sheet.Hyperlinks.Add(hyperlink);
}
}
// Create a hyperlink to a web page.
using (IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "External URI";
cell.Formatting = XlCellFormatting.Hyperlink;
XlHyperlink hyperlink = new XlHyperlink();
hyperlink.Reference = new XlCellRange(new XlCellPosition(cell.ColumnIndex, cell.RowIndex));
hyperlink.TargetUri = "http://www.devexpress.com";
sheet.Hyperlinks.Add(hyperlink);
}
}
}
#endregion #Hyperlinks
}
}
static void DocumentProperties(Stream stream, XlDocumentFormat documentFormat) {
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
#region #DocumentProperties
// Create a new document.
using (IXlDocument document = exporter.CreateDocument(stream)) {
document.Options.Culture = CultureInfo.CurrentCulture;
// Set the built-in document properties.
document.Properties.Title = "XL Export API: document properties example";
document.Properties.Subject = "XL Export API";
document.Properties.Keywords = "XL Export, document generation";
document.Properties.Description = "How to set document properties using the XL Export API";
document.Properties.Category = "Spreadsheet";
document.Properties.Company = "DevExpress Inc.";
// Set the custom document properties.
document.Properties.Custom["Product Suite"] = "XL Export Library";
document.Properties.Custom["Revision"] = 5;
document.Properties.Custom["Date Completed"] = DateTime.Now;
document.Properties.Custom["Published"] = true;
// Generate data for the document.
using(IXlSheet sheet = document.CreateSheet()) {
sheet.SkipRows(1);
using(IXlRow row = sheet.CreateRow()) {
row.SkipCells(1);
using(IXlCell cell = row.CreateCell()) {
cell.Value = "You can view document properties using the File->Info->Properties->Advanced Properties dialog.";
}
}
}
}
#endregion #DocumentProperties
}
static void DocumentOptions(Stream stream, XlDocumentFormat documentFormat) {
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
#region #DocumentOptions
// Create a new document.
using (IXlDocument document = exporter.CreateDocument(stream)) {
document.Options.Culture = CultureInfo.CurrentCulture;
// Create a worksheet.
using(IXlSheet sheet = document.CreateSheet()) {
using(IXlColumn column = sheet.CreateColumn()) {
column.WidthInPixels = 200;
}
using(IXlColumn column = sheet.CreateColumn()) {
column.Formatting = XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Bottom);
}
// Display the file format to which the document is exported.
using(IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "Document format:";
}
using(IXlCell cell = row.CreateCell()) {
cell.Value = document.Options.DocumentFormat.ToString().ToUpper();
}
}
// Display the maximum number of columns allowed by the output file format.
using(IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "Maximum number of columns:";
}
using(IXlCell cell = row.CreateCell()) {
cell.Value = document.Options.MaxColumnCount;
}
}
// Display the maximum number of rows allowed by the output file format.
using (IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "Maximum number of rows:";
}
using(IXlCell cell = row.CreateCell()) {
cell.Value = document.Options.MaxRowCount;
}
}
// Display whether the document can contain multiple worksheets.
using(IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "Supports document parts:";
}
using(IXlCell cell = row.CreateCell()) {
cell.Value = document.Options.SupportsDocumentParts;
}
}
// Display whether the document can contain formulas.
using(IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "Supports formulas:";
}
using(IXlCell cell = row.CreateCell()) {
cell.Value = document.Options.SupportsFormulas;
}
}
// Display whether the document supports grouping functionality.
using(IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "Supports outline/grouping:";
}
using(IXlCell cell = row.CreateCell()) {
cell.Value = document.Options.SupportsOutlineGrouping;
}
}
}
}
#endregion #DocumentOptions
}
static void CsvExportOptions(Stream stream, XlDocumentFormat documentFormat) {
#region #CsvOptions
// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// Create a new document.
using(IXlDocument document = exporter.CreateDocument(stream)) {
document.Options.Culture = CultureInfo.CurrentCulture;
// Specify options for exporting the document to CSV format.
CsvDataAwareExporterOptions csvOptions = document.Options as CsvDataAwareExporterOptions;
if(csvOptions != null) {
// Set the encoding of the text-based file to which the workbook is exported.
csvOptions.Encoding = Encoding.UTF8;
// Write a preamble that specifies the encoding used.
csvOptions.WritePreamble = true;
// Convert a cell value to a string by using the current culture's format string.
csvOptions.UseCellNumberFormat = false;
// Insert the newline character after the last row of the resulting text.
csvOptions.NewlineAfterLastRow = true;
}
// Generate data for the document.
using(IXlSheet sheet = document.CreateSheet()) {
using(IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = "Product";
}
for(int i = 0; i < 4; i++) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = string.Format("Q{0}", i + 1);
}
}
}
string[] products = new string[] {
"Camembert Pierrot", "Gorgonzola Telino", "Mascarpone Fabioli", "Mozzarella di Giovanni",
"Gnocchi di nonna Alice", "Gudbrandsdalsost", "Gustaf's Knäckebröd", "Queso Cabrales",
"Queso Manchego La Pastora", "Raclette Courdavault", "Singaporean Hokkien Fried Mee", "Wimmers gute Semmelknödel"
};
Random random = new Random();
for(int i = 0; i < 12; i++) {
using(IXlRow row = sheet.CreateRow()) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = products[i];
}
for(int j = 0; j < 4; j++) {
using(IXlCell cell = row.CreateCell()) {
cell.Value = Math.Round(random.NextDouble() * 2000 + 3000);
}
}
}
}
}
}
#endregion #CsvOptions
}
}
}