Printable Version of Topic

Click here to view this topic in its original format

The Ring of Wonder _ Θεός Μηχανή 『神之机器』 _ Lua入门:实例一

Posted by: bx_bob 2011-08-03, 13:55

CODE
--excel and trow
require("luacurl")
require("luacom")
--从trow论坛页面获取数据
html_result=""
fakebrowser=curl.new()
fakebrowser:setopt(curl.OPT_URL,"http://trow.cc/forum/index.php?act=idx")
--[[代理
fakebrowser:setopt(curl.OPT_PROXY,"proxy.chn.xerox.com")
fakebrowser:setopt(curl.OPT_PROXYPORT,8000)
]]
fakebrowser:setopt(curl.OPT_WRITEFUNCTION,function(i,buffer) html_result=html_result..buffer return #buffer end)
fakebrowser:perform()
fakebrowser:close()
--从数据中抽取需要的内容:游客数量,注册用户数量,隐身用户数量
guests_num,members_num,anony_num=string.match(html_result,"\n.*<b>(.-)</b> guests, <b>(.-)</b> members <b>(.-)</b> anonymous members")
print(guests_num,members_num,anony_num)
--创建一个Excel OLE对象
excel = luacom.CreateObject("Excel.Application")
assert(excel)
--对Excel OLE各种操作,加入数据并创建一个饼图
excel.Visible=true
book=excel.Workbooks:Add()
sheet=book.Worksheets(1)
sheet.Cells(1,1).Value2="游客"
sheet.Cells(1,2).Value2="注册用户"
sheet.Cells(1,3).Value2="隐身用户"
sheet.Cells(2,1).Value2=guests_num
sheet.Cells(2,2).Value2=members_num
sheet.Cells(2,3).Value2=anony_num

chart=excel.Charts:Add()
chart.ChartType=5 --饼图
range=sheet:Range("A1:C2")
chart:SetSourceData(range)
代码的作用是,从trow论坛获取当前在线游客,注册用户,隐身用户的数量,并创建一个Excel文件,制作一个饼状图。
Attached Image

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)