|
| 1 | +using OfficeOpenXml; |
| 2 | +using System; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | +using System.Text; |
| 9 | + |
| 10 | +namespace FileContextCore.CombinedManager |
| 11 | +{ |
| 12 | + public class ExcelManager : ICombinedManager |
| 13 | + { |
| 14 | + private ExcelPackage package; |
| 15 | + |
| 16 | + public ExcelManager(string password = "") |
| 17 | + { |
| 18 | + if (password != "") |
| 19 | + { |
| 20 | + package = new ExcelPackage(GetFilePath("data.xlsx"), password); |
| 21 | + } |
| 22 | + else |
| 23 | + { |
| 24 | + package = new ExcelPackage(GetFilePath("data.xlsx")); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + FileInfo GetFilePath(string fileName) |
| 29 | + { |
| 30 | + string path = Path.Combine(AppContext.BaseDirectory, "appdata"); |
| 31 | + Directory.CreateDirectory(path); |
| 32 | + |
| 33 | + return new FileInfo(Path.Combine(path, fileName)); |
| 34 | + } |
| 35 | + |
| 36 | + public IList GetItems(Type t) |
| 37 | + { |
| 38 | + ExcelWorksheet ws = package.Workbook.Worksheets[t.Name]; |
| 39 | + |
| 40 | + if(ws != null) |
| 41 | + { |
| 42 | + List<PropertyInfo> props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToList(); |
| 43 | + Dictionary<int, PropertyInfo> properties = new Dictionary<int, PropertyInfo>(); |
| 44 | + |
| 45 | + for(int i = 0; i < ws.Dimension.Columns; i++) |
| 46 | + { |
| 47 | + properties.Add(i + 1, props.FirstOrDefault(x => x.Name == (string)ws.Cells[1, i + 1].Value)); |
| 48 | + } |
| 49 | + |
| 50 | + IList result = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t)); |
| 51 | + |
| 52 | + for (int i = 1; i < ws.Dimension.Rows; i++) |
| 53 | + { |
| 54 | + object item = Activator.CreateInstance(t); |
| 55 | + |
| 56 | + foreach(KeyValuePair<int, PropertyInfo> prop in properties) |
| 57 | + { |
| 58 | + prop.Value.SetValue(item, Convert.ChangeType(ws.Cells[i + 1, prop.Key].Value, prop.Value.PropertyType)); |
| 59 | + } |
| 60 | + |
| 61 | + result.Add(item); |
| 62 | + } |
| 63 | + |
| 64 | + return result; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + ws = package.Workbook.Worksheets.Add(t.Name); |
| 69 | + |
| 70 | + PropertyInfo[] props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToArray(); |
| 71 | + |
| 72 | + for (int i = 0; i < props.Count(); i++) |
| 73 | + { |
| 74 | + PropertyInfo pi = props[i]; |
| 75 | + ws.Cells[1, i + 1].Value = pi.Name; |
| 76 | + ws.Column(i + 1).AutoFit(); |
| 77 | + } |
| 78 | + |
| 79 | + ws.View.FreezePanes(2, 1); |
| 80 | + |
| 81 | + package.Save(); |
| 82 | + return (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(t)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public void SaveItems(IList list) |
| 87 | + { |
| 88 | + Type t = list.GetType().GenericTypeArguments[0]; |
| 89 | + PropertyInfo[] props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToArray(); |
| 90 | + |
| 91 | + ExcelWorksheet ws = package.Workbook.Worksheets[t.Name]; |
| 92 | + |
| 93 | + for (int i = 0; i < list.Count; i++) |
| 94 | + { |
| 95 | + object item = list[i]; |
| 96 | + |
| 97 | + for (int x = 0; x < props.Count(); x++) |
| 98 | + { |
| 99 | + PropertyInfo pi = props[x]; |
| 100 | + |
| 101 | + ws.SetValue(i + 2, x + 1, pi.GetValue(item).ToString()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + for (int i = 0; i < ws.Dimension.Columns; i++) |
| 106 | + { |
| 107 | + ws.Column(i + 1).AutoFit(); |
| 108 | + } |
| 109 | + |
| 110 | + package.Save(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments