gpt4 book ai didi

c# - 将变体数组从 Excel VBA 传递到 WCF 函数

转载 作者:行者123 更新时间:2023-11-30 17:03:38 27 4
gpt4 key购买 nike

我正在拼命地尝试这样做。

我已经能够复制在这篇文章中发现的行为。

http://damianblog.com/2009/07/05/excel-wcf/comment-page-1/#comment-64232

但是,我无法将数组传递给公开的 wcf 函数。

我的 WCF 服务是这样工作的(我也尝试过使用 int 数组)

public object[] GetSomeArray()
{
return new object[] { 1, 2, 3, 4};
}

public object[] ReturnSomeArray(object someArray)
{
object[] temp = (object[]) someArray;
for (int i = 0; i < temp.Length; i++)
{
temp[i] = (int)temp[i] + 1;
}

return temp;
}

我的 VBA 代码如下所示。

Dim addr As String
...


Dim service1 As Object
Set service1 = GetObject(addr)

Dim columnsVar
columnsVar = Array(1, 2, 3)


Dim anotherArray As Variant
anotherArray = service1.ReturnSomeArray(columnsVar)

我总是在上面的最后一行遇到问题。我不明白为什么如果我能够从我的 WCF 服务返回一个数组,我却不能将该数组作为参数传递给另一个 WCF 函数。
current error message

我收到序列化错误。

如有任何帮助,我们将不胜感激。

最佳答案

只有当我以这种方式在 VBA 中声明数组变量时,我才会遇到与 Type mismatch 错误类似的问题:

Dim anotherArray() As Variant

但是如果这样定义变量,错误就消失了:

Dim anotherArray As Variant

你和我的类似解决方案之间的一些其他差异是:

//C#- my solution- without array[] definition:
public object[] ReturnSomeArray(object someArray)

//VBA- my solution -without array() definition:
Dim someArray As Variant

编辑:2013-08-28

使用 C#-Excel-Interop 我更喜欢尝试和测试搜索解决方案的方式。如果有任何效果,那么我会坚持下去,有时我会错过指出解决方案或逻辑的来源。

您将在下面找到包含 LINQ 的代码,以便对数组进行操作。这些代码片段双向工作——从 C# 获取数据到 VBA >> 将其传回 C# 进行排序 >> 返回到 VBA。我希望它能帮助你更多地最终解决你的问题。

首先:一些 C# 代码

    public object[] GetSomeArray()
{
return new object[] { 5, 2, 1, 7, 9, 1, 5, 7 };
}

public double[] ArraySorted(object tablica)
{
object[] obj = (object[])tablica;
var filtr = from i in obj
orderby Convert.ToDouble(i)
select Convert.ToDouble(i);

double[] wynik = (double[])filtr.ToArray();
return wynik;
}

其次:一些VBA代码

Sub qTest_second_attempt()

'declare array variable
Dim tmp()
'and other variables
Dim addr As String
addr = "UDFArrayLinqTest.ArrayLinq"
'get references
Dim service1 As Object
Set service1 = CreateObject(addr)

'get array from C#
tmp = service1.GetSomeArray()

'pass this array to C# back to sort it
Dim someArray As Variant
someArray = service1.ArraySorted(tmp)

'check the result in Immediate window
Debug.Print Join(WorksheetFunction.Transpose(WorksheetFunction.Transpose(someArray)))
'result: 1 1 2 5 5 7 7 9

End Sub

关于c# - 将变体数组从 Excel VBA 传递到 WCF 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18365198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com