Creating new Excel formulas/functions with C#

前提是你 提交于 2019-12-03 15:23:33

You're looking for Excel-DNA. This open-source library allows you to create managed Excel add-ins, and supports making user-defined functions, but also macros, real-time RTD data sources etc.

Creating an Excel UDF in C# is then as simple as:

[ExcelFunction(Description = "My first .NET function")]
public static string SayHello(string name)
{
    return "Hello " + name;
}

and you can call from a cell as:

=SayHello("Walter")

For code protection with .NET, you'd need to use an obfuscator - there are a variety of free and paid-for ones available.

I have also tried this sample, with the same error. I found a solution that worked for me.

In the ISheet1.cs file, replace the ISheet1 interface declaration with the following code. This code makes the ISheet1 interface public, and it applies the http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comvisibleattribute.aspx attribute to make the interface visible to COM.

C#

[System.Runtime.InteropServices.ComVisible(true)]
public interface ISheet1
{
    void CreateVstoNamedRange(Microsoft.Office.Interop.Excel.Range range, string name);
}

Full article her: http://www.nullskull.com/q/10059408/c-code-to-set-excel-workbook-macro-enabled-and-to-trust-vba-projects.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!