C# Office Com 开发 Excel插件与函数dll注册

这篇别处抄不到,只有自己怼。

1、先使用 regasm工具注册dll到注册表

2、插件dll,即com addin的dll需要写注册表的Excel插件项;函数dll,即automation-add-in只需要regasm注册后使用Excel的加载项(Automation)加载即可使用

3、使用vbs代码启用com addin 与函数Automation(addin)

下面是注册表 与 Excel启用addin代码:

 

 

bat代码:

@echo off
::echo Register for 64 bit Excel.......
set a=%~dp0
set addin=%a%Com.FToolsforExcel.dll
set function=%a%Com.FToolsforExcel.UDF.dll
set load=%a%load.vbs

:: The RegAsm tool adds the necessary class and interface information to the registry
:: %SystemRoot% means "C:\Windows"
%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe "%addin%"  /CodeBase
%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe "%function%"  /CodeBase

:: add Registry keys to "HKEY_CURRENT_USER\Software\Microsoft\Office\Excel\Addins"
echo Windows Registry Editor Version 5.00 >add.reg
echo.
echo [HKEY_CURRENT_USER\Software\Microsoft\Office\Excel\Addins\Com.FToolsforExcel.MyConnect] >>add.reg
echo "Description"="Com.FToolsforExcel.MyConnect" >>add.reg
echo "FriendlyName"="FTools for Excel ComVersion"  >>add.reg
echo "LoadBehavior"=dword:02 >>add.reg
regedit /s add.reg
del /q add.reg

:: run load vbscript
CScript  "%load%"

::echo Register successful
::pause
exit

bat调用的vbs代码:

On Error Resume Next
Dim oExcel
Set oExcel= GetObject(, "Excel.Application")
if Err.Number = 0 Then
    oExcel.COMAddIns("Com.FToolsforExcel.MyConnect").Connect = True
    oExcel.AddIns("Com.FToolsforExcel.UDF.Functions").Installed = True
else
    Set oExcel = CreateObject("Excel.Application")
    oExcel.COMAddIns("Com.FToolsforExcel.MyConnect").Connect = True
    oExcel.AddIns("Com.FToolsforExcel.UDF.Functions").Installed = True
    oExcel.Quit
end if

 

学习英文可以看

C# Office Com Addin开发第一步

自定义函数UDF可以参考:

https://www.codeproject.com/Articles/552375/ExcelplusUDFpluslibraryplusinplusC-23plusfrompluss

https://bettersolutions.com/csharp/excel-interop/csharp-excel-functions-automation-add-ins.htm

posted @ 2020-10-20 09:36  yzhyingcool  阅读(1644)  评论(0编辑  收藏  举报