Calling function in XLL file from C#

牧云@^-^@ 提交于 2019-12-25 03:30:34

问题


I have a xll file which have a function and I want to call that function from C#. This xll file is used in excel.any body have idea how to do that?.I tried refrencing the xll file but I am not getting the value.If I open excel like start-->programms-->excel and in excel if i directly give the function I am getting right value.Same thing If i automate excel in c# and opened excel from Microsoft.Office.Interop.Excel and applied the function I am getting error


回答1:


You probably won't be able to invoke the function directly because an XLL will expect the Excel interfaces to be driving. You'd have to do some serious faking.

Automating it through Excel will work though.




回答2:


An xll is just a dll. If you know the signature of the function you want to call, you can call it just like any other function. If you don't know the signature but can load the add-in in Excel, there is a way to get Excel to tell you the signature.

Automation only works if you are running Excel. There is no need for that once you know the signature.




回答3:


There is a few tools you can use:

XLL Plus
XLL Host


The question has also been asked on MSDN Use XLL from C#, this is the answer:

If it is a COM project, you can add it via the COM tab in the Add reference window.

If you need to create a .NET assembly from the dll, you can try to use Type Library Import tool to create a wrapper around the dll:

tlbimp.exe xll.dll /out:xllnet.dll

If the object does not support COM, but only native calls, you will need to platform invoke the methods using the DllImport attribute. See the Platform Invoke tutorial for more information.


I gave all 3 suggestions a go but didn't have any luck. Given the author of the XLL tool Excel-DNA recommends using a tool I suspect consuming a XLL in a Winform/WPF/Console or Web app is not trivial: https://stackoverflow.com/a/2865023/495455



来源:https://stackoverflow.com/questions/7406934/calling-function-in-xll-file-from-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!