2010-12-07 39 views
2

我有一個返回二維數組數據的C#web服務。由於我們不能讓webservices返回多維數據,所以我讓它返回一個鋸齒狀的數組。從C#webservice返回多維數組數據到vba

[OperationContract] 
object[][] WSGetData(); 

我有一個COM Visible C#類庫。這是一個使用此服務的薄層,並將其提供給Excel VBA客戶端。 (對於某些原因,我們選擇了不通過VSTO去,或Web Services參考工具包的路由。)

class Wrapper 
{ 
    public object[][] GetData() 
    { 
     return WSproxy.WSGetData(); //Calling the webservice method 
    } 
} 

我調用該方法在VBA如下。

Dim data as Variant 
data = wrapperObj.GetData(); 

我得到類型不匹配錯誤。

在返回到VBA之前,當我更改Wrapper類以將Web服務的「鋸齒陣列」輸出轉換爲多維輸出(即。object [,])時,它工作正常。但我不想這樣做,因爲它會影響性能,因爲我們會傳遞大量數據。

請實現此目的的最佳方法是什麼? 感謝您的任何指示..

回答

1

鐵血陣列是可能的。我不認爲VBA喜歡你宣稱你的變體的方式。您可能需要將其聲明爲Variant數組。看下面的例子:

Sub Test() 
    Dim oneToTen(9) As String 'Array 1 
    Dim tenTo21(10) As String 'Array 2 
    Dim twentyTwoTo23(1) As String 'Array 3 
    Dim vArray() As Variant 'Jagged Array (array of arrays) 

    'Fill test data in the three arrays 
    Dim iCount As Integer 
    For iCount = 0 To 9 
     oneToTen(iCount) = iCount + 1 
    Next iCount 
    For iCount = 0 To 10 
     tenTo21(iCount) = iCount + 11 
    Next iCount 
    For iCount = 0 To 1 
     twentyTwoTo23(iCount) = iCount + 22 
    Next iCount 

    'If you uncomment the code below, you will get a type mismatch (probably for the same reason you get it in your webservice) 
    'vArray1(0) = oneToTen 

    'However, if you REDIM the variant array, you can then set each array into the variant 
    Const JAGGED_ARRAY_SIZE = 2 'This will probably require another property on your webservice to see how big your Jagged Array is (e.g. wrapperObj.GetJaggedArraySize()) 
    ReDim vArray(JAGGED_ARRAY_SIZE) 

    vArray(0) = oneToTen 
    vArray(1) = tenTo21 
    vArray(2) = twentyTwoTo23 

    'Now loop through the jagged array: 
    Dim outerLoop As Integer 
    Dim innerLoop As Integer 
    Dim vCurrentArray As Variant 

    'Loop through the arrays in the array and print out the data 
    For outerLoop = 0 To JAGGED_ARRAY_SIZE 
     For innerLoop = 0 To UBound(vArray(outerLoop)) 
      Debug.Print "Outer Loop: " & outerLoop & " Inner Loop: " & innerLoop & " Array Value: " & vArray(outerLoop)(innerLoop) 
     Next innerLoop 
    Next outerLoop 

End Sub