How to check if .net interoperability for excel is installed

落花浮王杯 提交于 2019-12-20 07:51:14

问题


I am using .net Primary Interoperability Assembly for Excel in my code. But, the application can be run on machine which doesn't have .net PIA for Excel installed. I want to give an error message if it is not installed

Even though I am checking in GAC, to see if PIA is installed and only if it is present I am using Microsoft.Office.Interop.Excel related code. I am getting an error.

My problem is - I am getting error which is - Unhandled Exception - can't find could not load file or assembly Microsoft.Office.Interop.Excel

Any solution?

Thanks in advance!


回答1:


I've used this Building block to load the x86 / x64 versions of hunspell in may NHunspell wrapper. maybe it is a good starting point for your own dynamic loader:

            // Initialze the dynamic marshall Infrastructure to call the 32Bit (x86) or the 64Bit (x64) Dll respectively 
            SYSTEM_INFO info = new SYSTEM_INFO();
            GetSystemInfo( ref info );

            // Load the correct DLL according to the processor architecture
            switch( info.wProcessorArchitecture )
            {
                case PROCESSOR_ARCHITECTURE.Intel:
                    string pathx86 = AppDomain.CurrentDomain.BaseDirectory;
                    if (!pathx86.EndsWith("\\"))
                        pathx86 += "\\";
                    pathx86 +=  Resources.HunspellX86DllName;

                    dllHandle = LoadLibrary(pathx86);
                    if (dllHandle == IntPtr.Zero)
                        throw new DllNotFoundException(string.Format(Resources.HunspellX86DllNotFoundMessage, pathx86));
                    break;

                case PROCESSOR_ARCHITECTURE.Amd64:
                    string pathx64 = AppDomain.CurrentDomain.BaseDirectory;
                    if (!pathx64.EndsWith("\\"))
                        pathx64 += "\\";
                    pathx64 += Resources.HunspellX64DllName;

                    dllHandle = LoadLibrary(pathx64);
                    if (dllHandle == IntPtr.Zero)
                        throw new DllNotFoundException(string.Format(Resources.HunspellX64DllNotFoundMessage, pathx64));
                    break;

look at this delegate declarations:

internal delegate bool HunspellSpellDelegate(IntPtr handle, [MarshalAs(UnmanagedType.LPWStr)] string word);

And how to bind a library function to it

HunspellSpell = (HunspellSpellDelegate)GetDelegate("HunspellSpell", typeof(HunspellSpellDelegate));

I think this should work for you too, but you have to declare the complete interop code. You can check out the Nhunspell code to get a working sample of this technique:

NHunspell Web Site




回答2:


I guess System.Type.GetTypeFromProgID("Excel.Application",true); method will throw exception if Excel is not present. You can use a try catch block and verify.




回答3:


You shouldn't make static reference to Excel.Interop library. Instead of this, you should look for it in GAC, and load it in runtime. It's rather complicated, and there are no managed API to do this (by default).

Try to use this: fusion.dll API wrapper



来源:https://stackoverflow.com/questions/1237729/how-to-check-if-net-interoperability-for-excel-is-installed

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