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;
}
RegistryKey bkey = rk. OpenSubKey (@ "SOFTWARE \ Microsoft \ Office \ 9.0 \ Word \ InstallRoot \\");
// Check whether Office2003 is installed on the local machine
If (bkey! = Null)
{
String file00 = bkey. GetValue ("Path"). ToString ();
If (File. Exists (file00 + "Excel.exe "))
{
Ifused = true;
}
}
// Dynamically concatenate the connection string
DataSet ds = new DataSet (); string strConn = "";
If (ExistsRegedit03 ())
{
StrConn = "Provider = Microsoft. Jet. OLEDB.4.0;" + "Data Source =" + strpath + ";" + "Extended Properties = Excel 8.0 ;";
} Else if (ExistsRegedit07 ())
{
StrConn = "Provider = Microsoft. Ace. OLEDB.12.0;" + "Data Source =" + strpath + ";" + "Extended Properties = Excel 12.0 ;";
}
OleDbConnection conn = new OleDbConnection (strConn );
OleDbDataAdapter myCommand = new OleDbDataAdapter ("SELECT * FROM [RelationerTable $]", strConn );
MyCommand. Fill (ds );
DataTable dt = ds. Tables [0];
// Dt is the set of read data.