【问题标题】:Determine Excel Version/Culture via Microsoft.Office.Interop.Excel通过 Microsoft.Office.Interop.Excel 确定 Excel 版本/文化
【发布时间】:2010-05-26 15:58:07
【问题描述】:

如何在 .NET/C# 中实现这一点?

【问题讨论】:

    标签: c# .net excel office-interop


    【解决方案1】:

    您可以使用此代码 sn-p:(取自我的一个项目,因此不保证开箱即用)

    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;
    }
    

    【讨论】:

      【解决方案2】:

      阅读您的 app.config 文件,因为它将在其中放置参考:

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

      通过“阅读”,您可能必须打开文件并阅读内容,因为我认为您不能专门阅读程序集。

      【讨论】:

      • 您可以将反射命名空间与 Assembly myAss = Assembly.GetAssembly(type) 一起使用,而不是读取 app.config 文件,其中 type 是 Excel 程序集中的一种对象类型。 FullPath 属性将返回与 app.config 中存储的信息相同的信息,您只需对其进行解析即可。
      • 使用反射我可以确定 Excel 版本和文化,但我仍然需要了解 Office/Excel 安装语言。我怎样才能发现这一点?
      【解决方案3】:
       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!");
      
      
      
          }
      

      【讨论】:

      • 您能否详细说明一下并解释您的解决方案?如果你只是丢掉一段代码,它很可能会因为质量差而被删除。更好地解释你在做什么以及为什么这可以解决最初的问题。
      猜你喜欢
      • 2015-09-16
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多