Interop Excel method LinEst failing with DISP_E_TYPEMISMATCH

3

I am facing a problem while making Excel's LinEST function.

My program goes like

MyExcel.Application xl = new MyExcel.Application();
MyExcel.WorksheetFunction wsf = xl.WorksheetFunction;
List<int> x = new List<int> { 1, 2, 3, 4 };
List<int> y = new List<int> { 11, 12, 45, 42 };
object o = wsf.LinEst(x, y, true, true);

And the namespace is using MyExcel = Microsoft.Office.Interop.Excel;

The program is compiling smoothly but at runtime it is throwing an error

{System.Runtime.InteropServices.COMException (0x80020005): Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

Actually this is the first time I am using Excel function .. so I am unable to proceed further. If any one has run thru this kind of situation and has solved, please help me.

I am using C# 3.0.

excel
c#-3.0
excel-interop
asked on Stack Overflow Apr 24, 2010 by Newbie • edited Oct 24, 2014 by Unihedron

1 Answer

2

Convert the lists x and y to array :

    MyExcel.Application xl = new MyExcel.Application();
    MyExcel.WorksheetFunction wsf = xl.WorksheetFunction;
    List<int> x = new List<int> { 1, 2, 3, 4 };
    List<int> y = new List<int> { 11, 12, 45, 42 };
    //object o = wsf.LinEst(x, y, true, true);
    object o = wsf.LinEst(y.ToArray(), x.ToArray(), false, true);
answered on Stack Overflow Apr 24, 2010 by Newbie • edited Jan 23, 2013 by Kevin J F

User contributions licensed under CC BY-SA 3.0