2014-03-31 21 views
6

Excel範圍,以陣列我想用C#Excel範圍轉換成數組轉換爲使用C#

System.Array MyRange = (System.Array)range.cells.value; 
    for (int k = 0; k <= MyRange.Length; k++) 
    { 
     List<service_name> _ml = new List<service_name>(); 
     for (int j = 1; j < dataitems.Count; j++) 
     { 
      // enter code here 
     } 
    } 

如何在循環 迭代但是這個代碼不工作,並調用錯誤:

"Unable to cast object of type 'System.String' to type 'System.Array'."

+1

至少應該像'Range.Cells.Value'這樣大寫。但是沒有足夠的有關您的問題的信息。請澄清一下。 –

+0

現在我更新了代碼。 – user3217843

+0

可以請你提供我的鏈接。 – user3217843

回答

1

的錯誤意味着該值是一個字符串,因此您無法直接將其轉換爲數組。

如果該值爲例如逗號delimeted字符串你可以使用分割得到一個數組:

string[] MyRange = (range.Cells.Value + "").Split(','); 
for (int k = 0; k < MyRange.Length; k++) 
{ 
    //...loop here... 
} 

而且固定你的循環,你會得到索引越界的錯誤。

6

根據我提供的Microsoft here提供的幫助,這是我如何在Excel中讀寫數組。

var xlApp=new Microsoft.Office.Interop.Excel.Application(); 
var wb=xlApp.Workbooks.Open(fn, ReadOnly: false); 
xlApp.Visible=true; 
var ws=wb.Worksheets[1] as Worksheet; 
var r=ws.Range["A2"].Resize[100, 1]; 
var array=r.Value; 
// array is object[1..100,1..1] 
for(int i=1; i<=100; i++) 
{ 
    var text=array[i, 1] as string; 
    Debug.Print(text); 
} 
// to create an [1..100,1..1] array use 
var array2=Array.CreateInstance(
    typeof(object), 
    new int[] {100, 1}, 
    new int[] {1, 1}) as object[,]; 

// fill array2 
for(int i=1; i<=100; i++) 
{ 
    array2[i, 1] = string.Format("Text{0}",i); 
} 
r.Value2=array2; 

wb.Close(SaveChanges: true); 
xlApp.Quit(); 

Debug Excel

-1

晚的談話,但這裏是做到這一點的方法:

static string[][] GetStringArray(Object rangeValues) 
{ 
    string[][] stringArray = null; 

    Array array = rangeValues as Array; 
    if (null != array) 
    { 
     int rank = array.Rank; 
     if (rank > 1) 
     { 
      int rowCount = array.GetLength(0); 
      int columnCount = array.GetUpperBound(1); 

      stringArray = new string[rowCount][]; 

      for (int index = 0; index < rowCount; index++) 
      { 
       stringArray[index] = new string[columnCount-1]; 

       for (int index2 = 0; index2 < columnCount; index2++) 
       { 
        Object obj = array.GetValue(index + 1, index2 + 1); 
        if (null != obj) 
        { 
         string value = obj.ToString(); 

         stringArray[index][index2] = value; 
        } 
       } 
      } 
     } 
    } 

    return stringArray; 
} 

調用:

string[][] rows = GetStringArray(range.Cells.Value2); 
1

在錯誤信息當範圍恰好由一個c組成時,就會發生原始帖子因爲結果值的類型是變體,實際上可以是數組,雙精度型,字符串型,日期型和空值。

一種解決方案可以是檢查細胞數量,並在僅有一個細胞的情況下采取不同的行爲。

我的解決方案創建一個單元格數組。即使一個或多個單元格爲空,這可能會導致空對象。 (當範圍內的所有細胞都是空的,Range.Cells.Value爲空)。

System.Array cellArray = range.Cells.Cast<Excel.Range>().ToArray<Excel.Range>(); 

如果你喜歡在列表陣列(像我這樣做),你可以使用此代碼:

List<Excel.Range> listOfCells = range.Cells.Cast<Excel.Range>().ToList<Excel.Range>(); 

範圍可以是一維或二維。

最後,如果你確實需要一個字符串數組,那就是:

string[] strArray = range.Cells.Cast<Excel.Range>().Select(Selector).ToArray<string>(); 

其中選擇功能看起來是這樣的:

public string Selector(Excel.Range cell) 
{ 
    if (cell.Value2 == null) 
     return ""; 
    if (cell.Value2.GetType().ToString() == "System.Double") 
     return ((double)cell.Value2).ToString(); 
    else if (cell.Value2.GetType().ToString() == "System.String") 
     return ((string)cell.Value2); 
    else if (cell.Value2.GetType().ToString() == "System.Boolean") 
     return ((bool)cell.Value2).ToString(); 
    else 
     return "unknown"; 
} 

中,我有我不」的情況「未知」請記住Value2可以返回的所有數據類型。如果您發現任何其他類型,請改進此功能。