Http://hi.baidu.com/yebihaigsino/blog/item/36e4ea6f864743d281cb4ad9.html
Http://hi.baidu.com/devzhao/blog/item/3f527e435e91de149313c6b7.html
Method 1:
Exception detection method (based on the returned result form)
// Usage
Private void buttonok_click (Object sender, eventargs E)
{
If (codeboolisexcelinstalled ())
{
MessageBox. Show ("Excel files have been installed on the local machine ");
}
Else
{
MessageBox. Show ("No Excel files can be executed in the current system. To use the EXCEL function, install Office 2003", "warning", messageboxbuttons. OK, messageboxicon. Warning );
}
}
// Determine whether an Excel file is installed on the local machine
Private bool codeboolisexcelinstalled ()
{
Type type = type. gettypefromprogid ("Excel. application ");
Return type! = NULL;
}
========================================================== ======================================
Method 2:
Registry check
Check whether the registry contains SOFTWARE \ Microsoft \ Office \ 12.0 \ word \ installroot \ excel.exe, where 12.0 11.0 must be determined at the same time, because 11.0 is Office 2003 12.0 and Office 2007
// Usage
Private void buttonok_click (Object sender, eventargs E)
{
If (existsregedit ())
{
MessageBox. Show ("Excel files have been installed on the local machine ");
}
Else
{
MessageBox. Show ("No Excel files can be executed in the current system. To use the EXCEL function, install Office 2003", "warning", messageboxbuttons. OK, messageboxicon. Warning );
}
}
/// <Summary>
/// Self_variable: queries whether a key value in the registry exists.
/// </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 \\");
// Check whether office2003 is installed on the local machine
If (akey! = NULL)
{
String file03 = akey. getvalue ("path"). tostring ();
If (file. exists (file03 + "excel.exe "))
{
Ifused = true;
}
}
// Check whether the local machine has office2007 installed
If (akeytwo! = NULL)
{
String file07 = akeytwo. getvalue ("path"). tostring ();
If (file. exists (file07 + "excel.exe "))
{
Ifused = true;
}
}
Return ifused;
}
========================================================
String strinstallpath = file03/file07 + "excel.exe ";
// Obtain the Excel Process Information
System. Diagnostics. processstartinfo psinfo = new system. Diagnostics. processstartinfo (strinstallpath );
// Run Excel
System. Diagnostics. process. Start (strinstallpath );