lua运行外部程序_Lua通过COM调用外部程序excel及调用windows api

为了方便起见,最好安装lua for windows,里面已经包含了很多有用的第三方模块。

require(’luacom’) — luacom

ie = luacom.CreateObject(”InternetExplorer.Application”)

ie:Navigate2(”http://sunxiunan.com”)

ie.Visible = true

使用lua调用excel,然后往cell里面填一些数据。

require(’luacom’) — luacom

– Excelの起動

excel = luacom.CreateObject(”Excel.Application”)

excel.Visible = true — 可視状態に

– ワークブックを追加

local book  = excel.Workbooks:Add()

local sheet = book.Worksheets(1)

– 適当な値を100個書き込む

for row=1,100 do

sheet.Cells(row, 1).Value2 = math.floor(math.random() * 20)

end

稍微复杂一些的代码

require “luacom”

excel = luacom.CreateObject(”Excel.Application”)

local book  = excel.Workbooks:Add()

local sheet = book.Worksheets(1)

excel.Visible = true

– 適当な値を書き込む

for row=1,30 do

for col=1,30 do

sheet.Cells(row, col).Value2 = math.floor(math.random() * 100)

end

end

– 値を調べて50以上のものを黄色でマークする

local range = sheet:Range(”A1″)

for row=1,30 do

for col=1,30 do

local v = sheet.Cells(row, col).Value2

if v > 50 then

local cell = range:Offset(row-1, col-1)

cell:Select()

excel.Selection.Interior.Color = 65535

end

end

end

excel.DisplayAlerts = false — 終了確認を出さないようにする

excel:Quit()

excel = nil

如果想给excel加个图表该怎么做?

require “luacom”

excel = luacom.CreateObject(”Excel.Application”)

local book  = excel.Workbooks:Add()

local sheet = book.Worksheets(1)

excel.Visible = true

for row=1,30 do

sheet.Cells(row, 1).Value2 = math.floor(math.random() * 100)

end

local chart = excel.Charts:Add()

chart.ChartType = 4 — xlLine

local range = sheet:Range(”A1:A30″)

chart:SetSourceData(range)

如果想调用windows api,可以用下面的代码

require “alien”

MessageBox = alien.User32.MessageBoxA

MessageBox:types{ret = ‘long’, abi = ’stdcall’, ‘long’, ’string’,

’string’, ‘long’ }

MessageBox(0, “title for test”, “LUA call windows api”, 0)

如何实现回调函数呢?下面的例子展示了回调。

require ‘alien’

–声明了两个函数EnumWindows和GetClassName

EnumWindows = alien.user32.EnumWindows

EnumWindows:types {”callback”, “pointer”, abi=”stdcall”}

GetClassName = alien.user32.GetClassNameA

GetClassName:types {”long”, “pointer”, “int”, abi=”stdcall” }

local buf = alien.buffer(512)

– 会被EnumWindows反复调用,传入windows的handle

local function enum_func(hwnd, p)

GetClassName(hwnd, buf, 511)

print (hwnd..”:”..tostring(buf))

return 1

end

local callback_func = alien.callback(

enum_func,

{”int”, “pointer”, abi=”stdcall”})

EnumWindows(callback_func, nil)

其中函数原型是BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);int GetClassName(HWND hWnd, LPTSTR lpClassName, int nMaxCount);其中EnumWindows第一个参数的原型为,这个函数是客户调用时候传入,EnumWindows用它返回BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);其他复杂的使用方法可以参考alien的文档。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值