Lua讀寫EXCEL檔案
一、寫新的EXCEL檔案
import("luacom");--引用com外掛
--[[ 建立excel物件 ----------]]
local excel = luacom.CreateObject("Excel.Application") --建立新的例項
excel.Visible = true
local book =excel.Workbooks:Add()
local sheet = book.Sheets(1) --讀第一個工作表
sheet.Cells(1, 1).Value2 = "您好"
-- 儲存
book:SaveAs("c://test.xls")
book:Close()
--[[ 關閉excel -------------]]
excel:Quit(); --COM中的方法、函式寫在一個冒號後面,而屬性值是寫在小圓點後面
--[[ 釋放資源 -------------]]
excel = nil;
collectgarbage()
二、修改已存在的EXCEL檔案
import("comlua");--引用com外掛
--[[ 建立excel物件 ----------]]
local excel = luacom.CreateObject("Excel.Application") --建立新的例項
excel.Visible = true
local book =excel.Workbooks:Open("c://test.xls") --開啟指定的檔案
local sheet = book.Sheets(1) --讀第一個工作表
win.messageBox(sheet.Cells(1, 1).Text)
sheet.Cells(1, 2).Value2 = "世界"
-- 儲存
book:Save()
book:Close()
--[[ 關閉excel -------------]]
excel:Quit(); --COM中的方法、函式寫在一個冒號後面,而屬性值是寫在小圓點後面
--[[ 釋放資源 -------------]]
excel = nil;
collectgarbage()
三、存在問題
產生的EXCEL程序不能退出。您解決這問題,請給我留言,謝謝:-)