开发者

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

I have an application that needs MS Excel to run, otherwise it crashes. So I want 开发者_如何转开发to check and warn the user in case Excel is not installed on user's machine.

How do I do this?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜