2016-09-06 27 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);