C# 将excel工作簿表单始终保存在顶部c

C# 将excel工作簿表单始终保存在顶部c,c#,excel,save,C#,Excel,Save,我正在用c创建一个新的excel文件,最后我需要打开表单来保存它,但此时窗口在我的应用程序后面打开 有办法在上面打开它吗 这是我的密码: Excel.Application _xlApp; _xlApp.ActiveWindow.Activate(); _xlWorkBook.Close(true, Type.Missing, Type.Missing); _xlApp.Quit(); _xlApp = null; 为此,我们使用下一个helper类: public class Proces

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

有办法在上面打开它吗

这是我的密码:

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

为此,我们使用下一个helper类:

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
        {
        }
    }
}

您可以使用startassiciatedprocessassandbringittofront方法。

关键是我没有文件名,因为我在应用程序中创建文件,所以用户必须在保存文件之前选择文件名。