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

发布于 2024-11-10 15:41:19 字数 142 浏览 2 评论 0原文

是否可以从 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-11-17 15:41:19

此代码应该为您提供 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
    }

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
}

EDIT: 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
    }
捂风挽笑 2024-11-17 15:41:19

也许这可以工作(用 Excel 2013 ff 为我做这件事。)

try
{
    Type officeType = Type.GetTypeFromProgID("Excel.Application");
    object excelInstance = Activator.CreateInstance(officeType);
    if (excelInstance != null)
    {
        string results = officeType.InvokeMember("OperatingSystem", BindingFlags.GetProperty, null, excelInstance, null).ToString();
        if (!string.IsNullOrEmpty(results))
            detectedExcelPlatform = results.Contains("64")?EDetectExcelPlattform.Force64Bit:EDetectExcelPlattform.Force32Bit;
        officeType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, excelInstance, null);
    }
}
catch
{
    // To Ignore
}

EDetectExcelPlattform 并不重要,因为它仅来自我自己的代码。可以用 bool 结果替换。

Maybe this could work (do it for me with Excel 2013 ff.)

try
{
    Type officeType = Type.GetTypeFromProgID("Excel.Application");
    object excelInstance = Activator.CreateInstance(officeType);
    if (excelInstance != null)
    {
        string results = officeType.InvokeMember("OperatingSystem", BindingFlags.GetProperty, null, excelInstance, null).ToString();
        if (!string.IsNullOrEmpty(results))
            detectedExcelPlatform = results.Contains("64")?EDetectExcelPlattform.Force64Bit:EDetectExcelPlattform.Force32Bit;
        officeType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, excelInstance, null);
    }
}
catch
{
    // To Ignore
}

EDetectExcelPlattform doesn't matter, because it is only from my own code. Can be replaced with bool result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文