2010-05-26 71 views

回答

0

閱讀您的app.config文件,因爲它會放置在那裏的參考:

<compilation debug="false"> 
    <assemblies> 
     <add assembly="Office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/> 
    </assemblies> 
</compilation> 

通過「閱讀」,您可能需要打開文件並讀取其中的內容,因爲我不認爲你可以專門讀取組件。

+0

而不是閱讀,你可以使用反射app.config文件名稱空間與大會myAss = Assembly.GetAssembly(type)其中type是從Excel程序集中的一種對象。 FullPath屬性將返回與app.config中存儲的信息相同的信息,您可以解析該信息。 – Jay 2010-05-26 19:20:00

+0

使用反思我可以確定Excel的版本和文化,但我仍然需要了解Office/Excel安裝語言。我如何發現? – DotNetter 2010-05-27 08:08:04

6

你可以使用此代碼段:(從我的一個項目採取的,所以不能保證工作的開箱)

Microsoft.Office.Interop.Excel.Application tExcel = new Application(); 
CultureInfo cSystemCulture = Thread.CurrentThread.CurrentCulture; 
CultureInfo cExcelCulture = new CultureInfo(tExcel.LanguageSettings.get_LanguageID(
    Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI)); 

try 
{ 
    Thread.CurrentThread.CurrentCulture = cExcelCulture; 
    double tVersion; 
    bool tParseSucceded = double.TryParse(tExcel.Version, out tVersion); 

    // 12 is the first version with .xlsx extension 
    if (tVersion > 11.5) 
     cDefaultExtension = ".xlsx"; 
    else 
     cDefaultExtension = ".xls"; 

} 
catch (Exception aException) 
{ 
    cLogger.Debug("error retrieving excel version.", aException); 
    cLogger.Error("error retrieving excel version."); 
} 
finally 
{ 
    Thread.CurrentThread.CurrentCulture = cSystemCulture; 
} 
0
void Method1() 
    { 
     string strEVersionSubKey = "\\Excel.Application\\CurVer"; //HKEY_CLASSES_ROOT/Excel.Application/Curver 
     string strValue = null; 
     string strVersion = null; 
     RegistryKey rkVersion = null; 

     rkVersion = Registry.ClassesRoot.OpenSubKey(strEVersionSubKey, false); 


     strValue = (string)rkVersion.GetValue(string.Empty); 

     strValue = strValue.Substring(strValue.LastIndexOf(".") + 1); 


     switch (strValue) //Determine Version 
     { 
      case "7": 
       strVersion = "95"; 
       break; 

      case "8": 
       strVersion = "97"; 
       break; 

      case "9": 
       strVersion = "2000"; 
       break; 

      case "10": 
       strVersion = "2002"; 
       break; 

      case "11": 
       strVersion = "2003"; 
       break; 

      case "12": 
       strVersion = "2007"; 
       break; 

      case "14": 
       strVersion = "2010"; 
       break; 

      case "15": 
       strVersion = "2013"; 
       break; 

      case "16": 
       strVersion = "2016"; 
       break; 
     } 

     MessageBox.Show("Excel " + strVersion + " Installed!"); 



    } 
+0

您能詳細解釋一下並解釋您的解決方案嗎?如果您只刪除了一段代碼,它很可能會因質量差而被刪除。更好地解釋你在做什麼以及爲什麼這可能解決最初的問題。 – user1438038 2015-12-23 12:54:08