C# 获取到当前处于活跃状态的Excel对象(即已经打开的)c#获取打开的excel

 private List<Microsoft.Office.Interop.Excel.Workbook> GetExeclWorkbooks()
        {
            // 声明一个对象
            Excel._Application objExcel;
            List<Microsoft.Office.Interop.Excel.Workbook> workbooks = new List<Microsoft.Office.Interop.Excel.Workbook>();
            try
            {
                // 获取到当前处于活跃状态的Excel对象(即已经打开的)
                objExcel = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
                // 根据选择到的文件名去匹配工作对象
                foreach (Microsoft.Office.Interop.Excel.Workbook wb in objExcel.Workbooks)
                {
                    workbooks.Add(wb);
                }
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
            }
            return workbooks;
        }

  

posted @ 2022-03-15 11:41  上帝视角  阅读(1246)  评论(0编辑  收藏  举报