C# 判断本机是否安装Excel及多版本安装?获取Excel进程信息和打开Excel应用软件


http://hi.baidu.com/yebihaigsino/blog/item/36e4ea6f864743d281cb4ad9.html

http://hi.baidu.com/devzhao/blog/item/3f527e435e91de149313c6b7.html

方法一:

异常判断法(根据返回结果形式)

// 使用地方
private void buttonOk_Click(object sender, EventArgs e)
{
   if (codeboolisExcelInstalled())
   {
    MessageBox.Show("本机已安装Excel文件");
   }
   else
   {
    MessageBox.Show("当前系统没有发现可执行的Excel文件, 如需使用Excel功能请先安装office 2003", "警告", 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 2003 12.0是office 2007

// 使用地方
private void buttonOk_Click(object sender, EventArgs e)
{
   if (ExistsRegedit())
   {
    MessageBox.Show("本机已安装Excel文件");
   }
   else
   {
    MessageBox.Show("当前系统没有发现可执行的Excel文件, 如需使用Excel功能请先安装office 2003", "警告", 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\\");
    //检查本机是否安装Office2003
    if (akey != null)
    {
        string file03 = akey.GetValue("Path").ToString();
        if (File.Exists(file03 + "Excel.exe"))
        {
            ifused = true;
         }
     }
     //检查本机是否安装Office2007
     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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值