Lua WinAPI - 从Microsoft Excel读取单元格

时间:2013-06-10 19:49:01

标签: excel winapi lua

我正在尝试从正在运行的Excel 2007文档的单元格中读取值。我使用WinAPI extension作为Lua。我目前在Windows XP计算机上运行(如果这很重要,但似乎WinAPI可以在XP上运行。

这是我的Excel文档:

Excel

这是我的Lua代码:

require 'winapi'

w = winapi.find_window_match('Book1') -- Specify the name of the window

w:show() -- Set the visability

w:set_foreground() -- Bring this window to the foreground

handle = w:get_handle() -- Get window handle

t = {} -- Create a table

w:enum_children(function(w) table.insert(t,w) end) -- Enumerate all children

for k,v in pairs(t) do -- Print out all pairs in the table
    print("",k,"=",v)
end

这是我的Lua代码输出:

code output

有谁知道我递归应该如何枚举才能找到每个Cell?或者有更好的方法来解决这个问题吗?我想要的文字是“1234”。我没有做过很多Windows编程,因为我更喜欢Unix,但似乎我在正确的轨道上。我只是不知道如何从这里进步!

1 个答案:

答案 0 :(得分:1)

我怀疑Excel是否为电子表格中的每个单元格使用了一个窗口,因此尝试通过导航窗口层次结构来访问单元格可能是一个死胡同。

如果可能,我建议将数据导出为通用文件类型,例如以逗号分隔的值,并使程序解析该数据。

或者,您可以阅读UI Automation,这是屏幕阅读器可以访问Windows应用程序UI中的数据的方式。我从来没有这样做过,但看起来可能需要做很多工作。不过,我相信这是尝试通过其用户界面从Windows应用程序中获取数据的最强大,受支持的方法。

相关问题