Interop Excel method LinEst failing with DISP_E_TYPEMISMATCH

折月煮酒 提交于 2019-12-10 20:07:47

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/2703623/interop-excel-method-linest-failing-with-disp-e-typemismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!