@@ -10,50 +10,51 @@ namespace ExcelWs
10
10
{
11
11
public static class Interface
12
12
{
13
- public static int RowCount { get ; private set ; }
14
- public static int ColCount { get ; private set ; }
15
-
16
- public static Stopwatch ExcelGetTime { get ; private set ; } = new Stopwatch ( ) ;
13
+ /// <summary>
14
+ /// Time to perform SetExcel operation
15
+ /// </summary>
17
16
public static Stopwatch ExcelSetTime { get ; private set ; } = new Stopwatch ( ) ;
18
17
19
- public static IEnumerable < List < dynamic > > GetExcelRowEnumerator ( int sheetNumber , string path )
18
+ /// <summary>
19
+ /// Get Excel to List
20
+ /// </summary>
21
+ /// <param name="sheetNumber">Sheet number start from 1</param>
22
+ /// <param name="path">Path of excel file</param>
23
+ /// <param name="RowStart">Optional Row</param>
24
+ /// <returns>List of Excel data</returns>
25
+ public static IEnumerable < List < dynamic > > GetExcelRowEnumerator ( int sheetNumber , string path , int RowStart = 1 )
20
26
{
21
- //Start timer
22
- ExcelGetTime . Start ( ) ;
23
-
24
27
Excel . Application excelApp = new Excel . Application ( ) ;
25
28
Excel . Workbook workbook = excelApp . Workbooks . Open ( path , ReadOnly : true ) ;
26
29
27
30
//seleziono la scheda
28
31
Excel . _Worksheet worksheet = ( Excel . _Worksheet ) workbook . Sheets [ sheetNumber ] ;
29
32
Excel . Range range = worksheet . UsedRange ;
30
33
31
- RowCount = range . Rows . Count ;
32
- ColCount = range . Columns . Count ;
33
-
34
34
// Array che conterrà il foglio excel completo
35
35
object [ , ] valueArray = null ;
36
-
37
36
valueArray = range . get_Value ( Excel . XlRangeValueDataType . xlRangeValueDefault ) ;
38
37
39
- // build and yield each row at a time
40
- for ( int rowIndex = 1 ; rowIndex <= valueArray . GetLength ( 0 ) ; rowIndex ++ )
38
+ // Costruisco e yield ogni riga
39
+ for ( int rowIndex = RowStart ; rowIndex <= valueArray . GetLength ( 0 ) ; rowIndex ++ )
41
40
{
42
41
List < dynamic > row = new List < dynamic > ( valueArray . GetLength ( 1 ) ) ;
43
- // build a list of column values for the row
42
+
44
43
for ( int colIndex = 1 ; colIndex <= valueArray . GetLength ( 1 ) ; colIndex ++ )
45
44
{
46
45
row . Add ( valueArray [ rowIndex , colIndex ] ) ;
47
46
}
48
47
yield return row ;
49
48
}
50
-
51
49
workbook . Close ( ) ;
52
-
53
- //stop timer
54
- ExcelGetTime . Stop ( ) ;
55
50
}
56
51
52
+
53
+ /// <summary>
54
+ /// List to Excel
55
+ /// </summary>
56
+ /// <typeparam name="T">Class representing data model</typeparam>
57
+ /// <param name="data">List of data</param>
57
58
public static void SetExcelRow < T > ( List < T > data )
58
59
{
59
60
try
@@ -71,8 +72,7 @@ public static void SetExcelRow<T>(List<T> data)
71
72
72
73
//scrivo l'intestazione
73
74
Excel . Range head = workSheet . Range [ workSheet . Cells [ 1 , 1 ] , workSheet . Cells [ 1 , collumn ] ] ;
74
- var prova = ListToArray ( prop . Select ( x => x . Name ) . ToList ( ) ) ;
75
- head . Value2 = prova ;
75
+ head . Value2 = ListToArray ( prop . Select ( x => x . Name ) . ToList ( ) ) ;
76
76
77
77
//scrivo il contenuto
78
78
Excel . Range body = workSheet . Range [ workSheet . Cells [ 2 , 1 ] , workSheet . Cells [ row , collumn ] ] ;
@@ -88,28 +88,35 @@ public static void SetExcelRow<T>(List<T> data)
88
88
89
89
//stop timer
90
90
ExcelSetTime . Stop ( ) ;
91
-
92
91
}
93
92
catch ( Exception ex )
94
93
{
95
94
Console . WriteLine ( ex . ToString ( ) ) ;
96
95
}
97
96
}
98
97
99
- static public string [ , ] ListToArray ( List < string > list )
98
+ /// <summary>
99
+ /// List to multidimensional Array
100
+ /// </summary>
101
+ /// <param name="list">Rappresent Data</param>
102
+ /// <returns></returns>
103
+ static private string [ , ] ListToArray ( List < string > list )
100
104
{
101
-
102
105
string [ , ] elements = new string [ 1 , list . Count ( ) ] ;
103
106
104
107
for ( int i = 0 ; i < list . Count ( ) ; i ++ )
105
- {
106
108
elements [ 0 , i ] = list . ElementAt ( i ) ;
107
- }
108
109
109
110
return elements ;
110
111
}
111
112
112
- static public string [ , ] ListToArray < T > ( List < T > list )
113
+ /// <summary>
114
+ /// List<T> to multidimensional Array
115
+ /// </summary>
116
+ /// <typeparam name="T">Model Class</typeparam>
117
+ /// <param name="list">Rappresent Data</param>
118
+ /// <returns></returns>
119
+ static private string [ , ] ListToArray < T > ( List < T > list )
113
120
{
114
121
var props = typeof ( T ) . GetProperties ( ) ;
115
122
@@ -124,7 +131,6 @@ public static void SetExcelRow<T>(List<T> data)
124
131
ii ++ ;
125
132
}
126
133
}
127
-
128
134
return elements ;
129
135
}
130
136
}
0 commit comments