您的位置:首页 > 编程语言 > Lua

Lua通过COM调用外部程序excel及调用windows api

2009-08-18 12:41 666 查看
fromhttp://sunxiunan.com/?p=1258为了方便起见,最好安装luaforwindows,里面已经包含了很多有用的第三方模块。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—可視状態に
–ワークブックを追加
localbook=excel.Workbooks:Add()localsheet=book.Worksheets(1)
–適当な値を100個書き込む
forrow=1,100do
sheet.Cells(row,1).Value2=math.floor(math.random()*20)
end稍微复杂一些的代码require“luacom”
excel=luacom.CreateObject(”Excel.Application”)
localbook=excel.Workbooks:Add()
localsheet=book.Worksheets(1)
excel.Visible=true–適当な値を書き込む
forrow=1,30do
forcol=1,30do
sheet.Cells(row,col).Value2=math.floor(math.random()*100)
end
end
–値を調べて50以上のものを黄色でマークする
localrange=sheet:Range(”A1″)
forrow=1,30doforcol=1,30do
localv=sheet.Cells(row,col).Value2
ifv>50then
localcell=range:Offset(row-1,col-1)
cell:Select()
excel.Selection.Interior.Color=65535
end
endendexcel.DisplayAlerts=false—終了確認を出さないようにするexcel:Quit()
excel=nil如果想给excel加个图表该怎么做?require“luacom”
excel=luacom.CreateObject(”Excel.Application”)
localbook=excel.Workbooks:Add()
localsheet=book.Worksheets(1)
excel.Visible=trueforrow=1,30do
sheet.Cells(row,1).Value2=math.floor(math.random()*100)
endlocalchart=excel.Charts:Add()
chart.ChartType=4—xlLine
localrange=sheet:Range(”A1:A30″)chart:SetSourceData(range)如果想调用windowsapi,可以用下面的代码require“alien”MessageBox=alien.User32.MessageBoxA
MessageBox:types{ret=‘long’,abi=’stdcall’,‘long’,’string’,
’string’,‘long’}MessageBox(0,“titlefortest”,“LUAcallwindowsapi”,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”}localbuf=alien.buffer(512)–会被EnumWindows反复调用,传入windows的handle
localfunctionenum_func(hwnd,p)GetClassName(hwnd,buf,511)
print(hwnd..”:”..tostring(buf))
return1
end
localcallback_func=alien.callback(
enum_func,
{”int”,“pointer”,abi=”stdcall”})EnumWindows(callback_func,nil)其中函数原型是
BOOLEnumWindows(WNDENUMPROClpEnumFunc,LPARAMlParam);
intGetClassName(HWNDhWnd,LPTSTRlpClassName,intnMaxCount);
其中EnumWindows第一个参数的原型为,这个函数是客户调用时候传入,EnumWindows用它返回
BOOLCALLBACKEnumWindowsProc(HWNDhwnd,LPARAMlParam);
其他复杂的使用方法可以参考alien的文档。

这些代码都来自www.hakkaku.net/articles/20090615-459
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: