2016-06-30 1 views
-1

2つの入力データ(2月、3月など)を挿入しましたが、最後の入力は3月ののデータのみです。2つの入力がある場合、私のプログラムは1つの出力しか表示しないのはなぜですか?

コードをテストした後、私は最後の入力データのみを表示していることに気付きました。代わりにすべての入力データを表示するにはどうすればよいですか?

誤った:一つだけの出力が示されている:

Incorrect: only one output is shown

正しい:すべての入力データを示すべきである: Correct: should show all the input data

int count = 0; 

foreach (DataTable dt in ds.Tables)  
{ 

    var ws = wb.Worksheets.Add(dt.TableName); 

    if (count == 0) 
    { 
     string[] columnNames = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray(); 
     string[] rowValue = new string[dt.Rows.Count]; 
     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      rowValue = dt.Rows[i].ItemArray.Select(x => x.ToString()).ToArray(); 
     } 

     DateTime dttryparse; 
     for (int i = 0; i < columnNames.Count(); i++) 
     { 
      ws.Cell(1, i + 1).SetValue(columnNames[i]); 
     } 
     for (int i = 0; i < rowValue.Count(); i++)  
     { 
      if (rowValue[i].Contains("$")) 
      { 
       ws.Cell(2, i + 1).SetValue(rowValue[i]); 
       ws.Cell(2, i + 1).Style.NumberFormat.Format = "$ #,##0.00"; 
       ws.Cell(2, i + 1).DataType = XLCellValues.Text; 
      } 
      else if (rowValue[i].Contains("%")) 
      { 
       ws.Cell(2, i + 1).SetValue(rowValue[i]); 
       ws.Cell(2, i + 1).DataType = XLCellValues.Text; 
       ws.Cell(2, i + 1).Style.NumberFormat.Format = "0.0%"; 
      } 
      else if (DateTime.TryParse(rowValue[i], out dttryparse)) 
      { 
       ws.Cell(2, i + 1).SetValue(DateTime.Parse(rowValue[i])); 
       ws.Cell(2, i + 1).DataType = XLCellValues.DateTime; 
      } 

     } 
     ws.Columns().AdjustToContents(); 
     count += 1; 
    } 
    else 
    { 
     var rowValue = dt.Rows[0].ItemArray.Select(x => x.ToString()).ToArray(); 
     double dbltryparse = 0; 
     DateTime dttryparse; 

     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      if (rowValue[i].Contains("$")) 
      { 
       ws.Cell(2, i + 1).SetValue(rowValue[i]); 
       ws.Cell(2, i + 1).Style.NumberFormat.Format = "$ #,##0.00"; 
       ws.Cell(2, i + 1).DataType = XLCellValues.Text; 
      } 
      else if (rowValue[i].Contains("%")) 
      { 
       ws.Cell(2, i + 1).SetValue(double.Parse(rowValue[i])); 
       ws.Cell(2, i + 1).DataType = XLCellValues.Text; 
       ws.Cell(2, i + 1).Style.NumberFormat.Format = "0.0%"; 
      } 
      else if (DateTime.TryParse(rowValue[i], out dttryparse)) 
      { 
       ws.Cell(2, i + 1).SetValue(DateTime.Parse(rowValue[i])); 
       ws.Cell(2, i + 1).DataType = XLCellValues.DateTime; 
      } 
     } 
     ws.Columns().AdjustToContents(); 
    } 
} 
+0

ネストループを使用する必要があります –

答えて

3

この

for (int i = 0; i < dt.Rows.Count; i++) 
{ 
    rowValue = dt.Rows[i].ItemArray.Select(x => x.ToString()).ToArray(); 
} 

はすべてを歩きます行、exループを終了したときに最後の行(音色が分かりますか?)に来るまで、値をトラップし、次の行の値で上書きします。最後の行の値を含むrowValueが残ります。

これをデバッガでステップ実行すると、何が起きているのかがはっきり分かります。

関連する問題