我该如何使用luacom读取xlsx文件?

我有下面的代码

excel = luacom.CreateObject("Excel.Application")
 pcall(excel.Open, excel, "1.xlsx")
if excel ~= nil then
    sheets = excel.Worksheets
    sheet1 = sheets:Item(1)

    for row=1, 30 do

      for col=1, 30 do
        local cellValue = sheet1.Cells(row, col).Value2
        if cellValue ~= nil then
            --print(cellValue)

        end
      end
    end
end

我应该如何修改它才能读取 Excel 文件,因为当前 excel 变量没有包含任何工作表,并且似乎加载没有成功。

点赞
用户4668815
用户4668815

看起来你调用 Open 函数的方式不正确。尝试使用以下代码:

local workbook = excel.Workbooks:Open("1.xlsx")

然后使用 workbook (而不是 excel)来完成其他操作,例如:

local sheet = workbook.Sheets(1)
2015-03-13 20:04:19