tags:

views:

37

answers:

2

Everywhere I look for how to use excel function inside of a C# application tells me to do the following:

  • From the COM TabAdd the following using statement:using IExcel = Microsoft.Office.Interop.Excel
  • Then declare and initialise a IWorkSheetFunction objectIExcel.IWorksheetFunction iwf = new IExcel.IWorksheetFunction();
  • Now you can use the functions as in int result = iwf.Math.Sum(1,2);

The problem im having is that while intellisense is detecting IExcel, it is not showing IWorksheetFunction. It is however showing WorksheetFunction. Either way, it is not letting me instantiate it as an object. I am getting the error: Cannot create an instance of the abstract class or interface 'Microsoft.Office.Interop.Excel.WorksheetFunction'

any ideas?

+1  A: 

Try:

Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;
int result = wsf.Percentile(obj, 0.75);

Basically it comes down to, instead of:

IExcel.IWorksheetFunction iwf = 
       new IExcel.IWorksheetFunction(); // You can't instantiate an interface

use the WorksheetFunction property in Excel.Application:

IExcel.IWorksheetFunction iwf = xl.WorksheetFunction;
Michael Goldshteyn
that did it! weird. thanks :)
Sinaesthetic
A: 

Its a function off the application object.

using Microsoft.Office.Interop.Excel;
static void Main(string[] args)
{

     Application a = new Application();
     double x = a.WorksheetFunction.Sum(1, 2);
     Console.WriteLine("Sum = {0}", x);

}
rerun