关于 Excel Services UDF 的常见问题解答
上次修改时间: 2010年1月21日
适用范围: SharePoint Server 2010
本文内容
创建托管代码 UDF
数据类型
XLL
以下是一些有关 Excel Services 用户定义函数 (UDF) 的常见问题解答。
创建托管代码 UDF
什么是受支持的 UDF 类?
UDF 程序集中的 UDF 类必须是公共的。不能将其密封。它不能是抽象、内部或专用的。必须具有无参数的公共构造函数。对于可自动生成无参数公共构造函数的语言(例如 C#),您可以不使用任何构造函数。
什么是受支持的 UDF 方法?
UDF 程序集中的 UDF 方法必须是公共的。UDF 方法必须是线程安全的。
UDF 方法不能具有:
ref 或 out 形参
retval 属性
Optional 实参
不受支持的数据类型
UDF 方法还必须具有受支持的返回类型。若要获得受支持数据类型的列表,请参阅本主题中的"数据类型"部分。
可以从 UDF 程序集调用 Web 服务吗?
可以。有关示例,请参见以下 UDF 示例代码。另请参阅如何:创建调用 Web 服务的 UDF。
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Excel.Server.Udf;
using UdfWS.dk.iter.webservices;
namespace UdfWS
{
[UdfClass]
public class MyUdfClass
{
// Instantiate the Web service. The Web service used is at
// http://webservices.iter.dk/calculator.asmx
Calculator calc = new Calculator();
[UdfMethod]
public int MyFunction()
{
int i;
i = (i + 88) * 2;
return i;
}
[UdfMethod(IsVolatile = true)]
public double MyDouble(double d)
{
return d * 9;
}
[UdfMethod]
public int AddMe(int a, int b)
{
int c;
// Call the Web service Add method
c = calc.Add(a, b);
return c;
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Office.Excel.Server.Udf
Imports UdfWS.dk.iter.webservices
Namespace UdfWS
<UdfClass> _
Public Class MyUdfClass
' Instantiate the Web service. The Web service used is at
' http://webservices.iter.dk/calculator.asmx
Private calc As New Calculator()
<UdfMethod> _
Public Function MyFunction() As Integer
Dim i As Integer
i = (i + 88) * 2
Return i
End Function
<UdfMethod(IsVolatile := True)> _
Public Function MyDouble(ByVal d As Double) As Double
Return d * 9
End Function
<UdfMethod> _
Public Function AddMe(ByVal a As Integer, ByVal b As Integer) As Integer
Dim c As Integer
' Call the Web service Add method
c = calc.Add(a, b)
Return c
End Function
End Class
End Namespace
数据类型
哪些数据类型可以用作 UDF 参数?
所支持的数据类型如下所示:
数值类型:Double、Single、Int32、UInt32、Int16、UInt16、Byte、Sbyte
字符串
布尔值
对象数组:一维或二维数组,即对象 [] 和对象 [,]
DateTime
有哪些受支持的返回值类型?
所支持的返回值类型如下所示:
数值类型:Double、Single、Int32、UInt32、Int16、UInt16、Byte、Sbyte
字符串
布尔值
对象数组:一或二维数组,即,对象 []、对象 [,]、int[] 和 int[,])
DateTime
对象
XLL
支持 XLL 吗?
不直接支持。Excel Services 只会加载并调用托管代码 UDF。但您可以编写托管代码包装以调用 XLL,并将 XLL 与托管代码包装程序集一起部署到服务器。