0

所以这就是我需要做的

有一个程序连接到某个服务器并以每秒大约 15-20 个条目的速度从中获取数据

在与服务器建立连接的那一刻,将打开一个 Excel 窗口,从服务器获取的所有条目都被动态移动到该 Excel 表中

然而,所有这些内容都保留在 RAM 中,即只有当我们尝试关闭该 Excel 窗口并出现“另存为..”对话框时才会创建 Excel 文件本身

正如您可能已经理解的那样,我不想每次都手动保存文件,因此我需要一些方法以编程方式将打开的窗口中的 Excel 表格保存到文件中

有什么办法吗?

4

1 回答 1

0

如果您没有在服务器上运行它并且您不介意手动触发 Excel 保存,那么您可以创建一些将使用 Interop 连接到 Excel 实例的东西。以下将连接到 Excel 实例并返回Excel.Workbook指定工作簿名称的对象:

private Excel.Workbook GetWorkbook(string workbookName)
{
    Excel.Window window = null;      // Excel window object from which application is grabbed
    Excel.Application app = null;    // Excel instance from which we get all the open workbooks
    Excel.Workbooks wbs = null;      // List of workbooks
    Excel.Workbook wb = null;        // Workbook to return
    EnumChildCallback cb;            // Callback routine for child window enumeration routine
    List<Process> procs = new List<Process>();           // List of processes

    // Get a full list of all processes that have a name of "excel"

    procs.AddRange(Process.GetProcessesByName("excel"));

    foreach (Process proc in procs)
    {
        // Make sure we have a valid handle for the window

        if ((int)proc.MainWindowHandle > 0)
        {
            // Get the handle of the child window in the current Excel process

            int childWindow = 0;
            cb = new EnumChildCallback(EnumChildProc);
            EnumChildWindows((int)proc.MainWindowHandle, cb, ref childWindow);

            // Make sure we got a valid handle

            if (childWindow > 0)
            {
                // Get the address of the child window so that we can talk to it and
                // get all the workbooks

                const uint OBJID_NATIVEOM = 0xFFFFFFF0;
                Guid IID_IDispatch =
                    new Guid("{00020400-0000-0000-C000-000000000046}");
                int res = AccessibleObjectFromWindow(childWindow, OBJID_NATIVEOM,
                    IID_IDispatch.ToByteArray(), ref window);

                if (res >= 0)
                {
                    app = window.Application;
                    wbs = app.Workbooks;

                    // Loop through all the workbooks within the current Excel window
                    // to see if any match

                    for (int i = 1; i <= wbs.Count; i++)
                    {
                        wb = wbs[i];

                        if (wb.Name == workbookName)
                        {
                            break;
                        }

                        wb = null;
                    }
                }
            }
        }

        // If we've already found our workbook then there's no point in continuing
        // through the remaining processes

        if (wb != null)
        {
            break;
        }
    }

    Release(wbs);
    Release(app);
    Release(window);

    return wb;
}

上面调用的Release()方法只是将引用设置为 null 并调用Marshal.FinalReleaseComObject()它们,否则你最终会得到到处都是无头的 Excel 实例。

您还需要以下内容来执行一些抓取窗口的功能:

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

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

[DllImport("Oleacc.dll")]
private static extern int AccessibleObjectFromWindow(int hwnd, uint dwObjectID, byte[] riid, ref Excel.Window ptr);

private bool EnumChildProc(int hwndChild, ref int lParam)
{
    // Get the name of the class that owns the passed-in window handle

    StringBuilder buf = new StringBuilder(128);
    GetClassName(hwndChild, buf, 128);

    // If the class name is EXCEL7 then we've got an valid Excel window

    if (buf.ToString() == "EXCEL7")
    {
        lParam = hwndChild;
        return false;
    }

    return true;
}

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

一旦你有了工作簿,你就可以打电话Workbook.SaveAs()来保存它。

于 2013-04-06T03:48:56.470 回答