0

我有以下问题。某些应用程序会每隔几秒自动打开一个 excel 文件并在其中写入数据。该 excel 文件是独占打开的,我无法从其他应用程序访问。单元格的内容变化很快,所以我需要在打开的文件中读取/写入一些单元格内容。当我说打开时,我的意思是 excel 应用程序是打开的,我正在查看它。

是否可以使用 ac# 应用程序中的 winapi 向打开的 excel 发送消息,以填充某个单元格或读取某个单元格。由于文件被锁定,我需要尝试这种方式,所以我正在考虑使用这种方式访问​​它。

获取 excel 窗口句柄,读取单元格,向单元格发送文本。

提前致谢

4

2 回答 2

0

If the instance of Excel is running on the same machine as your C# app then you can use the following to get that instance. I use it in order to grab a user's running instance of Excel in order to read data from their open spreadsheet.

private Excel.Application GetCurrentVersionOfExcel()
{
    Excel.Application xlApp;

    // Try to get the currently-running Excel application

    try
    {
        xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
    }
    catch
    {
        // In case there isn't a version running or the current version hasn't registered itself with the Running Object Table

        xlApp = null;
    }

    return xlApp;
}

If that doesn't quite get you there I also have code to go through all open workbook processes and find the one you're looking for. It's quite long so if it's something you think might work for you then let me know and I'll post it. Basically it works by grabbing window handles and checking the child windows to see if they're workbooks.

于 2012-11-16T22:08:42.773 回答
0

您可以尝试使用 DDE(动态数据交换)API 与 Excel 进行交互。例如,DDESend() 函数用于与 excel 进行 DDE 对话。如需更多信息,请访问

http://office.microsoft.com/en-us/access-help/ddesend-function-HA001228822.aspx

于 2012-11-15T18:11:25.343 回答