vbs操作Excel生成lua配置文件

来源:互联网 发布:访问js对象的属性 编辑:程序博客网 时间:2024/06/05 09:22

大家都知道游戏里面的配置一般都成千上万行,如果由人手动去配置的话,工作效率一定都是很低的,所以今天我分享下载我们游戏项目中用Excel中数据生成quick cocos2d-x 游戏lua静态配置表的方法,这样程序员就只要写好程序,由策划配置游戏数值一键生成游戏中的静态配置了.
excel表:Score.xlsx

vbs代码:Score.vbs

Function WriteToFile (FileUrl, Str)       Set stm = CreateObject("Adodb.Stream")       stm.Type = 2       stm.mode = 3       stm.charset = "utf-8"       stm.Open       stm.WriteText Str       stm.Position = 3     Set newStream = CreateObject("Adodb.Stream")       With newStream           .Mode = 3           .Type = 1           .Open()       End With     stm.CopyTo(newStream)       newStream.SaveToFile FileUrl,2        stm.Close       Set stm = Nothing       newStream.flush       newStream.Close     Set newStream = Nothing End Function   dim startTime , endTime startTime = Time()set oFSO = CreateObject("Scripting.FileSystemObject")path = oFSO.GetFile(Wscript.ScriptFullName).ParentFolder.Pathset ExcelApp = CreateObject("Excel.Application")set ExcelBooks = ExcelApp.Workbooks.Open(path & "\Score.xlsx")set ExcelSheet = ExcelBooks.Sheets("Sheet1")dim idx,contentcontent = "--" & FormatDateTime(Date(),vbLongDate) & Chr(10)content = content & "--created by Score.xlsx , Score.vbs" & Chr(10) & Chr(10)content = content & "Score = {}" & Chr(10) & Chr(10)idx = 2While CStr(ExcelSheet.cells(idx,1)) <> ""    content = content & "Score[" & CStr(ExcelSheet.cells(idx,1)) & "] = {" & Chr(10)    content = content & "    name = """ & CStr(ExcelSheet.cells(idx,2)) & """," & Chr(10)    content = content & "    score = """ & CStr(ExcelSheet.cells(idx,3)) & """," & Chr(10)    content = content & "}" & Chr(10)    content = content & Chr(10)    idx = idx + 1Wendcontent = content & "return Score"call WriteToFile("./score.lua", content) ExcelBooks.CloseendTime = Time()msgbox "导出成功" & Chr(10) & " use:" & DateDiff("s", startTime, endTime) & "s"

生成的配置文件:Score.lua

--2015年12月28日--created by Score.xlsx , Score.vbsScore = {}Score[1] = {    name = "郭靖",    score = "34",}Score[2] = {    name = "刘备",    score = "78",}Score[3] = {    name = "关羽",    score = "12",}Score[4] = {    name = "曹操",    score = "43",}return Score

参考文章:
Vbs脚本经典教材
VBS CHR码值对应列表
vbs获取目录下的文件和文件夹集合
避免Adodb.Stream输出UTF-8时自动写入的BOM

0 0
原创粉丝点击