.net - 从 Excel 应用程序对象中查找位数(32 位/64 位)?

标签 .net excel 32bit-64bit excel-interop

是否可以从 Microsoft.Office.Interop.Excel.ApplicationClass 确定 Excel 是以 32 位还是 64 位运行?

编辑
该解决方案应适用于 Excel 2010 和 Excel 2007

最佳答案

此代码应该为您提供 Excel 的“位数”。

Microsoft.Office.Interop.Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (System.Runtime.InteropServices.Marshal.SizeOf(app.HinstancePtr) == 8)
{
    // excel 64-bit
}
else
{
    // excel 32-bit
}

编辑:这是另一个版本,也适用于以前版本的 Excel。只需传递一个 ApplicationClass 引用给它即可:

    public static ExcelVersion GetExcelVersion(object applicationClass)
    {
        if (applicationClass == null)
            throw new ArgumentNullException("applicationClass");

        PropertyInfo property = applicationClass.GetType().GetProperty("HinstancePtr", BindingFlags.Instance | BindingFlags.Public);
        if (property == null)
            return ExcelVersion.Excel;

        return (System.Runtime.InteropServices.Marshal.SizeOf(property.GetValue(applicationClass, null)) == 8) ? ExcelVersion.Excel2010_64 : ExcelVersion.Excel2010_32;
    }

    public enum ExcelVersion
    {
        Excel, // before 2010, so 32 bits
        Excel2010_32,
        Excel2010_64
    }

关于.net - 从 Excel 应用程序对象中查找位数(32 位/64 位)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6187565/

相关文章:

c# - 关于 ASP.NET Web API - 异步和等待

c# - Windows 时间戳格式(MOUSEMOVEPOINT 返回值的类型)

excel - 在 vba 上将值粘贴到另一个工作簿工作表上时出现问题

c++ - 无法用 64 位 g++ 编译 32 位

windows - 如何从 VB 6 应用程序确定 Windows 版本?

.net - 当应用程序终止时,我可以安全地依赖线程中的 IsBackground 吗?

c# - .Net HttpListener.GetContext() 给客户端 "503 Service Unavailable"

excel - 创建数据透视表的过程或调用无效

excel - 如何修复 hh :mm format

matlab - 在同一台机器上使用 Matlab 32 位和 64 位,如何将设置存储在不同的地方?