/**** Requires PeHeaderReader to determine bitness: http://code.cheesydesign.com/?p=572 ****/ using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Security.AccessControl; /// /// See: http://support.microsoft.com/kb/240794 /// public class ExcelHelper { public enum ExcelVersions { /* Office 97 - 7.0 Office 98 - 8.0 Office 2000 - 9.0 Office XP - 10.0 Office 2003 - 11.0 Office 2007 - 12.0 Office 2010 - 14.0 (sic!) Office 2013 - 15.0 */ Excel_Pre2003 = 0, Excel_2003 = 11, Excel_2007 = 12, Excel_2010 = 14, Excel_2013 = 15, Excel_2016 = 16, Unknown = 99 } private ExcelVersions Version = ExcelVersions.Unknown; public ExcelHelper(ExcelVersions version) { Version = version; } public string ExcelFilePath { get { var versionNumber = (int)Version; var keyPath = string.Format(@"Software\Microsoft\Office\{0}.0\Excel\InstallRoot", versionNumber); RegistryKey hklmWow32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32); RegistryKey key = hklmWow32.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey); if (key != null) { var path = key.GetValue("Path"); if (path != null) { path = Path.Combine(path.ToString(), "excel.exe"); if (File.Exists(path.ToString())) return path.ToString(); else throw new Exception("Excel path specified in Registry not found on disk: " + path); } throw new Exception("InstallRoot key doesn't contain Path sub-key"); } hklmWow32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); key = hklmWow32.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey); if (key != null) { var path = key.GetValue("Path"); if (path != null) { path = Path.Combine(path.ToString(), "excel.exe"); if (File.Exists(path.ToString())) return path.ToString(); else throw new Exception("Excel path specified in Registry not found on disk: " + path); } throw new Exception("InstallRoot key doesn't contain Path sub-key"); } throw new Exception("Excel file path (InstallRoot key) not found"); } } public bool ExcelIs32bit { get { var file = ExcelFilePath; if (string.IsNullOrWhiteSpace(file)) throw new Exception("Excel path could not be determined."); if (!File.Exists(file)) throw new Exception("Excel path is incorrect."); PeHeaderReader pe = new PeHeaderReader(file); return pe.Is32BitHeader; } } public ExcelVersions ExcelVersion { get { return Version; } } internal int ExcelVersionNumber { get { return (int)Version; } } public static string ExcelFullName(ExcelVersions version, bool is32bit) { var fullName = ""; if (version == ExcelVersions.Excel_2003) fullName = "Excel 2003"; else if (version == ExcelVersions.Excel_2007) fullName = "Excel 2007"; else if (version == ExcelVersions.Excel_2010) fullName = "Excel 2010"; else if (version == ExcelVersions.Excel_2013) fullName = "Excel 2013"; else if (version == ExcelVersions.Excel_2016) fullName = "Excel 2016"; else if (version == ExcelVersions.Excel_Pre2003) fullName = "(NOT SUPPORTED): Excel pre-2003"; fullName += is32bit ? " (32bit)" : " (64bit)"; return fullName; } public static List GetAllInstalledVersionsOfExcel() { var results = new List(); if (ExcelVersionIsInstalled(ExcelVersions.Excel_2003)) results.Add(ExcelVersions.Excel_2003); if (ExcelVersionIsInstalled(ExcelVersions.Excel_2007)) results.Add(ExcelVersions.Excel_2007); if (ExcelVersionIsInstalled(ExcelVersions.Excel_2010)) results.Add(ExcelVersions.Excel_2010); if (ExcelVersionIsInstalled(ExcelVersions.Excel_2013)) results.Add(ExcelVersions.Excel_2013); if (ExcelVersionIsInstalled(ExcelVersions.Excel_2016)) results.Add(ExcelVersions.Excel_2016); return results; } private static bool ExcelVersionIsInstalled(ExcelVersions version) { var versionNumber = (int)version; var keyPath = string.Format(@"Software\Microsoft\Office\{0}.0\Excel\InstallRoot", versionNumber); RegistryKey hklmWow32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32); RegistryKey key = hklmWow32.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey); if (key != null) { var path = key.GetValue("Path"); if (path != null) { path = Path.Combine(path.ToString(), "excel.exe"); return File.Exists(path.ToString()); } } //////////////// hklmWow32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); key = hklmWow32.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey); if (key != null) { var path = key.GetValue("Path"); if (path != null) { path = Path.Combine(path.ToString(), "excel.exe"); return File.Exists(path.ToString()); } } return false; } }