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

14,301

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;
}
Share:
14,301
DonKotlino
Author by

DonKotlino

Updated on June 17, 2022

Comments

  • DonKotlino
    DonKotlino almost 2 years

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

  • Jay
    Jay almost 14 years
    Instead of reading the app.config file you could use reflection name space with Assembly myAss = Assembly.GetAssembly(type) where type is a type of object from the Excel assembly. The FullPath property would return the same information as is stored in the app.config and you could just parse that.
  • DonKotlino
    DonKotlino almost 14 years
    Using reflection I could determine the Excel version and culture but I still need to know the Office/Excel installation language. How can I discover that?
  • user1438038
    user1438038 over 8 years
    Could you elaborate a bit more and explain your solution? It is very likely to get deleted for poor quality, if you just drop a block of code. Better explain what you are doing and why this might solve the initial problem.