步骤 2:创建托管代码 UDF

在项目中添加对 Microsoft.Office.Excel.Server.Udf.dll 的引用后,下一步是创建一些自定义函数,并使用 Excel Services 用户定义函数 (UDF) 属性进行标记。

您必须使用 Microsoft.Office.Excel.Server.Udf.UdfClass 属性标记 UDF 类,使用 Microsoft.Office.Excel.Server.Udf.UdfMethod 属性标记 UDF 方法。

UDF 程序集中任何未使用 Microsoft.Office.Excel.Server.Udf.UdfMethod 属性进行标记的方法将被忽略,因为它们被视为 UDF 方法。

Microsoft.Office.Excel.Server.Udf.UdfMethod 属性具有 IsVolatile 特性。 您可使用 IsVolatile 特性将 UDF 方法指定为易失性和或非易失性。 IsVolatile 特性使用布尔值。 默认值为 false,这表示特定 UDF 方法为非易失性。

创建 UDF

添加指令

  • 要使用的类型在 Microsoft.Office.Excel.Server.Udf 命名空间中定义。 在 Class1.cs 文件开头添加 using(或 Imports)指令,即可使用 Microsoft.Office.Excel.Server.Udf 中的类型,而无需完全符合命名空间中的类型。

    若要添加此指令,请将以下代码添加到 Class1.cs 文件中代码的开头,然后 using System.Text:

    using Microsoft.Office.Excel.Server.Udf;
    
    Imports Microsoft.Office.Excel.Server.Udf
    

标记 UDF 类和方法

  1. 通过将以下属性添加到正上方public class Class1,标记为 Class1 UDF 类:

    [UdfClass]
    
    <UdfClass>_
    
  2. 创建一个值为数字的函数(类型为 double),然后在函数中将数字乘以 9。 该函数是一个非易失性 UDF 方法。 将以下代码添加到 Class1

    [UdfMethod]
    public double MyDouble(double d)
    {
        return d * 9;
    }
    
    <UdfMethod> _
    Public Function MyDouble(ByVal d As Double) As Double
        Return d * 9
    End Function
    

    注意

    IsVolatile 属性的默认值为 false,这表示特定 UDF 方法为非易失性。 因此,将非易失性 UDF 方法 [UdfMethod]标记为 就足够了。 无需将其标记为 [UdfMethod(IsVolatile = false)]

  3. 使用 System.DateTime.Today 特性创建另一个返回最新状态的函数。 函数为易失性 UDF 方法。 将以下代码添加到 Class1

    [UdfMethod(IsVolatile = true)]
    public DateTime ReturnDateTimeToday()
    {
        return (DateTime.Today);
    }
    
    <UdfMethod(IsVolatile := True)> _
    Public Function ReturnDateTimeToday() As Date
        Return (Date.Today)
    End Function
    

构建项目

  1. 在"构建"菜单上,单击"构建解决方案"。
  2. 您应在您保存项目的目录中查找 SampleUdf.dll 程序集。

完整代码

以下代码示例是上述步骤中所述 Class1.cs 示例文件中的完整代码。

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Excel.Server.Udf;

namespace SampleUdf
{
    [UdfClass]
    public class Class1
    {
        [UdfMethod]
        public double MyDouble(double d)
        {
            return d * 9;
        }

        [UdfMethod(IsVolatile = true)]
        public DateTime ReturnDateTimeToday()
        {
            return (DateTime.Today);
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Office.Excel.Server.Udf

Namespace SampleUdf
    <UdfClass> _
    Public Class Class1
        <UdfMethod> _
        Public Function MyDouble(ByVal d As Double) As Double
            Return d * 9
        End Function

        <UdfMethod(IsVolatile := True)> _
        Public Function ReturnDateTimeToday() As Date
            Return (Date.Today)
        End Function
    End Class
End Namespace

另请参阅

任务

概念