新的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
不知道为什么
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
不知道为什么
哪句代码出的异常?
我用的是EXCEL2007,不过在另外一台机器上测试通过,系统是(VISTA+OFFICE2007)。就是为了这个2007才要做这个判别的