如何强制将 Excel 窗口置于前台?

11

我有一个用C# .NET开发的小应用程序,它可以操作Excel表格。但是,一些用户告诉我,在打开Excel文件时,窗口没有出现在最前面,尽管我已将其可见性设置为真并将窗口状态设置为最大化。

这是读取Excel文件的函数:

public static void OpenExcel(string fileName, bool visibility, FunctionToExecute fn = null)
{
    string addInPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft\\AddIns\\mDF_XLcalendar.xla");

    deleg = fn;
    app = new Excel.Application();

    app.Workbooks.Open(addInPath);
    app.Workbooks.Open(fileName);

    app.ScreenUpdating = true;
    app.DisplayAlerts = true;
    app.Visible = visibility;
    app.UserControl = true;
    app.WindowState = Excel.XlWindowState.xlMaximized;

    EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(application_WorkbookBeforeClose);
    EventSave_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Open_ExcelApp_WorkbookBeforeSave);

    app.WorkbookBeforeClose += EventDel_BeforeBookClose;
    app.WorkbookBeforeSave += EventSave_BeforeBookClose;     
} 

有任何想法吗?


是因为您在打开工作簿之后才设置了“WindowState”吗? - gunr2171
1
如果您的 .Net 代码在 Excel 应用程序打开后仍有一些代码需要执行,则它会将焦点设置回您的程序。如果您的程序具有 GUI,则它将位于 Excel 顶部。因此,您可以尝试的一件事是将打开 Excel 的行移动到代码末尾。 - Jafar Kofahi
1
我尝试了这个,但它没有起作用。问题是我无法在我的机器上重现这个问题,因为它在我的机器上运行良好... - Nabil Laarif
我打开了一个Excel应用程序,以及2个Excel工作簿并将其最小化, 当我尝试通过工作簿名称激活我想要的工作簿,然后使用app.activewindow.activate命令时,它并不总是将我想要激活的工作簿带到活动状态,请帮忙解决。 - Chandraprakash
6个回答

11

一些对我有效的魔法:

app.WindowState = XlWindowState.xlMinimized; // -4140
app.WindowState = XlWindowState.xlMaximized; // -4137

这个答案的好处是,即使应用程序还没有打开工作簿,它也可以正常工作。 - René Nyffenegger

7

我发现这个方法有效。 如何将Excel应用程序置于前台

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);   

public static void BringExcelWindowToFront(Application xlApp)
{

   string caption = xlApp.Caption;
   IntPtr handler = FindWindow(null, caption);
   SetForegroundWindow(handler);
}

5

我会尝试通过以下方式激活Excel窗口:

app.ActiveWindow.Activate();

如果这不起作用,您可能会在此线程中找到其他解决方案(涉及本地WinAPI调用):http://social.msdn.microsoft.com/

4
有时在Excel中同时打开多个文件时,ActiveWindow.Activate()可能不起作用。
一个成功的技巧是使用最小化和最大化命令。

2
有点晚了,但为什么需要使用FindWindow呢?Windows句柄可以通过应用程序访问:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

public static void BringExcelWindowToFront(Application xlApp)
{
   SetForegroundWindow((IntPtr)xlApp.Hwnd);  // Note Hwnd is declared as int
}

如果多个窗口具有相同的标题,那么不会出现任何问题。


0

激活特定的工作簿:

  [DllImport("user32.dll")]
  [return: MarshalAs(UnmanagedType.Bool)]
  static extern bool SetForegroundWindow(IntPtr hWnd);

  public static void BringExcelWindowToFront(Excel._Workbook xlBook)
  {
     var windows = xlBook.Windows;
     foreach (Excel.Window window in windows)
     {
        IntPtr handler = (IntPtr)window.Hwnd;

        SetForegroundWindow(handler);
        return;
     }
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接