2017-08-07 48 views
0

而不是從我的foreach循環寫入每個元素,我的語法只寫入每個單元格的foreach循環的最後一個值。我確信這是我忽略的東西,但是我的語法需要改變什麼,以便它將在每行上相應地編寫foreach循環的每個元素?代碼只是反覆寫入最後一筆數據

foreach (var calcs in dictionary) 
{ 
    var FileInfo = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "WriteDataToExcelThroughCode.xlsx")); 
    using (ExcelPackage package = new ExcelPackage(FileInfo)) 
    { 
     ExcelWorksheet ws = package.Workbook.Worksheets["TTest2"]; 
     //ExcelWorksheet ws = package.Workbook.Worksheets[0]; 

     ws.Cells["A24:C36"].Clear(); 

     int z = 0; 
     int x = 24; 
     while (z <= numofrows) 
     { 
      ws.Cells["A" + x + ":A" + x].Value = calcs.Key; 
      ws.Cells["B" + x + ":B" + x].Value = dictionary[calcs.Key][0]; 
      ws.Cells["C" + x + ":C" + x].Value = dictionary[calcs.Key][1]; 
      //ws.Cells["D" + x + "D" + x].Value = dictionary[calcs.Key][2]; 
      x = x + 1; 
      z = z + 1; 
     } 
     package.Save(); 
    } 
} 

編輯
爲了更好地說明什麼我預期的成果是,如下所示。 假想Calcs(計算)包含以下信息

James 1 2 
Jose 3 4 
Bob 5 6 

我想下列信息將被寫入到下面的細胞
A24 =詹姆斯B24 = 1 C24 = 2
A25 =聖何塞B25 = 3 C25 = 4
A26 =鮑勃B26 = 5 C26 = 6

編輯2
我的變量numofrows被建立像這樣

int start = Convert.ToInt32(txtstart.Text); 
int end = Convert.ToInt32(txtEnd.Text) + 1; 
int numofrows = end - start; 

int[] abc = Enumerable.Range(start, end-start).ToArray(); 

foreach (int a in abc) 
{ 
    Dictionary<int, int[]> dictionary = new Dictionary<int, int[]>(); 
    dictionary.Add(a, new int[] { 2, 3 }); 
    foreach (var calcs in dictionary) 
    { 
     // original code 
    } 
} 



    private void btnHitIT_Click(object sender, EventArgs e) 
{ 
    int start = Convert.ToInt32(Alpha); 
    int end = Convert.ToInt32(AlphaEnd)+1; 
    int numofrows = end - start; 
    int[] angles = Enumerable.Range(start, end - start).ToArray(); 
    foreach (int a in angles) 
    { 
     Dictionary<int, int[]> dictionary = new Dictionary<int, int[]>(); 
     dictionary.Add("Jack", new int[] { 2, 3 }); 
     dictionary.Add("James", new int[] { 7, 11 }); 
     dictionary.Add("Jason", new int[] { 14, 21 }); 
     pDrawingArea.Invalidate(); 
     var FileInfo = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "WriteDataToExcelThroughCode.xlsx")); 
     using (ExcelPackage package = new ExcelPackage(FileInfo)) 
     { 
      ExcelWorksheet ws = package.Workbook.Worksheets["TTest2"]; 
      int x = 24; 
      foreach (var calcs in dictionary) 
      { 
       ws.Cells["A24:C36"].Clear(); 
       for (int z = 0; z <= numofrows; ++z) 
       { 
        ws.Cells["A" + x + ":A" + x].Value = calcs.Key; 
        ws.Cells["B" + x + ":B" + x].Value = dictionary[calcs.Key][0]; 
        ws.Cells["C" + x + ":C" + x].Value = dictionary[calcs.Key][1]; 
        ++x; 
       } 
      } 
      package.Save(); 
     } 
    } 

回答

1

您正在定義和增加變量x在錯誤的地方。您需要在foreach循環之外定義它,並將其增加到while循環之外(但在foreach循環中)。

我已將while循環更改爲更合適的for循環。

還將WorkSheet定義移到foreach循環之外,因爲沒有理由不斷地打開和關閉excel文件。

我注意到你在Dictionary的值兩次搜索因爲你已經在calcs.Value

var FileInfo = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "WriteDataToExcelThroughCode.xlsx")); 
using (ExcelPackage package = new ExcelPackage(FileInfo)) 
{ 
    ExcelWorksheet ws = package.Workbook.Worksheets["TTest2"]; 
    //ExcelWorksheet ws = package.Workbook.Worksheets[0]; 
    int x = 24; 
    foreach (var calcs in dictionary) 
    { 
     ws.Cells["A24:C36"].Clear(); 

     for (int z = 0; z <= numofrows; ++z) 
     { 
      ws.Cells["A" + x + ":A" + x].Value = calcs.Key; 
      ws.Cells["B" + x + ":B" + x].Value = calcs.Value[0]; 
      ws.Cells["C" + x + ":C" + x].Value = calcs.Value[1]; 
      //ws.Cells["D" + x + "D" + x].Value = calcs.Value[2]; 
      ++x; 
     } 
     package.Save(); 
    } 
} 
+0

該值是+ X;和++ z;與我一樣設置z = z + 1? –

+1

@YohanGreenburg是的。有多種寫法('++ x','x + = 1','x = x + 1')。和'++ x'是通過'1'增加的最清晰的方式。 – Vikhram

+0

利用您的文章中的語法,只有最後一組值被寫入Excel –