Skip to content

Commit dd5d94d

Browse files
committed
small fix
1 parent 82277f6 commit dd5d94d

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/Example/Data/Context.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3030
//optionsBuilder.UseFileContext(new FileContextCore.Serializer.XMLSerializer());
3131

3232
//CSV
33-
optionsBuilder.UseFileContext(new FileContextCore.Serializer.CSVSerializer());
33+
//optionsBuilder.UseFileContext(new FileContextCore.Serializer.CSVSerializer());
3434

3535
//Excel
36-
//optionsBuilder.UseFileContext(new FileContextCore.CombinedManager.ExcelManager());
36+
optionsBuilder.UseFileContext(new FileContextCore.CombinedManager.ExcelManager());
3737
}
3838

3939
protected override void OnModelCreating(ModelBuilder modelBuilder)

src/FileContextCore/CombinedManager/ExcelManager.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,29 @@ public class ExcelManager : ICombinedManager
1414
{
1515
private string password = "";
1616

17-
public ExcelManager(string _password = "")
17+
private string fileName = "";
18+
19+
private string folder = Path.Combine(AppContext.BaseDirectory, "appdata");
20+
21+
public ExcelManager(string _password = "", string _fileName = "data.xlsx", string _folder = "")
1822
{
1923
password = _password;
24+
fileName = _fileName;
25+
26+
if(_folder != "")
27+
{
28+
folder = _folder;
29+
}
2030
}
2131

2232
FileInfo GetFilePath(string fileName)
2333
{
24-
string path = Path.Combine(AppContext.BaseDirectory, "appdata");
25-
Directory.CreateDirectory(path);
34+
if (!Directory.Exists(folder))
35+
{
36+
Directory.CreateDirectory(folder);
37+
}
2638

27-
return new FileInfo(Path.Combine(path, fileName));
39+
return new FileInfo(Path.Combine(folder, fileName));
2840
}
2941

3042
public List<T> GetItems<T>()
@@ -34,11 +46,11 @@ public List<T> GetItems<T>()
3446

3547
if (password != "")
3648
{
37-
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
49+
package = new ExcelPackage(GetFilePath(fileName), password);
3850
}
3951
else
4052
{
41-
package = new ExcelPackage(GetFilePath("data.xlsx"));
53+
package = new ExcelPackage(GetFilePath(fileName));
4254
}
4355

4456
ExcelWorksheet ws = package.Workbook.Worksheets[t.Name];
@@ -53,25 +65,26 @@ public List<T> GetItems<T>()
5365
properties.Add(i + 1, props.FirstOrDefault(x => x.Name == (string)ws.Cells[1, i + 1].Value));
5466
}
5567

56-
List<T> result = new List<T>();// (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t));
68+
List<T> result = new List<T>();
5769

5870
for (int i = 1; i < ws.Dimension.Rows; i++)
5971
{
6072
T item = (T)Activator.CreateInstance(t);
6173

6274
foreach (KeyValuePair<int, PropertyInfo> prop in properties)
6375
{
64-
if (prop.Value.PropertyType == typeof(TimeSpan))
76+
Type type = prop.Value.PropertyType;
77+
if (type == typeof(TimeSpan))
6578
{
6679
prop.Value.SetValue(item, TimeSpan.Parse((string)ws.Cells[i + 1, prop.Key].Value));
6780
}
68-
else if (prop.Value.PropertyType == typeof(Guid))
81+
else if (type == typeof(Guid))
6982
{
7083
prop.Value.SetValue(item, Guid.Parse((string)ws.Cells[i + 1, prop.Key].Value));
7184
}
7285
else
7386
{
74-
prop.Value.SetValue(item, Convert.ChangeType(ws.Cells[i + 1, prop.Key].Value, prop.Value.PropertyType));
87+
prop.Value.SetValue(item, Convert.ChangeType(ws.Cells[i + 1, prop.Key].Value, type));
7588
}
7689
}
7790

@@ -118,11 +131,11 @@ public void SaveItems<T>(List<T> list)
118131

119132
if (password != "")
120133
{
121-
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
134+
package = new ExcelPackage(GetFilePath(fileName), password);
122135
}
123136
else
124137
{
125-
package = new ExcelPackage(GetFilePath("data.xlsx"));
138+
package = new ExcelPackage(GetFilePath(fileName));
126139
}
127140

128141
Type t = list.GetType().GenericTypeArguments[0];
@@ -166,11 +179,11 @@ public bool Clear()
166179

167180
if (password != "")
168181
{
169-
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
182+
package = new ExcelPackage(GetFilePath(fileName), password);
170183
}
171184
else
172185
{
173-
package = new ExcelPackage(GetFilePath("data.xlsx"));
186+
package = new ExcelPackage(GetFilePath(fileName));
174187
}
175188

176189
if(package.Workbook.Worksheets.Count > 0)
@@ -204,11 +217,11 @@ public bool Exists()
204217

205218
if (password != "")
206219
{
207-
package = new ExcelPackage(GetFilePath("data.xlsx"), password);
220+
package = new ExcelPackage(GetFilePath(fileName), password);
208221
}
209222
else
210223
{
211-
package = new ExcelPackage(GetFilePath("data.xlsx"));
224+
package = new ExcelPackage(GetFilePath(fileName));
212225
}
213226

214227
result = package.Workbook.Worksheets.Count > 0;

0 commit comments

Comments
 (0)