-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
400 lines (337 loc) · 18.4 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
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
using DevExpress.Export.Xl;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace XLExportExampleSalesReport {
public partial class Form1 : Form {
// Obtain the data source.
List<SalesData> sales = SalesDataRepository.CreateSalesData();
XlCellFormatting headerRowFormatting;
XlCellFormatting dataRowFormatting;
XlCellFormatting totalRowFormatting;
XlCellFormatting grandTotalRowFormatting;
public Form1() {
InitializeComponent();
InitializeFormatting();
}
void InitializeFormatting() {
// Specify formatting settings for the header rows.
headerRowFormatting = new XlCellFormatting();
headerRowFormatting.Font = XlFont.BodyFont();
headerRowFormatting.Font.Bold = true;
headerRowFormatting.Font.Color = XlColor.FromTheme(XlThemeColor.Light1, 0.0);
headerRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent1, 0.0));
headerRowFormatting.Alignment = XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Bottom);
// Specify formatting settings for the data rows.
dataRowFormatting = new XlCellFormatting();
dataRowFormatting.Font = XlFont.BodyFont();
dataRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light1, 0.0));
// Specify formatting settings for the total rows.
totalRowFormatting = new XlCellFormatting();
totalRowFormatting.Font = XlFont.BodyFont();
totalRowFormatting.Font.Bold = true;
totalRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light2, 0.0));
// Specify formatting settings for the grand total row.
grandTotalRowFormatting = new XlCellFormatting();
grandTotalRowFormatting.Font = XlFont.BodyFont();
grandTotalRowFormatting.Font.Bold = true;
grandTotalRowFormatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light2, -0.2));
}
// 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 defaulName) {
saveFileDialog1.Filter = filter;
saveFileDialog1.FileName = defaulName;
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 instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);
// 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 = "Sales Report";
// Specify print settings for the worksheet.
SetupPageParameters(sheet);
// Specify the summary row and summary column location for the grouped data.
sheet.OutlineProperties.SummaryBelow = true;
sheet.OutlineProperties.SummaryRight = true;
// Generate worksheet columns.
GenerateColumns(sheet);
// Add the document title.
GenerateTitle(sheet);
// Begin to group worksheet rows (create the outer group of rows).
sheet.BeginGroup(false);
// Create the query expression to retrieve data from the sales list and group data by the State.
// Query variable is an IEnumerable<IGrouping<string, SalesData>>.
var statesQuery = from data in sales
group data by data.State into dataGroup
orderby dataGroup.Key
select dataGroup.Key;
// Create data rows to display sales for each state.
foreach (string state in statesQuery)
GenerateData(sheet, state);
// Finalize the group creation.
sheet.EndGroup();
// Create the grand total row.
GenerateGrandTotalRow(sheet);
// Specify the data range to be printed.
sheet.PrintArea = sheet.DataRange;
}
}
void GenerateColumns(IXlSheet sheet) {
// Create the column "A" and set its width.
using (IXlColumn column = sheet.CreateColumn())
column.WidthInPixels = 18;
// Create the column "B" and set its width.
using (IXlColumn column = sheet.CreateColumn())
column.WidthInPixels = 166;
XlNumberFormat numberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)";
// Begin to group worksheet columns starting from the column "C" to the column "F".
sheet.BeginGroup(false);
// Create four successive columns ("C", "D", "E" and "F") and set the specific number format for their cells.
for (int i = 0; i < 4; i++) {
using (IXlColumn column = sheet.CreateColumn()) {
column.WidthInPixels = 117;
column.ApplyFormatting(numberFormat);
}
}
// Finalize the group creation.
sheet.EndGroup();
// Create the summary column "G", adjust its width and set the specific number format for its cells.
using (IXlColumn column = sheet.CreateColumn()) {
column.WidthInPixels = 117;
column.ApplyFormatting(numberFormat);
}
}
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.5);
formatting.Border = new XlBorder();
formatting.Border.BottomColor = XlColor.FromTheme(XlThemeColor.Dark1, 0.5);
formatting.Border.BottomLineStyle = XlBorderLineStyle.Medium;
// Add the document title.
using (IXlRow row = sheet.CreateRow()) {
// Skip the cell "A1".
row.SkipCells(1);
// Create the cell "B1" containing the document title.
using (IXlCell cell = row.CreateCell()) {
cell.Value = "SALES ANALYSIS 2014";
cell.Formatting = formatting;
}
// Create five empty cells with the title formatting.
row.BlankCells(5, formatting);
}
// Skip one row before starting to generate data rows.
sheet.SkipRows(1);
// Insert a picture from a file and anchor it to the cell "G1".
string startupPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
using (IXlPicture picture = sheet.CreatePicture()) {
picture.Image = Image.FromFile(Path.Combine(startupPath, "Logo.png"));
picture.SetOneCellAnchor(new XlAnchorPoint(6, 0, 8, 4), 105, 30);
}
}
void GenerateData(IXlSheet sheet, string nameOfState) {
// Create the header row for the state sales.
GenerateHeaderRow(sheet, nameOfState);
int firstDataRowIndex = sheet.CurrentRowIndex;
// Begin to group worksheet rows (create the inner group of rows containing sales data for the specific state).
sheet.BeginGroup(false);
// Create the query expression to retrieve sales data for the specified State. Then, sort data by the Product key in ascending order.
var salesQuery = from data in sales
where data.State == nameOfState
orderby data.Product
select data;
// Create the data row to display sales information for each product.
foreach (SalesData data in salesQuery)
GenerateDataRow(sheet, data);
// Finalize the group creation.
sheet.EndGroup();
// Create the summary row for the group.
GenerateTotalRow(sheet, firstDataRowIndex);
}
void GenerateHeaderRow(IXlSheet sheet, string nameOfState) {
// Create the header row for sales data in the specific state.
using (IXlRow row = sheet.CreateRow()) {
// Skip the first cell in the row.
row.SkipCells(1);
// Create the cell that displays the state name and specify its format settings.
using (IXlCell cell = row.CreateCell()) {
cell.Value = nameOfState;
cell.ApplyFormatting(headerRowFormatting);
cell.ApplyFormatting(XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.0)));
cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.General, XlVerticalAlignment.Bottom));
}
// Create four successive cells with values "Q1", "Q2", "Q3" and "Q4".
// Apply specific formatting settings to the created cells.
for (int i = 0; i < 4; i++) {
using (IXlCell cell = row.CreateCell()) {
cell.Value = string.Format("Q{0}", i + 1);
cell.ApplyFormatting(headerRowFormatting);
}
}
// Create the "Yearly total" cell and specify its format settings.
using (IXlCell cell = row.CreateCell()) {
cell.Value = "Yearly total";
cell.ApplyFormatting(headerRowFormatting);
}
}
}
void GenerateDataRow(IXlSheet sheet, SalesData data) {
// Create the row to display sales information for each sale item.
using (IXlRow row = sheet.CreateRow()) {
// Skip the first row in the cell.
row.SkipCells(1);
// Create the cell to display the product name and specify its format settings.
using (IXlCell cell = row.CreateCell()) {
cell.Value = data.Product;
cell.ApplyFormatting(dataRowFormatting);
cell.Formatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.8));
}
// Create the cell to display sales amount in the first quarter and specify its format settings.
using (IXlCell cell = row.CreateCell()) {
cell.Value = data.Q1;
cell.ApplyFormatting(dataRowFormatting);
}
// Create the cell to display sales amount in the second quarter and specify its format settings.
using (IXlCell cell = row.CreateCell()) {
cell.Value = data.Q2;
cell.ApplyFormatting(dataRowFormatting);
}
// Create the cell to display sales amount in the third quarter and specify its format settings.
using (IXlCell cell = row.CreateCell()) {
cell.Value = data.Q3;
cell.ApplyFormatting(dataRowFormatting);
}
// Create the cell to display sales amount in the fourth quarter and specify its format settings.
using (IXlCell cell = row.CreateCell()) {
cell.Value = data.Q4;
cell.ApplyFormatting(dataRowFormatting);
}
// Create the cell to display annual sales for the product. Use the SUM function to add product sales in each quarter.
using (IXlCell cell = row.CreateCell()) {
cell.SetFormula(XlFunc.Sum(XlCellRange.FromLTRB(2, row.RowIndex, 5, row.RowIndex)));
cell.ApplyFormatting(dataRowFormatting);
cell.ApplyFormatting(XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light2, 0.0)));
}
}
}
void GenerateTotalRow(IXlSheet sheet, int firstDataRowIndex) {
// Create the total row for each inner group of sales in the specific state.
using (IXlRow row = sheet.CreateRow()) {
// Skip the first cell in the row.
row.SkipCells(1);
// Create the "Total" cell and specify its format settings.
using (IXlCell cell = row.CreateCell()) {
cell.Value = "Total";
cell.ApplyFormatting(totalRowFormatting);
cell.Formatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.6));
}
// Create four successive cells displaying total sales for each quarter individually. Use the SUBTOTAL function to add quarterly sales.
for (int j = 0; j < 4; j++) {
using (IXlCell cell = row.CreateCell()) {
cell.SetFormula(XlFunc.Subtotal(XlCellRange.FromLTRB(j + 2, firstDataRowIndex, j + 2, row.RowIndex - 1), XlSummary.Sum, false));
cell.ApplyFormatting(totalRowFormatting);
}
}
// Create the cell that displays yearly sales for the state. Use the SUBTOTAL function to add yearly sales in the current state for each product.
using (IXlCell cell = row.CreateCell()) {
cell.SetFormula(XlFunc.Subtotal(XlCellRange.FromLTRB(6, firstDataRowIndex, 6, row.RowIndex - 1), XlSummary.Sum, false));
cell.ApplyFormatting(totalRowFormatting);
cell.ApplyFormatting(XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Light2, -0.1)));
}
}
}
void GenerateGrandTotalRow(IXlSheet sheet) {
// Create the grand total row.
using (IXlRow row = sheet.CreateRow()) {
// Skip the first cell in the row.
row.SkipCells(1);
// Create the "Grand Total" cell and specify its format settings.
using (IXlCell cell = row.CreateCell()) {
cell.Value = "Grand Total";
cell.ApplyFormatting(grandTotalRowFormatting);
cell.Formatting.Fill = XlFill.SolidFill(XlColor.FromTheme(XlThemeColor.Accent2, 0.4));
}
// Create five successive cells displaying quarterly total sales and annual sales for all states. The SUBTOTAL function is used to calculate subtotals for the related rows in each column.
for (int j = 0; j < 5; j++) {
using (IXlCell cell = row.CreateCell()) {
cell.SetFormula(XlFunc.Subtotal(XlCellRange.FromLTRB(j + 2, 3, j + 2, row.RowIndex - 1), XlSummary.Sum, false));
cell.ApplyFormatting(grandTotalRowFormatting);
}
}
}
}
void SetupPageParameters(IXlSheet sheet) {
// Specify the header and footer for the odd-numbered pages.
sheet.HeaderFooter.OddHeader = XlHeaderFooter.FromLCR(XlHeaderFooter.Bold + "DevAV", null, XlHeaderFooter.Date);
sheet.HeaderFooter.OddFooter = XlHeaderFooter.FromLCR("Sales report", null, XlHeaderFooter.PageNumber + " of " + XlHeaderFooter.PageTotal);
// Specify page settings.
sheet.PageSetup = new XlPageSetup();
// Scale the print area to fit to one page wide.
sheet.PageSetup.FitToPage = true;
sheet.PageSetup.FitToWidth = 1;
sheet.PageSetup.FitToHeight = 0;
}
}
}