gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:04:01 29 4
gpt4 key购买 nike

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com