简体   繁体   English

如何以编程方式检查PC上是否存在MS Excel?

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

I have an application that needs MS Excel to run, otherwise it crashes. 我有一个需要MS Excel运行的应用程序,否则崩溃。 So I want to check and warn the user in case Excel is not installed on user's machine. 所以我想检查并警告用户以防用户机器上没有安装Excel。

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#). 这篇博客文章描述了如何验证Excel是否通过注册表安装 (VB.NET代码,但可以很容易地转换为C#)。 Basically, you are going to verify via the HKEY_CLASSES_ROOT\\Excel.Application\\CurVer key. 基本上,您将通过HKEY_CLASSES_ROOT\\Excel.Application\\CurVer键进行验证。

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 ? 它是否真的需要安装MS Excel ,或者您是否需要计算机才能显示Excel文件 For example, if the user has LibreOffice or another similar Excel-file compatible application installed, would that be acceptable? 例如,如果用户安装了LibreOffice或其他类似的Excel文件兼容应用程序,那么这是可以接受的吗?

We have an application that opens Excel files and PDF files for the user. 我们有一个应用程序可以为用户打开Excel文件和PDF文件。 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. 我们只需要Process.Start(...)文件,让操作系统从那里获取它。

We do wrap the call in a Try/Catch block and offer the user suggestions if this call results in an error; 我们将调用包装在Try/Catch块中,并在此调用导致错误时提供用户建议; suggestions, like that they may not have Office (Excel) installed, or they are missing a PDF viewer. 建议,例如他们可能没有安装Office(Excel),或者他们缺少PDF查看器。 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. 基本上,我们不是主动尝试识别用户的计算机是否处于完全足够的状态来执行操作,而是假设它是,但是在我们发现它的时候处理这​​种情况。

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

相关问题 如何以编程方式禁用 Excel 中的公式错误检查 - How to disable formula error check in Excel programmatically 如何检查记录是否存在并在C#中的ms Access数据库中插入 - How to check if record exists or not and insert in ms access database in c# 如何检查MS Access数据库中是否存在指定的列? - How do I check if specified column exists in MS Access database? 如何检查MS Access数据库文件中是否已存在ID,以及如何在TextBox中使用TextChanged进行检查? - How to check if ID already exists in MS Access database file, and how to check it by using TextChanged in TextBox? 检查Excel工作表中是否存在列 - check if a column exists in Excel sheet 以编程方式检查C#中是否存在COM端口 - Programmatically check if a COM port exists in C# 如何在PC上检查DirectShow过滤器 - How to check for directshow filters on pc 如何以编程方式控制我的电脑音量? - How to programmatically control the volume of my pc? 在尝试Process.Start()之前,如何以编程方式检查文件关联存在的文件? - How can I programmatically check file that a file association exists before attempting to Process.Start() it? 从控制器中的excel导入数据时,如何检查excel文件中是否存在特定的列? - While importing data from excel in the controller how to check if the specific column exists in the excel file or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM