簡體   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