强制将excel窗口置于最前面?

问题描述:

我有一个用C# .NET开发的小型应用程序,可以处理excel表格,我不知道为什么有些用户不断告诉我,尽管他们设置了可见为真,窗口状态为最大化.

i have a small application developed in C# .NET that manipulate excel sheets, I don't know why some users keep telling me that when they open the excel file the window doesn't appear on front/top although I set the visible to true and the window state on maximized.

这是读取 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;     
} 

有什么想法吗?

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

I would try to activate the excel window by

app.ActiveWindow.Activate();

如果这不起作用,您可能会在

If this doesn't work you might find other solutions (involving Native WinAPI calls) at this thread at http://social.msdn.microsoft.com/