locked
Use a function created in C# in Excel (call C# function in Excel) RRS feed

  • Question

  • I already have the program written, however, im having trouble making the connection from c# to excel.

    Is a very simple code so that i can understand how the connection between the two programs works and the move on to more complex codes.

        public double sumtres(double x, double y, double z)
            {
                return x + y + z;
            }

    After creating the connection I will like to call this function directly through excel. 

    Example:

    sumtres(A1,B1,C1) -------------> and then I get the result.

    Any suggestions?

    Thanks in advanced


    Thursday, August 8, 2013 9:44 AM

Answers

All replies

  • You're going to want to compile that C# into a library that you include in a VBA module (an Excel UDF that calls the C# library function), or perhaps an addin.

    See:

    http://www.codeproject.com/Articles/555660/Extend-your-VBA-code-with-Csharp-VB-NET-or-Cpluspl


    -jerrykern

    Thursday, August 8, 2013 3:29 PM
  • Hi,

    It seems like you're looking for Excel-DNA , which allows you to make Excel add-ins with User-Defined Functions using .NET. 

    Excel-DNA is an independent, open-source project to integrate .NET with Excel, using the native .xll interface. It works with all .NET and Excel versions, and you don't have to fiddle with VBA in the middle to make your UDFs work.

    More information, documentation and further links are at the Excel-DNA CodePlex site.

    If you're using Visual Studio 2010 or 2012, you can add Excel-DNA to a class library project via NuGet, with "Install-Package Excel-DNA". Then you just have to add your function (it needs to be static, too) and press F5 to run.

    -Govert

    Excel-DNA - Free and easy .NET for Excel

    Thursday, August 15, 2013 8:21 PM
  • I also recommend trying Excel DNA.  I have used it since 2012 for a variety of projects and when combined with Net Office for portability across O2000-2016, it can be a powerful SDK for your analytical needs within Excel.  For more information on developing Excel Addins using Excel DNA, check out my series of tutorials @ Excel Addin Framework Criteria.

    Cheers,

    BT

    Monday, December 19, 2016 1:06 PM