新的EXCEL的connectionstring与原来的写法不同,因此想在确定connectionstring前先获得本机安装的EXCEL版本
        private string CheckExcelVersion()
        {
            Type type;
            object excel;
            string version = "";            type = Type.GetTypeFromProgID("Excel.Application");
            string str = type.ToString();            if (type == null)
            {
                MessageBox.Show("没有安装excel");
            }
            else
            {
                excel = Activator.CreateInstance(type);
                if (excel == null)
                {
                    MessageBox.Show("创建对象出错");
                }
                else
                {
                    version = type.GetProperty("Version").GetValue(excel, null).ToString();
                    //type.GetProperty("Visible").SetValue(excel, true, null);
                    type.GetMethod("Quit").Invoke(excel, null);
                    //GC.Collect();
                }
            }
            return version.Trim();
        }在一台机器上测试没有问题,但是到另外一台机器上却报
TargetInvocationException
不知道为什么

解决方案 »

  1.   

    我这里测试可以,我的环境是Excel2003
    哪句代码出的异常?
      

  2.   

    version = type.GetProperty("Version").GetValue(excel, null).ToString();
    我用的是EXCEL2007,不过在另外一台机器上测试通过,系统是(VISTA+OFFICE2007)。就是为了这个2007才要做这个判别的
      

  3.   

    我以前做过97,2000,xp,2003,都是用的Excel.Application,没有问题,这种写法本来就是保持向后兼容的。是不是EXCEL2007的调用方法不一样????
      

  4.   

    强悍阿,这么快就用到office2007了
      

  5.   

    但是我在Vista+Excel2007上可以查到Excel版本