-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
357 lines (309 loc) · 16.7 KB
/
Form1.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
using DevExpress.Export.Xl;
using DevExpress.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Printing;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
namespace XLExportExample {
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
List<EmployeeData> employees = EmployeesRepository.CreateEmployees();
List<string> departments = EmployeesRepository.CreateDepartments();
XlCellFormatting headerRowFormatting;
XlCellFormatting evenRowFormatting;
XlCellFormatting oddRowFormatting;
public Form1() {
InitializeComponent();
InitializeFormatting();
}
void InitializeFormatting() {
// Specify formatting settings for the even rows.
evenRowFormatting = new XlCellFormatting();
evenRowFormatting.Font = new XlFont();
evenRowFormatting.Font.Name = "Century Gothic";
evenRowFormatting.Font.SchemeStyle = XlFontSchemeStyles.None;
evenRowFormatting.Alignment = XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Center);
// Specify formatting settings for the odd rows.
oddRowFormatting = new XlCellFormatting();
oddRowFormatting.CopyFrom(evenRowFormatting);
oddRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light1, -0.15));
// Specify formatting settings for the header row.
headerRowFormatting = new XlCellFormatting();
headerRowFormatting.CopyFrom(evenRowFormatting);
headerRowFormatting.Font.Bold = true;
headerRowFormatting.Font.Color = XlColor.FromTheme(XlThemeColor.Light1, 0.0);
headerRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.0));
headerRowFormatting.Border = new XlBorder();
headerRowFormatting.Border.TopColor = XlColor.FromTheme(XlThemeColor.Dark1, 0.0);
headerRowFormatting.Border.TopLineStyle = XlBorderLineStyle.Medium;
headerRowFormatting.Border.BottomColor = XlColor.FromTheme(XlThemeColor.Dark1, 0.0);
headerRowFormatting.Border.BottomLineStyle = XlBorderLineStyle.Medium;
}
// Export the document to XLSX format.
void btnExportToXLSX_Click(object sender, EventArgs e) {
string fileName = GetSaveFileName("Excel Workbook files(*.xlsx)|*.xlsx", "Document.xlsx");
if (string.IsNullOrEmpty(fileName))
return;
if (ExportToFile(fileName, XlDocumentFormat.Xlsx))
ShowFile(fileName);
}
// Export the document to XLS format.
void btnExportToXLS_Click(object sender, EventArgs e) {
string fileName = GetSaveFileName("Excel 97-2003 Workbook files(*.xls)|*.xls", "Document.xls");
if (string.IsNullOrEmpty(fileName))
return;
if (ExportToFile(fileName, XlDocumentFormat.Xls))
ShowFile(fileName);
}
// Export the document to CSV format.
void btnExportToCSV_Click(object sender, EventArgs e) {
string fileName = GetSaveFileName("CSV (Comma delimited files)(*.csv)|*.csv", "Document.csv");
if (string.IsNullOrEmpty(fileName))
return;
if (ExportToFile(fileName, XlDocumentFormat.Csv))
ShowFile(fileName);
}
string GetSaveFileName(string filter, string defaultName) {
saveFileDialog1.Filter = filter;
saveFileDialog1.FileName = defaultName;
if (saveFileDialog1.ShowDialog() != DialogResult.OK)
return null;
return saveFileDialog1.FileName;
}
void ShowFile(string fileName) {
if (!File.Exists(fileName))
return;
DialogResult dResult = MessageBox.Show(String.Format("Do you want to open the resulting file?", fileName),
this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dResult == DialogResult.Yes)
Process.Start(fileName);
}
bool ExportToFile(string fileName, XlDocumentFormat documentFormat) {
try {
using (FileStream stream = new FileStream(fileName, FileMode.Create)) {
// Create an exporter with the specified formula parser.
IXlExporter exporter = XlExport.CreateExporter(documentFormat, new XlFormulaParser());
// Create a new document and begin to write it to the specified stream.
using (IXlDocument document = exporter.CreateDocument(stream)) {
// Generate the document content.
GenerateDocument(document);
}
}
return true;
}
catch (Exception ex) {
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
void GenerateDocument(IXlDocument document) {
// Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture;
// Add a new worksheet to the document.
using (IXlSheet sheet = document.CreateSheet()) {
// Specify the worksheet name.
sheet.Name = "Employees";
// Specify print settings for the worksheet.
SetupPageParameters(sheet);
// Generate worksheet columns.
GenerateColumns(sheet);
// Add the title to the documents exported to the XLSX and XLS formats.
if (document.Options.DocumentFormat != XlDocumentFormat.Csv)
GenerateTitle(sheet);
// Create the header row.
GenerateHeaderRow(sheet);
int firstDataRowIndex = sheet.CurrentRowIndex;
// Create the data rows.
for (int i = 0; i < employees.Count; i++)
GenerateDataRow(sheet, employees[i], (i + 1) == employees.Count);
// Specify the data range to be printed.
sheet.PrintArea = sheet.DataRange;
// Create data validation criteria for the documents exported to the XLSX and XLS formats.
if(document.Options.DocumentFormat != XlDocumentFormat.Csv)
GenerateDataValidations(sheet, firstDataRowIndex);
}
// Create the hidden worksheet containing source data for the data validation drop-down list.
if (document.Options.DocumentFormat != XlDocumentFormat.Csv) {
using (IXlSheet sheet = document.CreateSheet()) {
sheet.Name = "Departments";
sheet.VisibleState = XlSheetVisibleState.Hidden;
foreach (string department in departments) {
using (IXlRow row = sheet.CreateRow()) {
using (IXlCell cell = row.CreateCell())
cell.Value = department;
}
}
}
}
}
void GenerateColumns(IXlSheet sheet) {
// Create the "Employee ID" column and set its width.
using (IXlColumn column = sheet.CreateColumn())
column.WidthInPixels = 110;
// Create the "Employee Name" column and set its width.
using (IXlColumn column = sheet.CreateColumn())
column.WidthInPixels = 200;
XlNumberFormat numberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)";
// Create the "Salary" and "Bonus" columns and set the specific number format for their cells.
for (int i = 0; i < 2; i++) {
using (IXlColumn column = sheet.CreateColumn()) {
column.WidthInPixels = 100;
column.ApplyFormatting(numberFormat);
}
}
// Create the "Department" column and set its width.
using (IXlColumn column = sheet.CreateColumn())
column.WidthInPixels = 140;
}
void GenerateTitle(IXlSheet sheet) {
// Specify formatting settings for the document title.
XlCellFormatting formatting = new XlCellFormatting();
formatting.Font = new XlFont();
formatting.Font.Name = "Calibri Light";
formatting.Font.SchemeStyle = XlFontSchemeStyles.None;
formatting.Font.Size = 24;
formatting.Font.Color = XlColor.FromTheme(XlThemeColor.Dark1, 0.35);
formatting.Border = new XlBorder();
formatting.Border.BottomColor = XlColor.FromTheme(XlThemeColor.Dark1, 0.35);
formatting.Border.BottomLineStyle = XlBorderLineStyle.Medium;
// Add the document title.
using (IXlRow row = sheet.CreateRow()) {
using (IXlCell cell = row.CreateCell()) {
cell.Value = "LIST OF EMPLOYEES";
cell.Formatting = formatting;
}
// Create four empty cells with the title formatting.
row.BlankCells(4, formatting);
}
// Skip one row before starting to generate data rows.
sheet.SkipRows(1);
}
void GenerateHeaderRow(IXlSheet sheet) {
string[] columnNames = new string[] { "Employee ID", "Employee Name", "Salary", "Bonus", "Department" };
// Create the header row and set its height.
using (IXlRow row = sheet.CreateRow()) {
row.HeightInPixels = 28;
// Create required cells in the header row and apply specific formatting settings to them.
foreach(string columnName in columnNames) {
using (IXlCell cell = row.CreateCell()) {
cell.Value = columnName;
cell.ApplyFormatting(headerRowFormatting);
}
}
}
}
void GenerateDataRow(IXlSheet sheet, EmployeeData employee, bool isLastRow) {
// Create the data row to display the employee's information.
using (IXlRow row = sheet.CreateRow()) {
row.HeightInPixels = 28;
// Specify formatting settings to be applied to the data rows to shade alternate rows.
XlCellFormatting formatting = new XlCellFormatting();
formatting.CopyFrom((row.RowIndex % 2 == 0) ? evenRowFormatting : oddRowFormatting);
// Set the bottom border for the last data row.
if (isLastRow) {
formatting.Border = new XlBorder();
formatting.Border.BottomColor = XlColor.FromTheme(XlThemeColor.Dark1, 0.0);
formatting.Border.BottomLineStyle = XlBorderLineStyle.Medium;
}
// Create the cell containing the employee's ID.
using (IXlCell cell = row.CreateCell()) {
cell.Value = employee.Id;
cell.ApplyFormatting(formatting);
}
// Create the cell containing the employee's name.
using (IXlCell cell = row.CreateCell()) {
cell.Value = employee.Name;
cell.ApplyFormatting(formatting);
}
// Create the cell containing the employee's salary.
using (IXlCell cell = row.CreateCell()) {
cell.Value = employee.Salary;
cell.ApplyFormatting(formatting);
}
// Create the cell containing information about bonuses.
using (IXlCell cell = row.CreateCell()) {
cell.Value = employee.Bonus;
cell.ApplyFormatting(formatting);
}
// Create the cell containing the department name.
using (IXlCell cell = row.CreateCell()) {
cell.Value = employee.Department;
cell.ApplyFormatting(formatting);
}
}
}
void SetupPageParameters(IXlSheet sheet) {
// Specify the header and footer for the odd-numbered pages.
sheet.HeaderFooter.OddHeader = XlHeaderFooter.FromLCR("NorthWind Inc.", null, XlHeaderFooter.Date);
sheet.HeaderFooter.OddFooter = XlHeaderFooter.FromLCR("List of employees", null, XlHeaderFooter.PageNumber + " of " + XlHeaderFooter.PageTotal);
// Specify page margins.
sheet.PageMargins = new XlPageMargins();
sheet.PageMargins.PageUnits = XlPageUnits.Centimeters;
sheet.PageMargins.Left = 2.0;
sheet.PageMargins.Right = 1.0;
sheet.PageMargins.Top = 1.4;
sheet.PageMargins.Bottom = 1.4;
sheet.PageMargins.Header = 0.7;
sheet.PageMargins.Footer = 0.7;
// Specify page settings.
sheet.PageSetup = new XlPageSetup();
// Select the paper size.
sheet.PageSetup.PaperKind = DevExpress.Drawing.Printing.DXPaperKind.A4;
// Scale the print area to fit to one page wide.
sheet.PageSetup.FitToPage = true;
sheet.PageSetup.FitToWidth = 1;
sheet.PageSetup.FitToHeight = 0;
}
void GenerateDataValidations(IXlSheet sheet, int firstDataRowIndex) {
// Restrict data entry in the "Employee ID" column using criteria calculated by a worksheet formula (Employee ID must be a 5-digit number).
XlDataValidation validation = new XlDataValidation();
validation.Ranges.Add(XlCellRange.FromLTRB(0, firstDataRowIndex, 0, sheet.CurrentRowIndex - 1));
validation.Type = XlDataValidationType.Custom;
validation.Criteria1 = string.Format("=AND(ISNUMBER(A{0}),LEN(A{0})=5)", firstDataRowIndex + 1);
validation.InputPrompt = "Please enter a 5-digit number.";
validation.PromptTitle = "Employee ID";
sheet.DataValidations.Add(validation);
// Restrict data entry in the "Salary" column to a whole number from 600 to 2000.
validation = new XlDataValidation();
validation.Ranges.Add(XlCellRange.FromLTRB(2, firstDataRowIndex, 2, sheet.CurrentRowIndex - 1));
validation.Type = XlDataValidationType.Whole;
validation.Operator = XlDataValidationOperator.Between;
validation.Criteria1 = 600;
validation.Criteria2 = 2000;
// Specify the error message.
validation.ErrorMessage = "Salary must be greater than $600 and less than $2000.";
validation.ErrorTitle = "Warning";
validation.ErrorStyle = XlDataValidationErrorStyle.Warning;
// Specify the input message.
validation.InputPrompt = "Please enter a whole number in the range 600...2000.";
validation.PromptTitle = "Salary";
validation.ShowErrorMessage = true;
validation.ShowInputMessage = true;
sheet.DataValidations.Add(validation);
// Restrict data entry in the "Bonus" column to a decimal number within the specified limits (bonus cannot be greater than 10% of the salary.)
validation = new XlDataValidation();
validation.Ranges.Add(XlCellRange.FromLTRB(3, firstDataRowIndex, 3, sheet.CurrentRowIndex - 1));
validation.Type = XlDataValidationType.Whole;
validation.Operator = XlDataValidationOperator.Between;
validation.Criteria1 = 0;
validation.Criteria2 = string.Format("=C{0}*0.1", firstDataRowIndex + 1);
// Specify the error message.
validation.ErrorMessage = "Bonus cannot be greater than 10% of the salary.";
validation.ErrorTitle = "Information";
validation.ErrorStyle = XlDataValidationErrorStyle.Information;
validation.ShowErrorMessage = true;
sheet.DataValidations.Add(validation);
// Restrict data entry in the "Department" column to values in a drop-down list obtained from the cell range in the hidden "Departments" worksheet.
validation = new XlDataValidation();
validation.Ranges.Add(XlCellRange.FromLTRB(4, firstDataRowIndex, 4, sheet.CurrentRowIndex - 1));
validation.Type = XlDataValidationType.List;
XlCellRange sourceRange = XlCellRange.FromLTRB(0, 0, 0, departments.Count - 1).AsAbsolute();
sourceRange.SheetName = "Departments";
validation.Criteria1 = sourceRange;
sheet.DataValidations.Add(validation);
}
}
}