简体   繁体   English

从C#调用Excel / DLL / XLL函数

[英]Calling Excel/DLL/XLL functions from C#

I have a particular function in an Excel addin(xll). 我在Excel插件(xll)中有一个特定的功能。 The addin is proprietary and we do not have access to the source code. 插件是专有的,我们无权访问源代码。 However we need to call some functions contained within the addin and we would like to call it from a C# program. 但是我们需要调用addin中包含的一些函数,我们想从C#程序中调用它。

Currently, I was thinking of writing a C++ interface calling the Excel function with xlopers, then calling this C++ interface from C#. 目前,我正在考虑编写一个C ++接口,用xlopers调用Excel函数,然后从C#调用这个C ++接口。

Does anybody who has prior experience of this kind of issues know what would be the best solution for that ? 有过此类问题的先前经验的人是否知道什么是最佳解决方案?

Anthony 安东尼

You need to create a fake xlcall32.dll , put it in the same directory as your XLL (do not put excel's own xlcall32.dll in the PATH). 您需要创建一个假的x​​lcall32.dll ,将它放在与XLL相同的目录中(不要将excel自己的xlcall32.dll放在PATH中)。 Here is some code: 这是一些代码:

# include <windows.h>

typedef void* LPXLOPER;

extern "C" void __declspec(dllexport) XLCallVer ( ) {}

extern "C" int __declspec(dllexport) Excel4 (int xlfn, LPXLOPER operRes, int count,... ) { return 0; }

extern "C" int __declspec(dllexport) Excel4v(int xlfn, LPXLOPER operRes, int count, LPXLOPER far opers[]) {return 0;}

Now suppose I have an XLL called xll-dll.xll with a function called (use "depends.exe" to find out the names of the exported functions) xlAdd that well adds two doubles: extern "C" __declspec(dllexport) XLOPER * __cdecl xlAdd(XLOPER* pA, XLOPER* pB); 现在假设我有一个名为xll-dll.xll的XLL,其函数名为(使用“depends.exe”来查找导出函数的名称)xlAdd,它添加了两个双精度:extern“C”__declspec(dllexport)XLOPER * __cdecl xlAdd(XLOPER * pA,XLOPER * pB);

The following code calls it: 以下代码称之为:


# include <windows.h>
# include <iostream>

// your own header that defines XLOPERs
# include <parser/xll/xloper.hpp>

// pointer to function taking 2 XLOPERS
typedef XLOPER * (__cdecl *xl2args) (XLOPER* , XLOPER* ) ;

void test(){
/// get the XLL address
HINSTANCE h = LoadLibrary("xll-dll.xll");
if (h != NULL){
xl2args myfunc;
/// get my xll-dll.xll function address
myfunc = (xl2args) GetProcAddress(h, "xlAdd");
if (!myfunc) { // handle the error
FreeLibrary(h); }
else { /// build some XLOPERS, call the remote function
XLOPER a,b, *c;
a.xltype = 1; a.val.num = 1. ;
b.xltype = 1; b.val.num = 2. ;
c = (*myfunc)(&a,&b);
std::cout << " call of xll " << c->val.num << std::endl; }
FreeLibrary(h); }
}

int main()
{test();}

My exe actually works (to my own surprise), and output 3 as expected. 我的exe实际上工作(令我惊讶的是),并按预期输出3。 You must have some knowledge of what your XLL actually expects for parameters. 您必须了解XLL实际对参数的期望。 If it allocates some memory, you must check if the #define xlbitDLLFree 0x4000 is set on your XLOPER c->type, and call back "xlAutoFree". 如果它分配了一些内存,则必须检查XLOPER c->类型上是否设置了#define xlbitDLLFree 0x4000,并回调“xlAutoFree”。

You might like to try XLL Plus http://www.planatechsolutions.com/xllplus/default.htm . 您可能想尝试XLL Plus http://www.planatechsolutions.com/xllplus/default.htm It's a bit pricey, but the XLL Wrapper Libraries feature is exactly what you are looking for: 它有点贵,但XLL Wrapper Libraries功能正是您所需要的:

"Sometimes it is useful to be able to call your Excel add-in functions from other environments, such as command-line programs or interactive applications, written in C++, Java, C# or Visual Basic. The Xll Wrapper toolkit contains tools, a runtime library, samples and documentation to help with the development of COM modules and .NET assemblies that wrap Excel XLL add-ins" “有时能够从其他环境调用Excel加载项函数很有用,例如用C ++,Java,C#或Visual Basic编写的命令行程序或交互式应用程序.Xll Wrapper工具包包含工具,运行时库,示例和文档,以帮助开发包含Excel XLL加载项的COM模块和.NET程序集“

An Excell add-in DLL might be written in C#. Excell外接程序DLL可能用C#编写。 If so, then you can probably bypass Excel altogether. 如果是这样,那么你可以完全绕过Excel。 Might need a wrapper though. 可能需要一个包装器。

检查汇编在安装plguin的程序文件中是否可用,将该汇编直接引用到c#项目 - 检查对象浏览器中的类,方法或对象

You should be able to use reflection to get access to your addin. 您应该能够使用反射来访问您的插件。 try using The Reflector to see what's available. 尝试使用The Reflector查看可用的内容。

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

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