Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating an Excel function

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?

like image 311
Sinaesthetic Avatar asked Jun 14 '26 22:06

Sinaesthetic


1 Answers

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;
like image 137
Michael Goldshteyn Avatar answered Jun 16 '26 10:06

Michael Goldshteyn