是否可以从 Microsoft.Office.Interop.Excel.ApplicationClass 确定 Excel 是以 32 位还是 64 位运行?
编辑
该解决方案应该适用于 Excel 2010 和 Excel 2007
问问题
3662 次
2 回答
9
此代码应该为您提供 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
}
于 2011-05-31T13:11:41.940 回答
0
也许这可以工作(用 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 没关系,因为它只是来自我自己的代码。可以用布尔结果替换。
于 2021-11-29T15:47:21.107 回答