Skip to main content
 首页 » 编程设计

c#之comObject 可以转换为 Excel.ApplicationClass 吗

2024年02月23日24daizhj

我正在使用

(Excel.Application)Marshal.GetActiveObject("Excel.Application") 

它返回一个 _ComObject 类型,但我想知道我如何才能将其转换为 Excel.ApplicationClass,我将如何去做?

请您参考如下方法:

知识库文章 here定义以下将起作用。

//Excel Application Object 
Excel.Application oExcelApp; 
 
this.Activate(); 
 
//Get reference to Excel.Application from the ROT. 
oExcelApp =  (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); 
 
//Display the name of the object. 
MessageBox.Show(oExcelApp.ActiveWorkbook.Name); 
 
//Release the reference. 
oExcelApp = null;