/
ExcelLoaderViewModel.cs
153 lines (135 loc) · 4.24 KB
/
ExcelLoaderViewModel.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
using System.Collections.Generic;
using System.ComponentModel;
using ExcelLoader.Helpers;
using System.Windows;
using System.Windows.Input;
using System;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using Microsoft.Win32;
using System.IO;
namespace ExcelLoader
{
public class ExcelLoaderViewModel : INotifyPropertyChanged
{
private static ExcelLoaderViewModel _instance;
public static ExcelLoaderViewModel GetInstance()
{
return _instance;
}
public ExcelLoaderViewModel()
{
_instance = this;
MyExcelHelper = new ExcelHelper();
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CloseWorkbook);
}
#region Properties
public bool IsLoaded
{
get
{
return (ExcelFileName != null);
}
}
string excelFileName = "";
public string ExcelFileName
{
get
{
return excelFileName;
}
set
{
excelFileName = value;
NotifyPropertyChanged();
}
}
private string currentStatus = "None";
public string CurrentStatus
{
get
{
return currentStatus;
}
set
{
currentStatus = value;
NotifyPropertyChanged();
}
}
private ObservableCollection<RuleDefinition> rules ;
public ObservableCollection<RuleDefinition> Rules
{
get
{
return rules;
}
set
{
rules = value;
NotifyPropertyChanged();
}
}
private ExcelHelper MyExcelHelper { get; set; }
#endregion Properties
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private RelayCommand loadExcelFileCommand;
public ICommand LoadExcelFileCommand
{
get
{
return loadExcelFileCommand ?? (loadExcelFileCommand = new RelayCommand(p => LoadExcelFile()));
}
}
public void LoadExcelFile()
{
var ofd = new OpenFileDialog();
ofd.Filter = "Excel files (*.xlsx) | *.xlsx;";
ofd.Multiselect = false;
var result = ofd.ShowDialog();
if (result == true)
{
ExcelFileName = ofd.FileName;
CurrentStatus = "Loading file: " + ExcelFileName;
MyExcelHelper.Load(ExcelFileName);
Rules = MyExcelHelper.Rules;
CurrentStatus = "File Loaded";
}
}
private RelayCommand generateExcelResultsCommand;
public ICommand GenerateExcelResultsCommand
{
get
{
return generateExcelResultsCommand ?? (generateExcelResultsCommand = new RelayCommand(p => GenerateExcelResults()));
}
}
public void GenerateExcelResults()
{
if (ExcelFileName == null)
{
MessageBox.Show("Please Load a file before attempting to generate the results", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
}
else
{
CurrentStatus = "Generating Results";
MyExcelHelper.GenerateOutputSheets(ExcelFileName);
CurrentStatus = "Result Generation Completed";
}
}
private void CloseWorkbook(object sender, EventArgs e)
{
MyExcelHelper.CloseWorkbook();
}
}
}