2012-06-21 2 views

답변

1

실제로는 Excel 파일을 읽고 데이터를 구문 분석하려고하는데 Excel 객체를 조작하지 않기 때문에 중복되지 않습니다.

이 작업을 수행 할 때마다 ADO를 사용하여 luasql을 사용했습니다. 가장 기본적인 수준에서 이와 같은 것은 DB 연결을 열고 쿼리의 각 행에 필드의 개수를 알면 데이터를 Excel에서 읽는 데 사용하는 것입니다. 다시 받고있어 얼마나 많은 필드 당신이 모르는 경우

function rows(connection, sql_stmt) 
    local cursor = assert(connection:execute(sql_stmt)) 
    return function() 
     return cursor:fetch() 
    end 
end 

local fpath = "path/to/file.xlxs" 
local constr = "Provider=Microsoft.ACE.OLEDB.12.0;".. 
       "Data Source=\""..fpath.."\";".. 
       "Extended Properties=\"Excel 12.0 Xml;HDR=YES\""; 

local env = assert(luasql.ado()) 
local con = assert(env:connect(constr)) 

-- the name of the worksheet needs to be surrounded by brackets, and end 
-- with a '$' sign. 
local query = "SELECT * FROM \[name_of_worksheet$\]" 

-- you can do this if you know how many fields you get back specifically. 
for field1, field2, field3, ... in rows(con, query) do  
    -- handle any parsing of data from a row, here. 
end 

con:close() 
env:close() 

, 당신은 같은 것을 할 수 있습니다

local fpath = "path/to/file.xlxs" 
local constr = "Provider=Microsoft.ACE.OLEDB.12.0;".. 
       "Data Source=\""..fpath.."\";".. 
       "Extended Properties=\"Excel 12.0 Xml;HDR=YES\""; 

local env = assert(luasql.ado()) 
local con = assert(env:connect(constr)) 

-- the name of the worksheet needs to be surrounded by brackets, and end 
-- with a '$' sign. 
local query = "SELECT * FROM \[name_of_worksheet$\]" 


-- if you didn't know how many fields you might get back per row, i 
-- believe you can do this to iterate over each row... 
local cursor = con:execute(query) 
local t = {} 
local results = cursor:fetch(t) 

while results ~= nil do 
    for k,v in pairs(results) do 
     -- check each field in the row here 
    end 
    results = con:fetch(t) 
end 

cursor:close() 
con:close() 
env:close() 

당신이있어 때 커서/연결/환경을 종료해야합니다 그들과 함께했다. 또한 위의 연결 문자열이 사용중인 Excel 버전에서 작동하는지 확인해야합니다. 필요한 연결 문자열을 결정하는 가장 쉬운 방법은 www.connectionstrings.com으로 이동하여 올바른 연결 문자열을 찾아 보는 것입니다.

관련 문제