来源:http://bbs.csdn.net/topics/330048207
5楼很欢乐,哈哈。
郑重的向6楼的表示感谢:
public static 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());
objVer = null;
objApp = null;
objExcelType = null;
GC.Collect();
return iVer;
}
public static 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;
}
通过上述代码解决了Excel版本识别的问题。