Skip to content

Commit 318d45d

Browse files
committed
final update
1 parent e3e5ba0 commit 318d45d

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

ExcelInterface/excelinterface.cs

+34-28
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,51 @@ namespace ExcelWs
1010
{
1111
public static class Interface
1212
{
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>
1716
public static Stopwatch ExcelSetTime { get; private set; } = new Stopwatch();
1817

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)
2026
{
21-
//Start timer
22-
ExcelGetTime.Start();
23-
2427
Excel.Application excelApp = new Excel.Application();
2528
Excel.Workbook workbook = excelApp.Workbooks.Open(path, ReadOnly: true);
2629

2730
//seleziono la scheda
2831
Excel._Worksheet worksheet = (Excel._Worksheet)workbook.Sheets[sheetNumber];
2932
Excel.Range range = worksheet.UsedRange;
3033

31-
RowCount = range.Rows.Count;
32-
ColCount = range.Columns.Count;
33-
3434
// Array che conterrà il foglio excel completo
3535
object[,] valueArray = null;
36-
3736
valueArray = range.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);
3837

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++)
4140
{
4241
List<dynamic> row = new List<dynamic>(valueArray.GetLength(1));
43-
// build a list of column values for the row
42+
4443
for (int colIndex = 1; colIndex <= valueArray.GetLength(1); colIndex++)
4544
{
4645
row.Add(valueArray[rowIndex, colIndex]);
4746
}
4847
yield return row;
4948
}
50-
5149
workbook.Close();
52-
53-
//stop timer
54-
ExcelGetTime.Stop();
5550
}
5651

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>
5758
public static void SetExcelRow<T>(List<T> data)
5859
{
5960
try
@@ -71,8 +72,7 @@ public static void SetExcelRow<T>(List<T> data)
7172

7273
//scrivo l'intestazione
7374
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());
7676

7777
//scrivo il contenuto
7878
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)
8888

8989
//stop timer
9090
ExcelSetTime.Stop();
91-
9291
}
9392
catch (Exception ex)
9493
{
9594
Console.WriteLine(ex.ToString());
9695
}
9796
}
9897

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)
100104
{
101-
102105
string[,] elements = new string[1, list.Count()];
103106

104107
for (int i = 0; i < list.Count(); i++)
105-
{
106108
elements[0, i] = list.ElementAt(i);
107-
}
108109

109110
return elements;
110111
}
111112

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)
113120
{
114121
var props = typeof(T).GetProperties();
115122

@@ -124,7 +131,6 @@ public static void SetExcelRow<T>(List<T> data)
124131
ii++;
125132
}
126133
}
127-
128134
return elements;
129135
}
130136
}

Test/Program.cs

+8-10
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,28 @@ class Program
1212
{
1313
static void Main(string[] args)
1414
{
15-
//var x = Interface.GetExcelRowEnumerator(1, args[0]);
15+
var x = Interface.GetExcelRowEnumerator(1, args[0]);
1616

17-
List<Prova> prova = new List<Prova>();
17+
List<Prova> lista = new List<Prova>();
1818

19-
for (int i = 0; i < 500; i++)
19+
foreach (var item in x)
2020
{
21-
prova.Add(new Prova {
22-
Nome = $"Claudio {i}",
23-
Cognome = $"Mola {i+i}",
24-
Telefono = i
21+
lista.Add(new Prova() {
22+
Nome = item[1],
23+
Telefono = (int)item[0]
2524
});
2625
}
2726

28-
Interface.SetExcelRow<Prova>(prova);
27+
Interface.SetExcelRow<Prova>(lista);
2928
Console.WriteLine($"Creating time: {Interface.ExcelSetTime.Elapsed}");
30-
29+
3130
Console.ReadLine();
3231
}
3332

3433

3534
public class Prova
3635
{
3736
public string Nome { get; set; }
38-
public string Cognome { get; set; }
3937
public int Telefono { get; set; }
4038
}
4139
}

0 commit comments

Comments
 (0)