使用 C# 创建新的 Excel 函数(用户定义函数) [英] Create new Excel Function (User Defined Function) using C#
问题描述
我想创建一个新的 Excel 函数(用户定义的函数),为此我做了与此链接相同的步骤:https://excel-dna.net/
I would like to create a new Excel function (user Defined Function), for that i did the same steps as in this link: https://excel-dna.net/
我创建了一个类库项目
I created a class Library Project
我安装了 ExcelDna.Integration 包
I installed the package ExcelDna.Integration
我创建了一个方法 sayHello
I created a method sayHello
但是当我尝试从 excel 中的单元格调用我的函数时(我输入 =sayHello("World"),该函数没有出现,似乎它没有添加到 excel 函数中.是否缺少某些步骤?我怎样才能让我的函数看起来可以使用它?
But when i try to call my function from a cell in excel (i put =sayHello("World"), the function didn't appear, it seems it's not added to excel functions. is there some missing steps? how can i make my function appear to be able to use it ?
推荐答案
您应该安装包 "ExcelDna.AddIn" 来制作加载项(这将设置加载项,包括重要的 .dna 文件,并引入 ExcelDna.Integration" 参考库).
You should install the package "ExcelDna.AddIn" to make the add-in (that will set up the add-in including the important .dna file, and also bring in the "ExcelDna.Integration" reference library).
所以步骤是:
- 创建一个新的 C# 类库项目(针对 .NET Framework 而不是 .NET Standard)
- 安装
ExcelDna.AddIn
包 - 添加一些代码,例如
<代码>公共静态类 MyFunctions{public static string SayHello(string name) { return "Hello " + name;}}
- 按 F5 在 Excel 中编译和加载加载项
(请注意,在某些 Excel 安装中,项目的 Debug 设置在可执行文件路径中获得一个额外的 %1 - 如果在调试时出现错误,只需将其从末尾删除)
(Note that on some Excel installations, the Debug setting for the project get an extra %1 in the executable path - just remove this from the end if you get an error when debugging)
对 Excel-DNA 的最佳支持是位于 https://groups 的 Google 小组.google.com/forum/#!forum/exceldna
The best support for Excel-DNA is the Google group at https://groups.google.com/forum/#!forum/exceldna
这篇关于使用 C# 创建新的 Excel 函数(用户定义函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!