2014-04-18 49 views
0

我對某事有點困惑。我編寫了一個代碼,它將計算ListBox中的項目數,然後將它們寫入Excel文件的每個單元格中。就像這樣:C#獲取單獨的列表框項目作爲字符串

int test = ItemsList.Items.Count; 

for (int i = 1; i < test; i++) 
{ 
    foreach (string itemText in ItemsList.Items) 
    { 
     worksheet.Cells[i, 0] = new Cell(itemText); 
    } 
} 

for (int i = test + 1; i < 100; i++) 
{ 
    worksheet.Cells[i, 0] = new Cell(""); 
} 

此代碼寫入到Excel正常不過,而不是在列表框中顯示的每個項目的單獨只顯示在所有單元的最後一個項目。關於如何從列表中獲取每個項目的任何想法作爲每個單元格的單獨字符串?

回答

0

你並不需要使用2個循環。做類似:

for (int i = 1; i <= test; i++) 
    { 
     worksheet.Cells[i, 0] = new Cell(ItemsList.Items[i-1]); 
    } 
+0

謝謝!我很感激 – LearningToCodeGuy

+0

不要忘記將評論標記爲答案,如果你受益於它。 :) –

相關問題