| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3689 人关注过本帖
标题:请问如何检查 excel 的版本?
只看楼主 加入收藏
jtain
Rank: 1
来 自:台北
等 级:新手上路
帖 子:18
专家分:3
注 册:2011-11-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:9 
请问如何检查 excel 的版本?
有什么办法可以得知目前系统中安装的 excel 版本?

BCB 6 可以用下面的方法...但 C# 我因为刚开使用实在是找不到类似的方法

AnsiString OfficeVer;
AnsiString GetOfficeVer(){
AnsiString s0;
double d1;                  
    //取得 Office 的版本并存在公用变数 AnsiString OfficeVer
    Variant objExcel = Variant::CreateObject("Excel.Application");
    OfficeVer=objExcel.OlePropertyGet("Version");
    VarClear(objExcel);
   
    if(OfficeVer.IsEmpty()) s0="(是否未安装 MS office?)";
    else{
        d1=OfficeVer.ToDouble();
        if(d1>=14) s0="2010";
        else if(d1>=12) s0="2007";
        else if(d1>=10) s0="XP";
        else if(d1>=9) s0="2000";
        else if(d1>=8) s0="97";
        else if(d1>=7) s0="95";

        s0="(Office "+s0+")";
    }

    return(s0);
}
搜索更多相关主题的帖子: 检查 office Office double excel 
2011-12-06 16:49
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
程序代码:
try
{
//   which   ever   version   of   Excel   was   referenced   to   build   the   interop   file
//   should   be   installed   on   client   machines
excelApp   =   new   Excel.Application(); 

//   create   the   task   list   on   the   first   worksheet   in   the   workbook
workBook   =   excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
ws   =   (Worksheet)   workBook.Worksheets[1];
}
catch
{
//   remove   the   excel   references:   Excel,   Microsoft.Office.Core,   and   VBIDE   and   re-add
//   the   COM   reference   Microsoft   Excel   10.0   Type   Library.
MessageBox.Show( "An   incompatible   version   of   Microsoft   Excel   is   intalled   on   your   system. ");
return;
} 

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2011-12-06 17:15
jtain
Rank: 1
来 自:台北
等 级:新手上路
帖 子:18
专家分:3
注 册:2011-11-20
收藏
得分:0 
版主你的這個只能檢查是否有安裝 excel
我是希望能確認版本

快乐不需要理由

Visual Studio 2010 Pro 繁体中文版
XP sp3 繁体中文版
2011-12-06 18:17
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:20 
http://

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2011-12-07 08:01
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
注册表啊!

   唯实惟新 至诚致志
2011-12-07 09:57
jtain
Rank: 1
来 自:台北
等 级:新手上路
帖 子:18
专家分:3
注 册:2011-11-20
收藏
得分:0 
刚刚发现的另一个判断是否有 excel 的方法

//判断本机是否安装Excel文件方法
private bool codeboolisExcelInstalled()
{
  Type type = Type.GetTypeFromProgID("Excel.Application");
  return type != null;
}

快乐不需要理由

Visual Studio 2010 Pro 繁体中文版
XP sp3 繁体中文版
2011-12-07 10:25
jtain
Rank: 1
来 自:台北
等 级:新手上路
帖 子:18
专家分:3
注 册:2011-11-20
收藏
得分:0 
再没有办法取得 Version 之前只能用下面的方式了

ps: 補充說明一下我不喜歡用 regedit 去識別的原因是必須檢查所有的可能
萬一有新版就會破功,像我原先的 BCB 我可以直接檢查 >=12 就可以辨識 .xls 或 .xlsx
    if(OfficeVer.ToDouble()>=12) s1=".xlsx";
    else s1=".xls";


转贴自 http://topic.

/// <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;
}


[ 本帖最后由 jtain 于 2011-12-7 14:26 编辑 ]

快乐不需要理由

Visual Studio 2010 Pro 繁体中文版
XP sp3 繁体中文版
2011-12-07 14:12
jtain
Rank: 1
来 自:台北
等 级:新手上路
帖 子:18
专家分:3
注 册:2011-11-20
收藏
得分:0 
上面的傳回值應該改成 int
並且要加上其他個版本的 Excel regedit 值
也就是用 0,1,2 來帶表示是否有EXCEL或 2003 以前,以及 2007 以後
不過我始終無法正確輸入那個驗證碼...所以放棄修改

快乐不需要理由

Visual Studio 2010 Pro 繁体中文版
XP sp3 繁体中文版
2011-12-07 14:59
jtain
Rank: 1
来 自:台北
等 级:新手上路
帖 子:18
专家分:3
注 册:2011-11-20
收藏
得分:0 
終於弄出正確的檢查方式
        //-----------------------------------------------
        //判斷本機是否安裝Excel文件方法
        private bool codeboolisExcelInstalled()
        {
            Type type = Type.GetTypeFromProgID("Excel.Application");
            return type != null;
        }

        private double JongCheckExcelVer()
        {
            Type objExcelType = Type.GetTypeFromProgID("Excel.Application");
            if (objExcelType == null)
            {
                return 0;
            }
            object objApp = Activator.CreateInstance(objExcelType);
            if (objApp == null)
            {
                return 0;
            }
            object objVer = objApp.GetType().InvokeMember("Version", BindingFlags.GetProperty, null, objApp, null);
            double iVer = Convert.ToDouble(objVer.ToString());
            return iVer;
        }

        public String JongGetExcelVerStr()
        {
            String s1;
            double excelver;
            excelver = JongCheckExcelVer();// ExistsExcelRegedit();
            s1 = " Office ";
            if (excelver == 0)
            {
                MessageBox.Show("無法識別Excel的版本", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
                s1 = "無法識別 office 版本";
            }
            else if (excelver >= 14) s1 += "2010或以上";
            else if (excelver >= 12) s1 += "2007";
            else if (excelver >= 11) s1 += "2003";
            else if (excelver >= 10) s1 += "XP";
            else if (excelver >= 9) s1 += "2000";
            else if (excelver >= 8) s1 += "97";
            else if (excelver >= 7) s1 += "95";

            return s1;
        }
        /*
            if (!codeboolisExcelInstalled())
            {
                MessageBox.Show("Excel不存在", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.Text += JongGetExcelVerStr();
         */

快乐不需要理由

Visual Studio 2010 Pro 繁体中文版
XP sp3 繁体中文版
2011-12-20 18:20
jtain
Rank: 1
来 自:台北
等 级:新手上路
帖 子:18
专家分:3
注 册:2011-11-20
收藏
得分:0 
因为发现在工作管理员会残留一个 execl.exe ,所以加上强制清除
因为不知道是否还有其他更好的释放方式,目前我就只能这样清除
底下这一段加在 return iVer; 之前

            objVer = null;
            objApp = null;
            objExcelType = null;
            GC.Collect();

快乐不需要理由

Visual Studio 2010 Pro 繁体中文版
XP sp3 繁体中文版
2011-12-29 16:53
快速回复:请问如何检查 excel 的版本?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013231 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved