无法加载XLL编程加载、XLL

2023-09-03 23:04:11 作者:若只如初见

我想一些自动化测试的Excel插件,这是XLL形式。我在加载XLL一些问题。我正在写在C#中,我的code是这样的:

I'm trying to automating some tests for an Excel add-in, which is in xll form. I have some problem in loading the xll. I'm writing it in C# and my code looks like this:

using Microsoft.Office.Interop.Excel;

Application xlApp;
Workbook xlWorkBook;
Worksheet xlWorkSheet;

// create application, open workbook, etc ...
// now try to register xll
xlApp.RegisterXLL("C:\\SomePath\\Whatever.xll");

不过,这总是返回false。我尝试看看Excel中偷偷做,当我通过录制宏手动加载XLL。宏是这样的:

However, this always return false. I try to see what Excel secretly does when I load the xll manually by recording the macro. The macro looks like:

Sub Macro1()
ChDir "C:\SomePath"
Application.RegisterXLL Filename:= _
"C:\SomePath\Whatever.xll"
End Sub

唯一的不同似乎是CHDIR,所以我改变了我的code到:

The only difference seems to be the ChDir, so I changed my code to:

FileSystem.ChDir("C:\\SomePath");
xlApp.RegisterXLL("C:\\SomePath\\Whatever.xll");

但它仍然无法正常工作。另一个奇怪的是,当我把一个断点RegisterXLL前行,并加载XLL手动第一,RegisterXLL方法将返回true。但除此之外,它会返回false。

JAVA运行错误 找不到或无法加载主类

But it still doesn't work. Another odd thing is when I put a breakpoint before the RegisterXLL line and load the xll manually first, the RegisterXLL method will return true. But otherwise, it will return false.

推荐答案

感谢所有的建议。

我通过更改默认的文件路径为Excel应用程序解决了这个问题。

I solved the problem by changing the default file path for the Excel application.

Application.xlApp = new ApplicationClass();
xlApp.DefaultFilePath = "C:\\SomePath";
xlApp.RegisterXLL("Whatever.xll");