2016-11-21 3 views

Antwort

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) 
Verwandte Themen