Welcome Guest ( Log In | Register )

欢迎访问本站。游客仅能浏览首页新闻、版块主题、维基条目与资源信息,需登录后方可获得内容发布、话题讨论、维基编辑与资源下载等权限。若无账号请先完成注册流程。
 
Reply to this topicStart new topic
> Lua入门:实例一, Trow与Excel
bx_bob
2011-08-03, 13:55
Post #1


比非主流还非主流的Lua原教旨主义机械师
Group Icon
 817
   15

Group: Avatar
Posts: 416
Joined: 2007-07-25
Member No.: 14794


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

This post has been edited by bx_bob: 2011-08-03, 13:55
TOP
Fast ReplyReply to this topicStart new topic
 


Time is now: 2021-09-17, 23:59