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