gpt4 book ai didi

excel - 互操作 Excel 方法 LinEst 因 DISP_E_TYPEMISMATCH 失败

转载 作者:行者123 更新时间:2023-12-02 21:06:40 24 4
gpt4 key购买 nike

我在制作 Excel 的 LinEST 函数时遇到问题。

我的程序是这样的

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

命名空间是 using MyExcel = Microsoft.Office.Interop.Excel;

程序编译顺利,但在运行时抛出错误

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

实际上这是我第一次使用Excel函数..所以我无法继续。如果有人遇到过这种情况并解决了,请帮助我。

我使用的是 C# 3.0。

最佳答案

将列表 x 和 y 转换为数组:

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

关于excel - 互操作 Excel 方法 LinEst 因 DISP_E_TYPEMISMATCH 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2703623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com