判断机器是否有安装excel
//判断机器是否有安装excel
Type type = Type.GetTypeFromProgID("Excel.Application");
今天改bug的时候,突然有个要求需要判断机器是否有安装excel,如果没有安装,用dev控件导出excel会出错.俺就在网上狂搜了,结果搜到这句最简便,还有可以使用检查注册表的方式.
注册表方式:

using System;
using System.Collections.Generic;
using System.Text;
namespace WpsTest
{
public class OfficeOperator
{
#region 方法
#region public static
/// <summary>
/// 检测MS-Office是否正确安装
/// 通过注册表检测
/// </summary>
/// <param name="version">获得安装的版本号,如office2000,office2003,office2007</param>
/// <returns></returns>
public static bool IsInstall(out string Version)
{
bool result = false;
string officePath = "";
string officeVersion = "";
Version = "";
GetOfficePath(out officePath, out officeVersion);
if (!string.IsNullOrEmpty(officeVersion) && !string.IsNullOrEmpty(officePath))
{
result = true;
Version = officeVersion;
}
return result;
}
/// <summary>
/// 获取当前某个版本Office的安装路径
/// </summary>
/// <param name="Path">返回当前系统Office安装路径</param>
/// <param name="Version">返回当前系统Office版本信息</param>
public static void GetOfficePath(out string Path,out string Version)
{
string strPathResult = "";
string strVersionResult = "";
string strKeyName = "Path";
object objResult = null;
Microsoft.Win32.RegistryValueKind regValueKind;
Microsoft.Win32.RegistryKey regKey = null;
Microsoft.Win32.RegistryKey regSubKey = null;
try
{
regKey = Microsoft.Win32.Registry.LocalMachine;
if (regSubKey == null)
{//office97
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\8.0\Common\InstallRoot", false);
strVersionResult = "office97";
strKeyName = "OfficeBin";
}
if (regSubKey == null)
{//Office2000
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\9.0\Common\InstallRoot", false);
strVersionResult = "office2000";
strKeyName = "Path";
}
if (regSubKey == null)
{//officeXp
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\10.0\Common\InstallRoot", false);
strVersionResult = "officeXP";
strKeyName = "Path";
}
if (regSubKey == null)
{//Office2003
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\11.0\Common\InstallRoot", false);
strVersionResult = "office2003";
strKeyName = "Path";
}
if (regSubKey == null)
{//office2007
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Common\InstallRoot", false);
strVersionResult = "office2007";
strKeyName = "Path";
}
objResult = regSubKey.GetValue(strKeyName);
regValueKind = regSubKey.GetValueKind(strKeyName);
if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
{
strPathResult = objResult.ToString();
}
}
catch (System.Security.SecurityException ex)
{
throw new System.Security.SecurityException("您没有读取注册表的权限", ex);
}
catch (Exception ex)
{
throw new Exception("读取注册表出错!", ex);
}
finally
{
if (regKey != null)
{
regKey.Close();
regKey = null;
}
if (regSubKey != null)
{
regSubKey.Close();
regSubKey = null;
}
}
Path = strPathResult;
Version = strVersionResult;
}
#endregion public static
#endregion 方法
}
}
//使用方法
private void btnGetOfficePath_Click(object sender, EventArgs e)
{
string officePath = "";
string officeVersion = "";
try
{
OfficeOperator.GetOfficePath(out officePath,out officeVersion);
}
catch (Exception ex)
{
MessageBox.Show("无法获取系统Office信息");
}
if (!string.IsNullOrEmpty(officePath) && !string.IsNullOrEmpty(officeVersion))
{
MessageBox.Show(string.Format("版本:{0}\r\n安装路径:{1}",officeVersion,officePath));
}
}
//是否安装及版本
private void btnIsInstall_Click(object sender, EventArgs e)
{
try
{
string version = "";
if (OfficeOperator.IsInstall(out version))
{
MessageBox.Show(string.Format("当前安装了{0}", version));
}
else
{
MessageBox.Show("您当前还没有安装微软Office系列软件");
}
}
catch(Exception ex)
{
MessageBox.Show("无法获取系统Office信息");
}
}
using System.Collections.Generic;
using System.Text;
namespace WpsTest
{
public class OfficeOperator
{
#region 方法
#region public static
/// <summary>
/// 检测MS-Office是否正确安装
/// 通过注册表检测
/// </summary>
/// <param name="version">获得安装的版本号,如office2000,office2003,office2007</param>
/// <returns></returns>
public static bool IsInstall(out string Version)
{
bool result = false;
string officePath = "";
string officeVersion = "";
Version = "";
GetOfficePath(out officePath, out officeVersion);
if (!string.IsNullOrEmpty(officeVersion) && !string.IsNullOrEmpty(officePath))
{
result = true;
Version = officeVersion;
}
return result;
}
/// <summary>
/// 获取当前某个版本Office的安装路径
/// </summary>
/// <param name="Path">返回当前系统Office安装路径</param>
/// <param name="Version">返回当前系统Office版本信息</param>
public static void GetOfficePath(out string Path,out string Version)
{
string strPathResult = "";
string strVersionResult = "";
string strKeyName = "Path";
object objResult = null;
Microsoft.Win32.RegistryValueKind regValueKind;
Microsoft.Win32.RegistryKey regKey = null;
Microsoft.Win32.RegistryKey regSubKey = null;
try
{
regKey = Microsoft.Win32.Registry.LocalMachine;
if (regSubKey == null)
{//office97
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\8.0\Common\InstallRoot", false);
strVersionResult = "office97";
strKeyName = "OfficeBin";
}
if (regSubKey == null)
{//Office2000
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\9.0\Common\InstallRoot", false);
strVersionResult = "office2000";
strKeyName = "Path";
}
if (regSubKey == null)
{//officeXp
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\10.0\Common\InstallRoot", false);
strVersionResult = "officeXP";
strKeyName = "Path";
}
if (regSubKey == null)
{//Office2003
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\11.0\Common\InstallRoot", false);
strVersionResult = "office2003";
strKeyName = "Path";
}
if (regSubKey == null)
{//office2007
regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Common\InstallRoot", false);
strVersionResult = "office2007";
strKeyName = "Path";
}
objResult = regSubKey.GetValue(strKeyName);
regValueKind = regSubKey.GetValueKind(strKeyName);
if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
{
strPathResult = objResult.ToString();
}
}
catch (System.Security.SecurityException ex)
{
throw new System.Security.SecurityException("您没有读取注册表的权限", ex);
}
catch (Exception ex)
{
throw new Exception("读取注册表出错!", ex);
}
finally
{
if (regKey != null)
{
regKey.Close();
regKey = null;
}
if (regSubKey != null)
{
regSubKey.Close();
regSubKey = null;
}
}
Path = strPathResult;
Version = strVersionResult;
}
#endregion public static
#endregion 方法
}
}
//使用方法
private void btnGetOfficePath_Click(object sender, EventArgs e)
{
string officePath = "";
string officeVersion = "";
try
{
OfficeOperator.GetOfficePath(out officePath,out officeVersion);
}
catch (Exception ex)
{
MessageBox.Show("无法获取系统Office信息");
}
if (!string.IsNullOrEmpty(officePath) && !string.IsNullOrEmpty(officeVersion))
{
MessageBox.Show(string.Format("版本:{0}\r\n安装路径:{1}",officeVersion,officePath));
}
}
//是否安装及版本
private void btnIsInstall_Click(object sender, EventArgs e)
{
try
{
string version = "";
if (OfficeOperator.IsInstall(out version))
{
MessageBox.Show(string.Format("当前安装了{0}", version));
}
else
{
MessageBox.Show("您当前还没有安装微软Office系列软件");
}
}
catch(Exception ex)
{
MessageBox.Show("无法获取系统Office信息");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?