Home » excel » c# 3.0 – Interop Excel method LinEst failing with DISP_E_TYPEMISMATCH

c# 3.0 – Interop Excel method LinEst failing with DISP_E_TYPEMISMATCH

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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);