-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExcelManager.cs
115 lines (94 loc) · 4.86 KB
/
ExcelManager.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
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using RussellScreener.Entities;
namespace RussellScreener {
public class ExcelManager {
#region Methods
/// <summary>
/// Write an Excel file with two worksheets: the selection of stocks for the final portfolio and a worksheet with all stocks from the ETF
/// with all fields given by the IEX API.
/// </summary>
/// <param name="excelFileName">Name of the target excel file</param>
/// <param name="portfolioStocks">List of stocks for the final portfolio selected according to the strategy rules</param>
/// <param name="allStocks">List of all stocks</param>
public void WriteExcelFile(string excelFileName, List<Stock> portfolioStocks, List<Stock> allStocks) {
var newFile = new FileInfo(excelFileName);
// discard any existing file
if (newFile.Exists) {
newFile.Delete(); // ensures we create a new workbook
newFile = new FileInfo(excelFileName);
}
var allStatsProperties = typeof(StockResultStats).GetProperties();
var allCompanyProperties = typeof(StockResultCompany).GetProperties();
using (var package = new ExcelPackage(newFile)) {
FillWorksheet(portfolioStocks, package.Workbook.Worksheets.Add("Selection"), allCompanyProperties, allStatsProperties);
FillWorksheet(allStocks, package.Workbook.Worksheets.Add("All Stocks"), allCompanyProperties, allStatsProperties);
package.Save();
}
}
/// <summary>
/// Create a worksheet containing a list of stocks.
/// The first header cells are a summary with the important metrics for Piard's rules.
/// The other properties coming from IEX API are then appended as additional header cells.
/// </summary>
/// <param name="stocks">List of stocks to write</param>
/// <param name="ws">Target Excel worksheet</param>
/// <param name="allCompanyProperties">All properties within the StockResultCompany object</param>
/// <param name="allStatsProperties">All properties within the StockResultStats object</param>
private void FillWorksheet(List<Stock> stocks, ExcelWorksheet ws, PropertyInfo[] allCompanyProperties, PropertyInfo[] allStatsProperties) {
ws.Cells[1, 1].Value = "Ticker";
ws.Cells[1, 2].Value = "Company";
ws.Cells[1, 3].Value = "Sector";
ws.Cells[1, 4].Value = "Dividend Yield";
ws.Cells[1, 5].Value = "ROA";
int currentHeaderCol = 6;
foreach (var prop in allCompanyProperties) {
ws.Cells[1, currentHeaderCol++].Value = prop.Name;
}
foreach (var prop in allStatsProperties) {
ws.Cells[1, currentHeaderCol++].Value = prop.Name;
}
int lastHeaderCol = currentHeaderCol;
int currentRow = 2;
foreach (var s in stocks) {
ws.Cells[currentRow, 1].Value = s.Ticker;
ws.Cells[currentRow, 2].Value = s.Company.CompanyName;
ws.Cells[currentRow, 3].Value = s.Company.Sector;
ws.Cells[currentRow, 4].Value = s.Stats.DividendYield;
ws.Cells[currentRow, 5].Value = s.Stats.ReturnOnAssets;
int currentCol = 6;
foreach (var prop in allCompanyProperties) {
ws.Cells[currentRow, currentCol].Value = prop.GetValue(s.Company);
currentCol++;
}
foreach (var prop in allStatsProperties) {
ws.Cells[currentRow, currentCol].Value = prop.GetValue(s.Stats);
currentCol++;
}
currentRow++;
}
// set header formatting for the first important cells
using (var range = ws.Cells[1, 1, 1, 5]) {
range.Style.Font.Bold = true;
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);
range.Style.Font.Color.SetColor(Color.White);
}
// different formatting for other header cells
using (var range = ws.Cells[1, 6, 1, lastHeaderCol]) {
range.Style.Font.Bold = true;
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(Color.DarkGreen);
range.Style.Font.Color.SetColor(Color.White);
}
// apply auto-filtering + autofit on all columns
ws.Cells[1, 1, 1, lastHeaderCol].AutoFilter = true;
ws.Cells.AutoFitColumns(0);
}
#endregion Methods
}
}