Check the internal version of Excel installed in .NET
Operating environment
- Visual Studio
-
- Visual Studio 2022
- .NET
-
- .NET 6.0
- Windows
-
- Windows 11
- Excel
-
- Microsoft 365
Prerequisites
- Windows
-
- One of the versions
- Excel
-
- One of the versions
About Internal Versions
This time, you can check the internal version of Excel programmatically, but as of 2023/05/07, the internal version of Excel 2016 and later has not changed from 16.0. As a result, it is no longer possible to identify Excel 2016, 2019, 2021, and Microsoft 265 by their internal versions. However, since the internal version has not changed, the advantage is that as long as a new version does not come out, the Excel library can be used in common since 2016.
About the operating program
You can find out which internal version of Excel is installed with the following code: Since it is a method, it is possible to bring it to your own program as it is.
GetInstallExcelVersion
If you call the method, the version will be returned as a number.
<summary>
インストールされている Excel の内部バージョンを取得します。
</summary>
<returns>インストールされている Excel の内部バージョン。正常に取得できなかった場合は 0 を返します。</returns>
static decimal GetInstallExcelVersion()
{
// Excel.Application の Type を取得
var type = Type.GetTypeFromProgID("Excel.Application");
if (type == null)
{
// Excel がインストールされていない
return 0;
}
object? application = null;
try
{
// Excel.Application のインスタンスを生成します
application = Activator.CreateInstance(type);
if (application == null)
{
// Excel.Application の生成に失敗
return 0;
}
// バージョンを取得
var ver = application.GetType().InvokeMember("Version", System.Reflection.BindingFlags.GetProperty, null, application, null);
if (ver == null)
{
return 0;
}
// 文字列を数値に変換します
decimal version;
if (!decimal.TryParse(ver.ToString(), out version))
{
return 0;
}
return version;
}
finally
{
// 生成した Excel のリソースを解放します
if (application != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
}
}
}
A simple explanation of the program is to use the method to Excel.Application
get the type ofType.GetTypeFromProgID
program ID,
Activator.CreateInstance
The method creates an Excel.Application
instance of .
Excel.Application
The reason for not generating a direct instance of is to avoid relying on the version of the Excel library by referencing it directly.
By doing this, you will be able to see the latest version.
You can then refer to Version
the properties from the generated application
instance and get the version.
The version that is retrieved is the version that Excel has internally. For example, Excel 2010 is 14.0, Excel 2013 is 15.0, and Excel 2016 and later are 16.0.
Note that this process is no different from actually starting Excel, so if you do not call the method and Excel.Application
release , System.Runtime.InteropServices.Marshal.ReleaseComObject
the Excel process will remain.
By the way, this code has been tested on .NET 6, but I think it can be used in the .NET Framework in much the same way.
It works in .NET, but Type.GetTypeFromProgID
keep in mind that the methods and methods are Windows-only code and System.Runtime.InteropServices.Marshal.ReleaseComObject
will not work on other platforms.