使用Lua与Excel一起工作

我打算学习我的桌面脚本需求的Lua。 我想知道是否有任何文档可用,以及标准库中是否有所需的东西。

你应该看看Lua for Windows–一个在Windows上用于Lua脚本语言的“包含电池的环境”

http://luaforwindows.luaforge.net/

它包含了可以从中访问Excel COM对象的LuaCOM库。

试着看看LuaCOM文档,那里有一些Excel例子:

http://www.tecgraf.puc-rio.br/~rcerq/luacom/pub/1.3/luacom-htmldoc/

我只用过这个非常简单的东西。 这里是一个示例,让你开始:

-- test.lua require('luacom') excel = luacom.CreateObject("Excel.Application") excel.Visible = true wb = excel.Workbooks:Add() ws = wb.Worksheets(1) for i=1, 20 do ws.Cells(i,1).Value2 = i end 

lua与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 for col=1, 30 do sheet.Cells(row, col).Value2 = math.floor(math.random() * 100) end end 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 

再比如,可以添加一个图表图表。

 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)