2016-09-06 23 views
0

我有一個使用Excel Interop從Excel電子表格中提取的2D Object Array將Excel Interop值傳遞給Deedle DataFrame

data = activeWorksheet.UsedRange.Value2; 

data.GetType() 
{Name = "Object[,]" FullName = "System.Object[,]"} 

Looking at the Deedle documentation on Frames,看來我可以出一個二維數組FromArray2D(array),或者FromRecords(values)「一中的任意.NET對象序列」的創建一個框架,但它似乎並沒有能夠利用這個對象數組。

我也嘗試鑄造到一個數組,但只給了我一個com對象的數組,我可以參考Value2。所以這真的沒有幫助。

cellArray = activeWorksheet.UsedRange.Cells.Cast<Excel.Range>().ToArray<Excel.Range>(); 

cellArray.GetType() 
{Name = "Range[]" FullName = "Microsoft.Office.Interop.Excel.Range[]"} 

我想我可以寫一個函數來重建我的數據,based on the Github examples,但在此之前我做....

是否有一個類型轉換/施放我應該看什麼,使一個簡單的Excel - > Deedle DataFrame插入?


編輯:

當嘗試使用FromArray2D,我得到了下面。

Frame df = Frame.FromArray2D(data); 

'Frame.FromArray2D(data)' threw an exception of type 'System.IndexOutOfRangeException' 
    Data: {System.Collections.ListDictionaryInternal} 
    HResult: -2146233080 
    HelpLink: null 
    InnerException: null 
    Message: "Index was outside the bounds of the array." 
    Source: "Deedle" 
    StackTrace: " at Deedle.Frame.FromArray2D[T](T[,] array) in C:\\code\\deedle\\src\\Deedle\\FrameExtensions.fs:line 281\r\n at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)" 
    TargetSite: {Deedle.Frame`2[System.Int32,System.Int32] FromArray2D[T](T[,])} 

這可能是因爲Excel Interop使用1索引數組而不是0索引,因此存在不匹配?

我無法在官方文檔中找到這個Excel怪癖,但是SO上充滿了答案。例如:https://stackoverflow.com/a/14345372/1886901

+1

當您嘗試使用'FromArray2D'時會出現什麼錯誤? –

+0

更新我的問題以包含'FromArray2D'的錯誤 – getglad

回答

0

不確定這是否是最好的方法,但將數組移動到0索引允許FromArray2D通過時沒有錯誤。

data = activeWorksheet.UsedRange.Value2;  
string[,] newData = new string[data.GetLength(0), data.GetLength(1)]; 
for (int x = 0; x < data.GetLength(0); x++) 
{ 
    for (int y = 0; y < data.GetLength(1); y++) 
    { 
    newData[x, y] = data[x + 1, y + 1].ToString(); 
    } 
} 
var df = Frame.FromArray2D(newData);