-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExportToExcel.cs
319 lines (282 loc) · 14.5 KB
/
ExportToExcel.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
using Microsoft.Office.Interop.Excel;
using Microsoft.TeamFoundation.TestManagement.Client;
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ExcelApp = Microsoft.Office.Interop.Excel.Application;
namespace TestPlanViewer
{
/// <summary>
/// Class for creating excel documents
/// </summary>
public class ExportToExcel
{
/// <summary>
/// A handle to the excel application
/// </summary>
private ExcelApp app = null;
/// <summary>
/// A handle to the excel application's worksheet
/// </summary>
private Worksheet worksheet = null;
/// <summary>
/// Launch a speadsheet with the cumulative content
/// </summary>
/// <param name="results">The cumulative results</param>
public void LaunchExcel(AllResults results)
{
// Get the file name
string file = SharedUtils.GetFileName("Excel Open XML | *.xlsm", "xlsm");
if (string.IsNullOrEmpty(file))
{
// No file was provided so quite
return;
}
this.LaunchExcel(results, file);
}
/// <summary>
/// Launch a speadsheet with the cumulative content
/// </summary>
/// <param name="results">The cumulative content</param>
/// <param name="fileDestination">File to save the content to</param>
public void LaunchExcel(AllResults results, string fileDestination)
{
// Make sure the destination file has the correct extension
if (!fileDestination.EndsWith(".xlsm", StringComparison.CurrentCultureIgnoreCase))
{
fileDestination = fileDestination + ".xlsm";
}
// Remove the existing file to avoid mystery
if (File.Exists(fileDestination))
{
try
{
File.SetAttributes(fileDestination, FileAttributes.Normal);
File.Delete(fileDestination);
}
catch (Exception e)
{
MessageBox.Show("Failed create file '" + fileDestination + "' because :\r\n" + e.Message, "Create Excel Document Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
// Create the instance of excel we will be interacting with
if (!this.CreateHiddenExcelInstance())
{
return;
}
// Load the data into a temp file
string tempFile = Path.GetTempFileName();
SharedUtils.CreateResultFlatFile(results, tempFile);
// Load the temp file
this.app.Workbooks.OpenText(tempFile, Type.Missing, 1, XlTextParsingType.xlDelimited, XlTextQualifier.xlTextQualifierNone, Type.Missing, true, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Format the active sheet
this.FormatSheet(results);
// Silently save the file
this.app.ActiveWorkbook.SaveAs(fileDestination, XlFileFormat.xlOpenXMLWorkbookMacroEnabled, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Close the copy of excel that we have an programmatic reference to
this.CloseHiddenExcelInstance();
// Cleanup the temp file
File.Delete(tempFile);
// Open excel in an external process
SharedUtils.OpenExternalProcess(fileDestination, "Failed To Open Excel File");
}
/// <summary>
/// Create a hidden instance of excel
/// </summary>
/// <returns>True if an instance of excel was created</returns>
private bool CreateHiddenExcelInstance()
{
try
{
// Create a programmatic instance of excel and make sure it is not visible
this.app = new ExcelApp();
this.app.Visible = false;
return true;
}
catch (Exception e)
{
MessageBox.Show("Failed create an excel document because:\r\n" + e.Message, "Create Excel Document Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}
/// <summary>
/// Close the hidden instance of excel
/// </summary>
private void CloseHiddenExcelInstance()
{
// Close the copy of excel that we have an programmatic reference to
if (this.app != null)
{
try
{
Workbook workbook = this.app.ActiveWorkbook;
this.app.Workbooks.Close();
this.app.Quit();
Marshal.ReleaseComObject(this.worksheet);
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(this.app);
Marshal.FinalReleaseComObject(this.app);
}
catch (Exception e)
{
MessageBox.Show("Failed close hidden copy of excel because:\r\n" + e.Message, "Close Excel Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
/// <summary>
/// Format the spreadsheet
/// </summary>
/// <param name="results">The cumulative results</param>
private void FormatSheet(AllResults results)
{
this.worksheet = this.app.ActiveSheet;
this.SetTotals(results);
this.FormattingDuplicates(results.Count);
this.FormatHeaderRow();
}
/// <summary>
/// Set the result totals
/// </summary>
/// <param name="results">The cumulative results</param>
private void SetTotals(AllResults results)
{
int topRow = 3;
int leftStart = 17;
int leftStartTotalResults = leftStart + 1;
int leftStartLatestResults = leftStart + 2;
// Creates the main header
this.CreateDataCell(topRow, leftStart, "Counts", Color.LightBlue);
this.CreateDataCell(topRow, leftStartTotalResults, "All", Color.LightYellow);
this.CreateDataCell(topRow, leftStartLatestResults, "Latest", Color.LightYellow);
this.CreateDataCell(topRow + 1, leftStart, "Totals", Color.LightGray);
this.CreateDataCell(topRow + 2, leftStart, "Passed", Color.LightGray);
this.CreateDataCell(topRow + 3, leftStart, "Failed", Color.LightGray);
this.CreateDataCell(topRow + 4, leftStart, "Blocked", Color.LightGray);
this.CreateDataCell(topRow + 5, leftStart, "Active", Color.LightGray);
this.CreateDataCell(topRow + 6, leftStart, "NotExecuted", Color.LightGray);
this.CreateDataCell(topRow + 7, leftStart, "Inconclusive", Color.LightGray);
this.CreateDataCell(topRow + 8, leftStart, "Unspecified", Color.LightGray);
this.CreateDataCell(topRow + 9, leftStart, "Other", Color.LightGray);
// Total results
this.CreateDataCell(topRow + 2, leftStartTotalResults, results.GetOutcomeCount(TestOutcome.Passed, false).ToString());
this.CreateDataCell(topRow + 3, leftStartTotalResults, results.GetOutcomeCount(TestOutcome.Failed, false).ToString());
this.CreateDataCell(topRow + 4, leftStartTotalResults, results.GetOutcomeCount(TestOutcome.Blocked, false).ToString());
this.CreateDataCell(topRow + 5, leftStartTotalResults, results.GetActiveCount(false).ToString());
this.CreateDataCell(topRow + 6, leftStartTotalResults, results.GetOutcomeCount(TestOutcome.NotExecuted, false).ToString());
this.CreateDataCell(topRow + 7, leftStartTotalResults, results.GetOutcomeCount(TestOutcome.Inconclusive, false).ToString());
this.CreateDataCell(topRow + 8, leftStartTotalResults, results.GetOutcomeCount(TestOutcome.Unspecified, false).ToString());
this.CreateDataCell(topRow + 9, leftStartTotalResults, results.GetOtherCount(false).ToString());
string sumRange = string.Format("=SUM({0}{1}:{0}{2})", this.LetterForColumn(leftStart + 1), topRow + 2, topRow + 9);
this.CreateDataCell(topRow + 1, leftStartTotalResults, sumRange);
// Latest (without duplicates) results
this.CreateDataCell(topRow + 2, leftStartLatestResults, results.GetOutcomeCount(TestOutcome.Passed).ToString());
this.CreateDataCell(topRow + 3, leftStartLatestResults, results.GetOutcomeCount(TestOutcome.Failed).ToString());
this.CreateDataCell(topRow + 4, leftStartLatestResults, results.GetOutcomeCount(TestOutcome.Blocked).ToString());
this.CreateDataCell(topRow + 5, leftStartLatestResults, results.GetActiveCount().ToString());
this.CreateDataCell(topRow + 6, leftStartLatestResults, results.GetOutcomeCount(TestOutcome.NotExecuted).ToString());
this.CreateDataCell(topRow + 7, leftStartLatestResults, results.GetOutcomeCount(TestOutcome.Inconclusive).ToString());
this.CreateDataCell(topRow + 8, leftStartLatestResults, results.GetOutcomeCount(TestOutcome.Unspecified).ToString());
this.CreateDataCell(topRow + 9, leftStartLatestResults, results.GetOtherCount().ToString());
sumRange = string.Format("=SUM({0}{1}:{0}{2})", this.LetterForColumn(leftStart + 2), topRow + 2, topRow + 9);
this.CreateDataCell(topRow + 1, leftStartLatestResults, sumRange);
}
/// <summary>
/// Get the letter representation of a column
/// </summary>
/// <param name="column">The column number</param>
/// <returns>The alpha value of the given column</returns>
private string LetterForColumn(int column)
{
// Deal with invalid column numbers
if (column <= 0)
{
throw new ArgumentOutOfRangeException("column", "Column numbers must be 1 or greater.");
}
column--;
string columnString = Convert.ToString((char)('A' + (column % 26)));
while (column >= 26)
{
column = (column / 26) - 1;
columnString = Convert.ToString((char)('A' + (column % 26))) + columnString;
}
return columnString;
}
/// <summary>
/// Format duplicate results
/// </summary>
/// <param name="numberOfResults">The number results in the spreadsheet</param>
private void FormattingDuplicates(int numberOfResults)
{
for (int i = 1; i < numberOfResults + 2; i++)
{
string primary = ((Range)this.worksheet.Cells[i, 14]).Text;
if (primary.Equals("FALSE"))
{
this.worksheet.Rows.Range[string.Format("A{0}", i), string.Format("N{0}", i)].Font.Color = Color.DarkGray;
this.worksheet.Rows.Range[string.Format("A{0}", i), string.Format("N{0}", i)].Interior.Color = Color.LightYellow;
}
}
}
/// <summary>
/// Create data cell with standard black text and white background header
/// </summary>
/// <param name="row">The row number</param>
/// <param name="col">The column number</param>
/// <param name="text">The text to add to the specific cell</param>
private void CreateDataCell(int row, int col, string text)
{
this.CreateDataCell(row, col, text, Color.White, Color.Black);
}
/// <summary>
/// Create a data cell with black text and the specified background colors
/// </summary>
/// <param name="row">The row number</param>
/// <param name="col">The column number</param>
/// <param name="text">The text to add to the specific cell</param>
/// <param name="interiorColor">The background color for the specific cell</param>
private void CreateDataCell(int row, int col, string text, Color interiorColor)
{
this.CreateDataCell(row, col, text, interiorColor, Color.Black);
}
/// <summary>
/// Create a data cell with the specified text and background colors
/// </summary>
/// <param name="row">The row number</param>
/// <param name="col">The column number</param>
/// <param name="text">The text to add to the specific cell</param>
/// <param name="interiorColor">The background color of the specific cell</param>
/// <param name="fontColor">The text color for the specific cell</param>
private void CreateDataCell(int row, int col, string text, Color interiorColor, Color fontColor)
{
// Set the text
this.worksheet.Cells[row, col] = text;
// Set the coloring
Range range = this.worksheet.Cells[row, col];
range.Interior.Color = interiorColor;
range.Font.Color = fontColor;
// Add borders to the header cell
range.Borders.Color = System.Drawing.Color.Black.ToArgb();
}
/// <summary>
/// Format the header row
/// </summary>
private void FormatHeaderRow()
{
// Auto fit columns that look funny without auto fit
this.worksheet.Cells[1, 1].EntireColumn.AutoFit();
this.worksheet.Cells[1, 9].EntireColumn.AutoFit();
this.worksheet.Cells[1, 10].EntireColumn.AutoFit();
this.worksheet.Cells[1, 16].EntireColumn.AutoFit();
// Format the first row
Range firstRow = this.worksheet.Rows.Range[string.Format("{0}{1}", this.LetterForColumn(1), 1), string.Format("{0}{1}", this.LetterForColumn(15), 1)];
this.worksheet.Application.ActiveWindow.SplitRow = 1;
this.worksheet.Application.ActiveWindow.FreezePanes = true;
firstRow.Borders.Color = Color.Black;
firstRow.Interior.Color = Color.DarkKhaki;
firstRow.AutoFilter(1, Type.Missing, XlAutoFilterOperator.xlAnd, Type.Missing, true);
}
}
}