简体   繁体   English

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

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

I would like to know how to run the regression function in microsoft excel using c#. Currently i am following this tutorial to automate regression in excel:我想知道如何使用 c# 在 Microsoft excel 中运行回归 function。目前我正在按照本教程在 excel 中自动回归:

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

Everything works fine, but the function that i am using (x1 in the tutorial as the variable):一切正常,但我正在使用的 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);

Throws the following error:抛出以下错误:

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.

I have checked that the data analysis tab is usable in excel and i can successfully execute the regression function within excel itself manually.我检查过数据分析选项卡在 excel 中是否可用,我可以在 excel 中手动成功执行回归 function。 The rest of the program can also open excel, insert into the cells and close it.程序的rest也可以打开excel,插入到单元格中关闭。

My hypotheses is that the macro im trying to call is misspelled or that something within excel is preventing me from calling the tool/macro.我的假设是我试图调用的宏拼写错误或者 excel 中的某些内容阻止我调用该工具/宏。

I am having trouble finding a reference sheet or something recording the macro/tool names, so any example would be appreciated.我找不到参考表或记录宏/工具名称的东西,所以任何例子都将不胜感激。

Thanks in advance!提前致谢!

You need to reference the name of the.xlam file that has the macro you are trying to run.您需要引用包含您尝试运行的宏的 .xlam 文件的名称。 For what you are trying to do, I think it should be something like:对于您要尝试做的事情,我认为它应该是这样的:

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