保存excel工作簿表单总是在顶部c#

时间:2014-03-10 08:14:48

标签: c# excel save

我正在使用c#创建一个新的excel文件,最后我需要打开表单来保存它,但此时窗口会在我的应用程序后面打开。

有没有办法在顶部打开它?

这是我的代码:

Excel.Application _xlApp; 
_xlApp.ActiveWindow.Activate();
_xlWorkBook.Close(true, Type.Missing, Type.Missing);
_xlApp.Quit();
_xlApp = null;

1 个答案:

答案 0 :(得分:0)

我们为此目的使用下一个辅助类:

public class ProcessManager
{
    private IntPtr _buffer = IntPtr.Zero;
    private IntPtr _hwnd = IntPtr.Zero;
    private string _filenameMarker;

    public void StartAssiciatedProcessAndBringItToFront(string fileName, string fileNameMarker)
    {
        if (string.IsNullOrEmpty(fileName))
            throw new ArgumentException();

        _filenameMarker = fileNameMarker;

        try
        {
            Process.Start(fileName);

            if (!string.IsNullOrEmpty(_filenameMarker))
            {
                _buffer = IntPtr.Zero;
                _hwnd = IntPtr.Zero;

                _buffer = Marshal.AllocHGlobal(512);
                NativeHelpers.EnumWindows(new NativeHelpers.EnumWindowsProc(searcher), IntPtr.Zero);

                if (_hwnd != IntPtr.Zero)
                {
                    BringToFront(_hwnd);
                }
            }
        }
        finally
        {
            if (_buffer != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(_buffer);
                _buffer = IntPtr.Zero;
            }

            _hwnd = IntPtr.Zero;
            _filenameMarker = null;
        }
    }

    private int searcher(IntPtr hWnd, IntPtr lParam)
    {
        int result;
        int ok = NativeHelpers.SendMessageTimeout(hWnd,
                                                  NativeHelpers.WM_GETTEXT,
                                                  new IntPtr(250),
                                                  _buffer,
                                                  NativeHelpers.SMTO_BLOCK | NativeHelpers.SMTO_ABORTIFHUNG,
                                                  100,
                                                  out result);
        if (ok == 0)
            return 1; // ignore this and continue

        if (result > 0)
        {
            string windowName = Marshal.PtrToStringAuto(_buffer);
            if (windowName.Contains(_filenameMarker))
            {
                _hwnd = hWnd;
                return 0; // stop search
            }
        }

        return 1; // continue
    }

    private static void BringToFront(IntPtr hwnd)
    {
        if (NativeHelpers.IsIconic(hwnd) != 0)
            NativeHelpers.ShowWindowAsync(hwnd, NativeHelpers.SW_RESTORE);
        NativeHelpers.SetForegroundWindow(hwnd);
    }

    public void StartAssiciatedProcessAndBringItToFront(string fileName)
    {
        if (string.IsNullOrEmpty(fileName))
            throw new ArgumentException();

        try
        {
            var p = Process.Start(fileName);
            p.WaitForInputIdle(5 * 1000);
            _hwnd = p.MainWindowHandle;

            if (_hwnd != IntPtr.Zero)
            {
                BringToFront(_hwnd);
            }
        }
        catch
        {
        }
    }
}

您可以使用StartAssiciatedProcessAndBringItToFront方法。