@@ -12,18 +12,11 @@ namespace FileContextCore.CombinedManager
1212{
1313 public class ExcelManager : ICombinedManager
1414 {
15- private ExcelPackage package ;
15+ private string password = "" ;
1616
17- public ExcelManager ( string password = "" )
17+ public ExcelManager ( string _password = "" )
1818 {
19- if ( password != "" )
20- {
21- package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) , password ) ;
22- }
23- else
24- {
25- package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) ) ;
26- }
19+ password = _password ;
2720 }
2821
2922 FileInfo GetFilePath ( string fileName )
@@ -36,14 +29,25 @@ FileInfo GetFilePath(string fileName)
3629
3730 public IList GetItems ( Type t )
3831 {
32+ ExcelPackage package ;
33+
34+ if ( password != "" )
35+ {
36+ package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) , password ) ;
37+ }
38+ else
39+ {
40+ package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) ) ;
41+ }
42+
3943 ExcelWorksheet ws = package . Workbook . Worksheets [ t . Name ] ;
4044
41- if ( ws != null )
45+ if ( ws != null )
4246 {
4347 List < PropertyInfo > props = t . GetRuntimeProperties ( ) . Where ( x => ! x . SetMethod . IsVirtual ) . ToList ( ) ;
4448 Dictionary < int , PropertyInfo > properties = new Dictionary < int , PropertyInfo > ( ) ;
45-
46- for ( int i = 0 ; i < ws . Dimension . Columns ; i ++ )
49+
50+ for ( int i = 0 ; i < ws . Dimension . Columns ; i ++ )
4751 {
4852 properties . Add ( i + 1 , props . FirstOrDefault ( x => x . Name == ( string ) ws . Cells [ 1 , i + 1 ] . Value ) ) ;
4953 }
@@ -53,13 +57,17 @@ public IList GetItems(Type t)
5357 for ( int i = 1 ; i < ws . Dimension . Rows ; i ++ )
5458 {
5559 object item = Activator . CreateInstance ( t ) ;
56-
57- foreach ( KeyValuePair < int , PropertyInfo > prop in properties )
60+
61+ foreach ( KeyValuePair < int , PropertyInfo > prop in properties )
5862 {
59- if ( prop . Value . PropertyType == typeof ( TimeSpan ) )
63+ if ( prop . Value . PropertyType == typeof ( TimeSpan ) )
6064 {
6165 prop . Value . SetValue ( item , TimeSpan . Parse ( ( string ) ws . Cells [ i + 1 , prop . Key ] . Value ) ) ;
6266 }
67+ else if ( prop . Value . PropertyType == typeof ( Guid ) )
68+ {
69+ prop . Value . SetValue ( item , Guid . Parse ( ( string ) ws . Cells [ i + 1 , prop . Key ] . Value ) ) ;
70+ }
6371 else
6472 {
6573 prop . Value . SetValue ( item , Convert . ChangeType ( ws . Cells [ i + 1 , prop . Key ] . Value , prop . Value . PropertyType ) ) ;
@@ -69,6 +77,8 @@ public IList GetItems(Type t)
6977 result . Add ( item ) ;
7078 }
7179
80+ package . Dispose ( ) ;
81+
7282 return result ;
7383 }
7484 else
@@ -86,13 +96,34 @@ public IList GetItems(Type t)
8696
8797 ws . View . FreezePanes ( 2 , 1 ) ;
8898
89- package . Save ( ) ;
99+ if ( password != "" )
100+ {
101+ package . Save ( password ) ;
102+ }
103+ else
104+ {
105+ package . Save ( ) ;
106+ }
107+
108+ package . Dispose ( ) ;
109+
90110 return ( IList ) Activator . CreateInstance ( typeof ( List < > ) . MakeGenericType ( t ) ) ;
91- }
111+ }
92112 }
93113
94114 public void SaveItems ( IList list )
95115 {
116+ ExcelPackage package ;
117+
118+ if ( password != "" )
119+ {
120+ package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) , password ) ;
121+ }
122+ else
123+ {
124+ package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) ) ;
125+ }
126+
96127 Type t = list . GetType ( ) . GenericTypeArguments [ 0 ] ;
97128 PropertyInfo [ ] props = t . GetRuntimeProperties ( ) . Where ( x => ! x . SetMethod . IsVirtual ) . ToArray ( ) ;
98129
@@ -110,12 +141,80 @@ public void SaveItems(IList list)
110141 }
111142 }
112143
113- for ( int i = 0 ; i < ws . Dimension . Columns ; i ++ )
144+ for ( int y = 0 ; y < ws . Dimension . Columns ; y ++ )
145+ {
146+ ws . Column ( y + 1 ) . AutoFit ( ) ;
147+ }
148+
149+ if ( password != "" )
150+ {
151+ package . Save ( password ) ;
152+ }
153+ else
154+ {
155+ package . Save ( ) ;
156+ }
157+
158+ package . Dispose ( ) ;
159+ }
160+
161+ public bool Clear ( )
162+ {
163+ bool result = false ;
164+ ExcelPackage package ;
165+
166+ if ( password != "" )
167+ {
168+ package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) , password ) ;
169+ }
170+ else
171+ {
172+ package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) ) ;
173+ }
174+
175+ if ( package . Workbook . Worksheets . Count > 0 )
176+ {
177+ for ( int i = 0 ; i < package . Workbook . Worksheets . Count ; i ++ )
178+ {
179+ package . Workbook . Worksheets . Delete ( i + 1 ) ;
180+ }
181+
182+ if ( password != "" )
183+ {
184+ package . Save ( password ) ;
185+ }
186+ else
187+ {
188+ package . Save ( ) ;
189+ }
190+
191+ result = true ;
192+ }
193+
194+ package . Dispose ( ) ;
195+
196+ return result ;
197+ }
198+
199+ public bool Exists ( )
200+ {
201+ bool result = false ;
202+ ExcelPackage package ;
203+
204+ if ( password != "" )
114205 {
115- ws . Column ( i + 1 ) . AutoFit ( ) ;
206+ package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) , password ) ;
116207 }
208+ else
209+ {
210+ package = new ExcelPackage ( GetFilePath ( "data.xlsx" ) ) ;
211+ }
212+
213+ result = package . Workbook . Worksheets . Count > 0 ;
214+
215+ package . Dispose ( ) ;
117216
118- package . Save ( ) ;
217+ return result ;
119218 }
120219 }
121220}
0 commit comments