Determine Excel Version/Culture via Microsoft.Office.Interop.Excel

前端 未结 3 2038
我寻月下人不归
我寻月下人不归 2020-12-04 00:47

How can I achieve that in .NET/C#?

相关标签:
3条回答
  • 2020-12-04 01:09

    Read your app.config file as it will place the reference in there:

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

    By "Read", you may have to open the file and read the contents as I don't think that you can specifically read Assemblies.

    0 讨论(0)
  • 2020-12-04 01:29
     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 讨论(0)
  • 2020-12-04 01:32

    You could use this code snippet: (taken from one of my projects, so not guaranteed to work out of the box)

    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 讨论(0)
提交回复
热议问题