简体   繁体   English

如何迭代Excel c#的实例

[英]How to iterate through instance of Excel c#

I can access an instance of Excel in memory using Marshal.GetActiveObject. 我可以使用Marshal.GetActiveObject访问内存中的Excel实例。 But this always returns the oldest existing instance. 但这总是返回最早的现有实例。

I would like to iterate though all instances and be able to choose the one to link to. 我想迭代所有实例,并能够选择要链接到的实例。

Can anyone help with this please. 任何人都可以帮助这个。

Try this. 试试这个。

        List<Process> procs = new List<Process>();
        procs.AddRange(Process.GetProcessesByName("excel"));

Edit: There is an article that fully implements this at http://blogs.officezealot.com/whitechapel/archive/2005/04/10/4514.aspx . 编辑:有一篇文章在http://blogs.officezealot.com/whitechapel/archive/2005/04/10/4514.aspx上完全实现了这一点。 GetActiveObject will always return the first object from the table. GetActiveObject将始终从表中返回第一个对象。 This is because Office doesn't register new objects. 这是因为Office不会注册新对象。 You have to get the application from the child windows. 您必须从子窗口获取应用程序。

Edit: This is the code that worked for me. 编辑:这是适合我的代码。

using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{        

class Program
{
    [DllImport("Oleacc.dll")]
    public static extern int AccessibleObjectFromWindow(
          int hwnd, uint dwObjectID, byte[] riid,
          ref Microsoft.Office.Interop.Excel.Window ptr);

    public delegate bool EnumChildCallback(int hwnd, ref int lParam);

    [DllImport("User32.dll")]
    public static extern bool EnumChildWindows(
          int hWndParent, EnumChildCallback lpEnumFunc,
          ref int lParam);


    [DllImport("User32.dll")]
    public static extern int GetClassName(
          int hWnd, StringBuilder lpClassName, int nMaxCount);

    public static bool EnumChildProc(int hwndChild, ref int lParam)
    {
        StringBuilder buf = new StringBuilder(128);
        GetClassName(hwndChild, buf, 128);
        if (buf.ToString() == "EXCEL7")
        {
            lParam = hwndChild;
            return false;
        }
        return true;
    }

    static void Main(string[] args)
    {
        Excel.Application app = new Excel.Application();
        EnumChildCallback cb;
        List<Process> procs = new List<Process>();
        procs.AddRange(Process.GetProcessesByName("excel"));

        foreach (Process p in procs)
        {
            if ((int)p.MainWindowHandle > 0)
            {
                int childWindow = 0;
                cb = new EnumChildCallback(EnumChildProc);
                EnumChildWindows((int)p.MainWindowHandle, cb, ref childWindow);

                if (childWindow > 0)
                {
                    const uint OBJID_NATIVEOM = 0xFFFFFFF0;
                    Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
                    Excel.Window window = null;
                    int res = AccessibleObjectFromWindow(childWindow, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref window);
                    if (res >= 0)
                    {
                        app = window.Application;
                        Console.WriteLine(app.Name);
                    }
                }
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM