从 Excel 应用程序对象中查找位数(32 位/64 位)? [英] Find bitness (32-bit/64-bit) from Excel Application object?

查看:35
本文介绍了从 Excel 应用程序对象中查找位数(32 位/64 位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 Microsoft.Office.Interop.Excel.ApplicationClass 确定 Excel 是在 32 位还是 64 位下运行?

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

Is it possible to determine whether Excel is running in 32-bit or 64-bit from the Microsoft.Office.Interop.Excel.ApplicationClass?

Edit
The solution should work for both Excel 2010 and Excel 2007

推荐答案

这段代码应该给你 Excel 的位".

This code should give you the "bitness" of 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 引用:

here is another version that should work for previous versions of Excel as well. Just pass an ApplicationClass reference to it:

    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
    }

这篇关于从 Excel 应用程序对象中查找位数(32 位/64 位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆