Accessing an open Excel Workbook in C#

前端 未结 5 1135
情话喂你
情话喂你 2020-12-09 06:51

I need to access an excel file that is already open. I thought just inspecting the .Workbooks property that it would be there but it isn\'t. What is the right w

5条回答
  •  盖世英雄少女心
    2020-12-09 07:40

    Instead of instantiating a new instance, check for an existing one:

    try
    {
      Microsoft.Office.Interop.Excel.Application app = 
          System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
    }
    catch
    {
      // Excel is not running.
    }
    

提交回复
热议问题