lua版本:lua5.1
windows
依赖库 luacom.dll (可以从 lua官网下载 LuaDist 自带的luacom)
local luacom = require("luacom")
local M = {}
function M:readexcel(filename)
local excel = luacom.CreateObject("Excel.Application")
-- excel.Visible = false -- 不显示窗口
local book = excel.Workbooks:Open(filename)
local sheet = book.Sheets(self._sheet) -- 第一个sheet 或 Sheets('data')
local info = {}
for i = 1, sheet.UsedRange.Rows.Count, 1 do
local row = {}
for j = 1, sheet.UsedRange.Columns.Count, 1 do
local str = tostring(sheet.Cells(i, j).Value2)
-- str = utfgbk.g2u(str)
table.insert(row, str)
end
-- P(i, row)
table.insert(info, row)
break
end
excel.Application:Quit()
excel:Quit()
return info
end
function M:main()
local info = self:readexcel("test.xlsx") -- 注意,路径不要用中文
end
注意:
- xlsx路径不支持中文
- 读取中文数据时,长度不对.