[英]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.