2012-07-10 2 views
0

Excel 시트에서 셀을 가져오고 있습니다. 이것은 제가 얻는 결과입니다.foreach 루프를 사용하여 Excel 시트에서 목록 생성

Cell A2 has value 113,295 
Cell B2 has value 540 
Cell C2 has value 41,044 
Cell A3 has value 192,653 
Cell B3 has value 510 
Cell C3 has value 41,037 

코드 :

 using (ExcelPackage package = new ExcelPackage(existingFile)) 
     { 
      ExcelWorksheet sheet = package.Workbook.Worksheets[1]; 

      //Select all cells in column d between 9990 and 10000 
      var query1 = (from cell in sheet.Cells["A2:C2"] select cell); 

      int count = 0; 
      string[] s = null; 

      foreach (var cell in query1) 
      { 
       Console.WriteLine("Cell {0} has value {1:N0}", cell.Address, cell.Value); 

      } 

내 문제는 내가 회원들과 함께, b와 c를 클래스를 만들려면 내가이 개체의 컬렉션을 보유 할 목록 <>를 원하는 것입니다.

현재 iterating 중이지만 for 루프에서 어느 셀이 A, B 또는 C인지 알 수 없습니다.

답변

0

당신은 파티션 방법을 작성하고 그것을 행의 길이를 줄 수 ... 귀하의 경우에는 내가 희망 3

IEnumerable<List<T>> Partition<T>(IEnumerable<T> col, int partitionSize) { 
if (col.Count() == 0) { 
    return new List<List<T>>(); 
} else { 
    var l = new List<List<T>>(); 
    l.Add(col.Take(partitionSize).ToList()); 
    return l.Concat(Partition(col.Skip(partitionSize), partitionSize)); 
} 
} 

class TheClass { 
public int A, B, C; 
} 

return Partition(sheet.Cells["A2:C2"], 3) 
     .Select(cells => new TheClass { 
      A = int.Parse(cells[0].Value), 
      B = int.Parse(cells[1].Value), 
      C = int.Parse(cells[2].Value) 
     }); 

을하는 데 도움이
관련 문제