Creating an instance of an Excel function

Wherever I look for how to use the excel function inside a C # application, I need to do the following:

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

Im problem is that although intellisense detects IExcel, it does not show IWorksheetFunction. However, it shows WorksheetFunction. In any case, this does not allow me to instantiate the object as an object. I get an error: cannot create an instance of the abstract class or interface "Microsoft.Office.Interop.Excel.WorksheetFunction"

any ideas?

+3
source share
2 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);

It basically boils down to:

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

use property WorksheetFunctionin Excel.Application:

IExcel.IWorksheetFunction iwf = xl.WorksheetFunction;
+3
source

Its function is disconnected from 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);

}
0
source

Source: https://habr.com/ru/post/1770029/


All Articles