是否有任何内置函数或对象在Lua脚本中获取excel文件中的行和列计数。提出了一种如何通过lua脚本获取行和列长度的方法
答案 0 :(得分:0)
local function get_rows_cols_in_excel_file(excel_file)
-- returns two values: number of rows and number of columns
-- returns nothing if file not found
local vbscript_filename = os.getenv"TEMP".."\\getrowscols.vbs"
local vbscript = [[
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.DisplayAlerts = False
Set objBook = objExcel.Workbooks.Open(Wscript.Arguments(0))
If not objBook Is Nothing Then
Set Rng = objBook.Worksheets(1).UsedRange
Wscript.Echo Rng.Rows.Count & ";" & Rng.Columns.Count
objBook.Close False
End If
]]
local vbs_file = assert(io.open(vbscript_filename, "w"))
assert(vbs_file:write(vbscript))
vbs_file:close()
local command = '"cscript //nologo "'..vbscript_filename..'" "'..excel_file..'""'
local pipe = io.popen(command)
local rows, cols = pipe:read"*a":match"(%d+);(%d+)"
pipe:close()
if rows then
return tonumber(rows), tonumber(cols)
end
end
local your_excel_file = [[C:\Path\to\your\excelfile.xls]]
local rows, cols = get_rows_cols_in_excel_file(your_excel_file)
print(rows, cols)