简体   繁体   中英

Lua WinAPI - Read cell from Microsoft Excel

I am trying to read values from cells of a running Excel 2007 document. I am using the WinAPI extension for Lua . I am currently running on a Windows XP computer (if that matters, but it seems the WinAPI works from XP on.

This is my Excel document:

高强

This is my Lua code:

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

This is my Lua code output:

代码输出

Does anyone know how I recursively should enumerate in order to find each Cell? Or is there a better way to go about this? The text I want is the "1234". I have not done much Windows programming since I prefer Unix, but it seems like I am on the right track. I just don't know how to progress from here!

I doubt that Excel uses a window for each and every cell in the spreadsheet, so trying to get to the cell by navigating the window hierarchy is likely a dead end.

If possible, I'd recommend exporting the data to a common file type, like comma-separated values, and making your program parse that.

Alternatively, you can read about UI Automation , which is how things like screen readers can access the data in a Windows application's UI. I've never done that, but it looks like it might be a lot of work. Nevertheless, I believe it's the most robust, supported method for trying to get data out of a Windows application via its user interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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