How to check, programmatically, if MS Excel exists on a pc?

耗尽温柔 提交于 2019-11-28 07:31:48
Type officeType = Type.GetTypeFromProgID("Excel.Application");
if (officeType == null)
{
     //no Excel installed
}
else
{
     //Excel installed
}

As a quick fix you could just catch the exception and implement proper error handling. Then you can inform the user there.

const string ASSEMBLY2003 = "Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";  

static bool IsAssemblyInstalled(string assembly)  
{  
   try 
   {  
       s_assemblyExcel = Assembly.Load(assembly);  
       return true;  
   }   
   catch 
   {  
       return false;  
   }  
} 

this will do the trick, just do it for all versions

and it can be done like this also

RegistryKey key = Registry.ClassesRoot;
RegistryKey excelKey = key.OpenSubKey("Excel.Application");
bool excelInstalled = excelKey == null ? false : true;

This blog post here describes how to verify if Excel is installed via the registry (VB.NET code but can easily be converted to C#). Basically, you are going to verify via the HKEY_CLASSES_ROOT\Excel.Application\CurVer key.

This doesn't answer your specific question, but does tackle it from an alternate direction...

Does it really need MS Excel to be installed, or do you need the computer to simply be able to display Excel files? For example, if the user has LibreOffice or another similar Excel-file compatible application installed, would that be acceptable?

We have an application that opens Excel files and PDF files for the user. We don't really care what software they user has on their computer to view these files. That's not really our concern. We simply Process.Start(...) the file and let the OS take it from there.

We do wrap the call in a Try/Catch block and offer the user suggestions if this call results in an error; suggestions, like that they may not have Office (Excel) installed, or they are missing a PDF viewer. Basically, instead of proactively trying to identify if the user's computer is in a complete enough state to perform the action, we assume it is, but then handle the situation when it's not once we have discovered it.

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