終於弄出正確的檢查方式
//-----------------------------------------------
//判斷本機是否安裝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();
*/