/yebihaigsino/blog/item/36e4ea6f864743d281cb4ad9.html
/devzhao/blog/item/3f527e435e91de149313c6b7.html
方法一:
异常判断法(根据返回结果形式)
// 使用地方
private void buttonOk_Click(object sender, EventArgs e)
{
if (codeboolisExcelInstalled())
{
MessageBox.Show("本机已安装Excel文件");
}
else
{
MessageBox.Show("当前系统没有发现可执行的Excel文件, 如需使用Excel功能请先安装office ", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//判断本机是否安装Excel文件方法
private bool codeboolisExcelInstalled()
{
Type type = Type.GetTypeFromProgID("Excel.Application");
return type != null;
}
====================================================================
方法二:
注册表检查发
判断注册表里有没有SOFTWARE\\Microsoft\\Office\\12.0\\Word\\InstallRoot\\Excel.exe 其中12.0 11.0需要同时判断,因为11.0是office 12.0是office
// 使用地方
private void buttonOk_Click(object sender, EventArgs e)
{
if (ExistsRegedit())
{
MessageBox.Show("本机已安装Excel文件");
}
else
{
MessageBox.Show("当前系统没有发现可执行的Excel文件, 如需使用Excel功能请先安装office ", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
/// <summary>
/// Self_Variable:查询注册表某个键值是否存在
/// </summary>
/// <returns></returns>
public bool ExistsRegedit()
{
bool ifused = false;
RegistryKey rk = Registry.LocalMachine;
RegistryKey akey = rk.OpenSubKey(@"SOFTWARE\\Microsoft\\Office\\11.0\\Word\\InstallRoot\\");
RegistryKey akeytwo = rk.OpenSubKey(@"SOFTWARE\\Microsoft\\Office\\12.0\\Word\\InstallRoot\\");
//检查本机是否安装Office
if (akey != null)
{
string file03 = akey.GetValue("Path").ToString();
if (File.Exists(file03 + "Excel.exe"))
{
ifused = true;
}
}
//检查本机是否安装Office
if (akeytwo != null)
{
string file07 = akeytwo.GetValue("Path").ToString();
if (File.Exists(file07 + "Excel.exe"))
{
ifused = true;
}
}
return ifused;
}
=======================================
string strInstallPath = file03 /file07 + "Excel.exe";
//获取Excel进程信息
System.Diagnostics.ProcessStartInfo psInfo = new System.Diagnostics.ProcessStartInfo(strInstallPath);
//运行Excel
System.Diagnostics.Process.Start(strInstallPath);