|
开发环境 VS2017+OFFice2010
系统 WIN 7 SP1
开发语言VB.net
采用类库开发Excel函数,采用接口的方式,与其他的开发模式不同,这种方法可以屏蔽某些不必要的函数。
注意事项:勾选com互操作。
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports Microsoft.Win32
Imports ExcelFunction
<Guid("68559C01-F472-44A6-AB60-F7DBD7791488")>
Public Interface IFunctions
'定义函数名 有多少个函数就定义多少个接口
Function Add(x As Double, y As Double) As Double
End Interface
<Guid("512121D6-F6C4-4570-AEA9-9C3D98F9D3A4"),
ProgId("ExcelFunction.UDF"),
'更改类名
ClassInterface(ClassInterfaceType.AutoDual),
ComDefaultInterface(GetType(IFunctions))>
Public Class UDF
'导入接口函数
Implements IFunctions
<ComRegisterFunction>
Public Shared Sub RegisterFunction(type As Type)
Dim PATH As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace("\", "/")
Dim ASSM As String = Assembly.GetExecutingAssembly().FullName
Dim startPos As Integer = ASSM.ToLower().IndexOf("version=") + "version=".Length
Dim len As Integer = ASSM.ToLower().IndexOf(",", startPos) - startPos
Dim VER As String = ASSM.Substring(startPos, len)
Dim GUID As String = "{" + type.GUID.ToString().ToUpper() + "}"
Dim NAME As String = type.[Namespace] + "." + type.Name
Dim BASE As String = Convert.ToString("Classes\") & NAME
Dim CLSID As String = Convert.ToString("Classes\CLSID\") & GUID
Dim CU As RegistryKey = Registry.CurrentUser.OpenSubKey("Software", True)
Dim key As RegistryKey = CU.OpenSubKey(Convert.ToString(CLSID & Convert.ToString("\InprocServer32\")) & VER)
If key Is Nothing Then
key = CU.CreateSubKey(BASE)
key.SetValue("", NAME)
key = CU.CreateSubKey(BASE & Convert.ToString("\CLSID"))
key.SetValue("", GUID)
key = CU.CreateSubKey(CLSID)
key.SetValue("", NAME)
key = CU.CreateSubKey(CLSID & Convert.ToString("\Implemented Categories")).CreateSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}")
key = CU.CreateSubKey(CLSID & Convert.ToString("\InprocServer32"))
key.SetValue("", Environment.SystemDirectory + "\mscoree.dll")
key.SetValue("ThreadingModel", "Both")
key.SetValue("Class", NAME)
key.SetValue("CodeBase", PATH)
key.SetValue("Assembly", ASSM)
key.SetValue("RuntimeVersion", "v4.0.30319")
key = CU.CreateSubKey(Convert.ToString(CLSID & Convert.ToString("\InprocServer32\")) & VER)
key.SetValue("Class", NAME)
key.SetValue("CodeBase", PATH)
key.SetValue("Assembly", ASSM)
key.SetValue("RuntimeVersion", "v4.0.30319")
key = CU.CreateSubKey(CLSID & Convert.ToString("\ProgId"))
key.SetValue("", NAME)
key = CU.CreateSubKey(CLSID & Convert.ToString("\Programmable"))
For Each keyName As String In Registry.CurrentUser.OpenSubKey("Software\Microsoft\Office\").GetSubKeyNames()
If IsVersionNum(keyName) Then
key = Registry.CurrentUser.OpenSubKey((Convert.ToString("Software\Microsoft\Office\") & keyName) + "\Excel\Add-in Manager", True)
If key IsNot Nothing Then
key.SetValue(NAME, "")
End If
End If
Next
End If
End Sub
<ComUnregisterFunctionAttribute>
Public Shared Sub UnregisterFunction(type As Type)
Dim GUID As String = "{" + type.GUID.ToString().ToUpper() + "}"
Dim NAME As String = type.[Namespace] + "." + type.Name
Dim BASE As String = Convert.ToString("Classes\") & NAME
Dim CLSID As String = Convert.ToString("Classes\CLSID\") & GUID
Dim CU As RegistryKey = Registry.CurrentUser.OpenSubKey("Software", True)
Try
CU.DeleteSubKeyTree(BASE)
Catch
End Try
Try
CU.DeleteSubKeyTree(CLSID)
Catch
End Try
For Each keyName As String In Registry.CurrentUser.OpenSubKey("Software\Microsoft\Office\").GetSubKeyNames()
If IsVersionNum(keyName) Then
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey((Convert.ToString("Software\Microsoft\Office\") & keyName) + "\Excel\Add-in Manager", True)
If key IsNot Nothing Then
Try
key.DeleteValue(NAME)
Catch
End Try
End If
key = Registry.CurrentUser.OpenSubKey((Convert.ToString("Software\Microsoft\Office\") & keyName) + "\Excel\Options", True)
If key Is Nothing Then
Continue For
End If
For Each valueName As String In key.GetValueNames()
If valueName.StartsWith("OPEN") Then
If key.GetValue(valueName).ToString().Contains(NAME) Then
Try
key.DeleteValue(valueName)
Catch
End Try
End If
End If
Next
End If
Next
End Sub
Public Shared Function IsVersionNum(s As String) As Boolean
Dim idx As Integer = s.IndexOf(".")
If idx >= 0 AndAlso s.EndsWith("0") AndAlso Integer.Parse(s.Substring(0, idx)) > 0 Then
Return True
Else
Return False
End If
End Function
'写接口函数
Public Function Add(x As Double, y As Double) As Double Implements IFunctions.Add
Return x + y
End Function
End Class
|
评分
-
1
查看全部评分
-
|