2015-06-17 64 views
0

我想讀取一個特定的數據行到一個數組和另一行(行A)到它自己的數組中。我知道如何讓我的Excel文件閱讀,但我不知道如何獲得一個特定的行到一個數組....請幫我理解如何宣佈兩個動態並行陣列!如何創建從Excel文件讀取的動態並行數組? c#

//Checks if the ID Number Matches 
      string value = EmployeeIDTextbox.Text; 
      Match match = Regex.Match(value, @".*[0-9].*"); 
      Console.WriteLine(value); 
      int TotalRows = xlsDs.Rows.Count; 
      for (int i = 0; i < TotalRows; i++) 
      { 
       DataRow row = xlsDs.Rows[i]; 

       //Cell Name that contains ID Number 
       String row_Val = row["Enter Employee ID number"].ToString(); 

       Match myMatch = Regex.Match(row_Val, EmployeeIDTextbox.Text); 

       //If ID Number is found 
       if (match.Success && myMatch.Success) 
       { 
        Console.WriteLine(EmployeeIDTextbox); 

        //Place Employee information in array 

        //Places Coloumn headers in array 
+0

使用2列出了與轉換數組後(如有必要) – PaulF

+0

@PaulF我將如何聲明列表? – Cscience18

+0

取決於數據類型,但列表 list1 =新列表();然後l1.Add(...);添加到列表&最後l1.ToArray()如果​​你需要轉換爲數組。 – PaulF

回答

0

您已經可以訪問行值作爲數組是這樣的:

var rowValues = row.ItemArray; 

列標題可以被轉換成一個數組是這樣的:

using System.Linq; 
using System.Data; 
.... 
var columnHeaders = table.Columns 
    .Cast<DataColumn>() 
    .Select(x => x.ColumnName) 
    .ToArray(); 
+0

當我嘗試輸出數組Console.WriteLine(rowValues);控制檯只顯示System.Object [] – Cscience18

+0

控制檯不支持直接輸出數組(除了剛剛觀察的方式)。您需要手動循環每個元素,或者使用類似String.Join方法的方式將數組轉換爲字符串。 – RogerN