通过 Handle 使用 C# 获取 Excel 应用程序的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1118735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:38:04  来源:igfitidea点击:

Get instance of Excel application with C# by Handle

c#exceloffice-interop

提问by Babba

I have a c# simple application that have to write some values in a excel ranges of a specific worksheet. I create an instance of Excel application if not exist, but if exist i want to set active it and take an instance if it to use in my code.

我有一个 ac# 简单的应用程序,它必须在特定工作表的 excel 范围内写入一些值。如果不存在,我会创建一个 Excel 应用程序实例,但是如果存在,我想将它设置为活动状态,并在我的代码中使用一个实例。

I use this code to create a new application:

我使用此代码创建一个新应用程序:

Microsoft.Office.Interop.Excel app = 
   new Microsoft.Office.Interop.Excel.Application();
app.Visible = true;

To get the handle of active excel window i use this api

要获得活动 excel 窗口的句柄,我使用这个 api

[DllImportAttribute("User32.dll")]
private static extern int FindWindow(String ClassName, String WindowName);

How can i get an instance of excel application by an handle?

如何通过句柄获取excel应用程序的实例?

int hWnd = FindWindow(null, "Microsoft Excel - MySheet.xlsx");
Microsoft.Office.Interop.Excel app = ....(hWnd)

采纳答案by James

Use the following code to get the first running instance of Excel:

使用以下代码获取 Excel 的第一个运行实例:

oExcelApp =  (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

Example

例子

public Excel.Application StartExcel()
{
    Excel.Application instance = null;
    try
    {
       instance = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
       instance = new Excel.ApplicationClass();
    }

    return instance;
}

回答by Shiraz Bhaiji

You can use Marshal.GetActiveObject, see this blog post for details:

您可以使用 Marshal.GetActiveObject,详情请参阅此博客文章:

http://blogs.msdn.com/andreww/archive/2008/11/30/starting-or-connecting-to-office-apps.aspx

http://blogs.msdn.com/andreww/archive/2008/11/30/starting-or-connecting-to-office-apps.aspx

回答by Govert

There might be more than one Excel instance running.

可能有多个 Excel 实例在运行。

GetActiveObject(...) looks in the Running Object Table (ROT) and would give you the last Excel instance that was opened - not necessarily the one corresponding with the window handle you have.

GetActiveObject(...) 在运行对象表 (ROT) 中查找,并会为您提供最后一个打开的 Excel 实例 - 不一定是与您拥有的窗口句柄对应的实例。

You're looking for AccessibleObjectFromWindow(..). The Andrew Whitechapel postlinked to in the other answer shows how to use this function.

您正在寻找 AccessibleObjectFromWindow(..)。另一个答案中链接到的Andrew Whitechapel 帖子展示了如何使用此功能。

Another link - http://blogs.officezealot.com/whitechapel/archive/2005/04/10/4514.aspx.

另一个链接 - http://blogs.officezealot.com/whitechapel/archive/2005/04/10/4514.aspx