这篇别处抄不到,只有自己怼。
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.NETFramework64v4.0.30319RegAsm.exe "%addin%" /CodeBase %SystemRoot%Microsoft.NETFramework64v4.0.30319RegAsm.exe "%function%" /CodeBase :: add Registry keys to "HKEY_CURRENT_USERSoftwareMicrosoftOfficeExcelAddins" echo Windows Registry Editor Version 5.00 >add.reg echo. echo [HKEY_CURRENT_USERSoftwareMicrosoftOfficeExcelAddinsCom.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
学习英文可以看
自定义函数UDF可以参考:
https://www.codeproject.com/Articles/552375/ExcelplusUDFpluslibraryplusinplusC-23plusfrompluss
https://bettersolutions.com/csharp/excel-interop/csharp-excel-functions-automation-add-ins.htm