I am using Excel 2007. I have C# code written in a separate binary. The code uses static classes and static methods on the classes. I have a reference to the DLL in my VSTO Excel Worksheet project. What do I have to add or modify to get this to work?
My C# code looks like this:
using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
namespace FooStatistics
{
[ComVisible(true)]
public static class Statistics
{
public static int Count(Range range)
{
return range.Count;
}
I want to be able to put a formula into an Excel cell that looks like this:
=FooStatistic.Statistic.Count(A1:A10)
Or whatever.
I've seen this but it appears to be for non-static classes in Excel 2003. I can't believe the integration is not better by now.
I've looked at a lot of StackOverflow questions on this. They don't appear to provide native integration (many say, "Use X open source library") and, ominously, many are not accepted by the OP. I am not looking for, "Make it into a COM object and call it from VBA."
So I'm looking for:
- Excel 2007
- code in C# DLL
- call from Excel cell as UDF
- native integration
So here's another StackOverflow link, in which two responders say:
- As far as I know, you cannot directly create UDFs in VSTO.
- VSTO has no support for creating Excel UDFs. Automation Add-Ins can be created in .Net, and seem to be the Microsoft approved way of doing it.
This is a question from June 2009. Is this true -- in 2009 you have to expose your .NET components as COM servers to get callable UDFs for Excel?