繁体   English   中英

如何使用 C# 在 Microsoft excel 中运行回归 function(或任何工具/内置宏)

[英]How to run the regression function (or any tool/built-in macro) in microsoft excel using C#

我想知道如何使用 c# 在 Microsoft excel 中运行回归 function。目前我正在按照本教程在 excel 中自动回归:

http://www.michaelcodes.net/2018/09/using-c-to-automate-linear-regression.html

一切正常,但我正在使用的 function(教程中的 x1 作为变量):

myExcelInteropApplication.Run(@"Regress", sheet.Range($"$A$1:$A${rowCount}"),
                sheet.Range($"$B$1:$B${rowCount}"), false, true, Type.Missing, sheet.Range("$D$1"),
                true, false, false, false, Type.Missing, false);

抛出以下错误:

Exception thrown: 'System.Runtime.InteropServices.COMException' in System.Dynamic.dll
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Dynamic.dll
Cannot run the macro 'Regress'. The macro may not be available in this workbook or all macros may be disabled.

我检查过数据分析选项卡在 excel 中是否可用,我可以在 excel 中手动成功执行回归 function。 程序的rest也可以打开excel,插入到单元格中关闭。

我的假设是我试图调用的宏拼写错误或者 excel 中的某些内容阻止我调用该工具/宏。

我找不到参考表或记录宏/工具名称的东西,所以任何例子都将不胜感激。

提前致谢!

您需要引用包含您尝试运行的宏的 .xlam 文件的名称。 对于您要尝试做的事情,我认为它应该是这样的:

myExcelInteropApplication.Run("ATPVBAEN.XLAM!Regress", sheet.Range($"$A$1:$A${rowCount}"),
                sheet.Range($"$B$1:$B${rowCount}"), false, true, Type.Missing, sheet.Range("$D$1"),
                true, false, false, false, Type.Missing, false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM