@@ -16,6 +16,7 @@ class EXCELSerializer<T>
1616 private IEntityType entityType ;
1717 private string [ ] propertyKeys ;
1818 private readonly Type [ ] typeList ;
19+ private int [ ] propertyColumnIndices ;
1920 private readonly string password ;
2021 private readonly string databaseName ;
2122 private readonly string _location ;
@@ -24,7 +25,7 @@ class EXCELSerializer<T>
2425 private static Dictionary < string , ExcelPackage > packages = new Dictionary < string , ExcelPackage > ( ) ;
2526 private ExcelPackage package ;
2627 private ExcelWorksheet worksheet ;
27-
28+
2829
2930 FileInfo GetFilePath ( )
3031 {
@@ -44,6 +45,7 @@ public EXCELSerializer(IEntityType _entityType, string _password, string databas
4445 {
4546 entityType = _entityType ;
4647 propertyKeys = entityType . GetProperties ( ) . Select ( p => p . GetColumnName ( ) ) . ToArray ( ) ;
48+ propertyColumnIndices = new int [ propertyKeys . Length ] ;
4749 typeList = entityType . GetProperties ( ) . Select ( p => p . GetValueConverter ( ) ? . ProviderClrType ?? p . ClrType ) . ToArray ( ) ;
4850 password = _password ;
4951 this . databaseName = databaseName ;
@@ -81,6 +83,7 @@ public EXCELSerializer(IEntityType _entityType, string _password, string databas
8183 for ( int i = 0 ; i < propertyKeys . Length ; i ++ )
8284 {
8385 worksheet . Cells [ 1 , i + 1 ] . Value = propertyKeys [ i ] ;
86+ propertyColumnIndices [ i ] = i + 1 ;
8487 worksheet . Column ( i + 1 ) . AutoFit ( ) ;
8588 }
8689
@@ -95,6 +98,21 @@ public EXCELSerializer(IEntityType _entityType, string _password, string databas
9598 package . Save ( ) ;
9699 }
97100 }
101+ else
102+ {
103+ for ( int i = 0 ; i < propertyKeys . Length ; i ++ )
104+ {
105+ for ( int x = 0 ; x < worksheet . Dimension . Columns ; x ++ )
106+ {
107+ string val = worksheet . Cells [ 1 , x + 1 ] . GetValue < string > ( ) ;
108+ if ( propertyKeys [ i ] . Equals ( val , StringComparison . InvariantCultureIgnoreCase ) )
109+ {
110+ propertyColumnIndices [ i ] = x + 1 ;
111+ break ;
112+ }
113+ }
114+ }
115+ }
98116 }
99117
100118 public Dictionary < TKey , object [ ] > Deserialize < TKey > ( Dictionary < TKey , object [ ] > newList )
@@ -105,12 +123,12 @@ public Dictionary<TKey, object[]> Deserialize<TKey>(Dictionary<TKey, object[]> n
105123
106124 for ( int x = 0 ; x < propertyKeys . Length ; x ++ )
107125 {
108- object val = worksheet . Cells [ y , x + 1 ] . GetValue < string > ( ) . Deserialize ( typeList [ x ] ) ;
126+ object val = worksheet . Cells [ y , propertyColumnIndices [ x ] ] . GetValue < string > ( ) . Deserialize ( typeList [ x ] ) ;
109127 value . Add ( val ) ;
110128 }
111129
112- TKey key = SerializerHelper . GetKey < TKey , T > ( _keyValueFactory , entityType , propertyName =>
113- worksheet . Cells [ y , propertyKeys . IndexOf ( propertyName ) + 1 ] . GetValue < string > ( ) ) ;
130+ TKey key = SerializerHelper . GetKey < TKey , T > ( _keyValueFactory , entityType , propertyName =>
131+ worksheet . Cells [ y , propertyColumnIndices [ propertyKeys . IndexOf ( propertyName ) ] ] . GetValue < string > ( ) ) ;
114132
115133 newList . Add ( key , value . ToArray ( ) ) ;
116134 }
0 commit comments