以前用Python,用py导表觉得很方便,现在用的是lua,项目用的是一个本地服务器从数据库服务器获取数据,导成lua文件,然后生成,每次加一个导表很多不方便,每次导表还要开启本地服务器,而且,加一个导表需要在每个使用者的本地服务器添加文件名字;于是乎,我觉得还是用py导表lua用吧,用xlrd模块(不知道的先了解一下很简单的)。
#-*- coding:utf-8 -*- import sys import os import xlrd DEFAULT = "local %s = class(\"%s\")\nfunction %s:ctor()\n\tself.data = self:getData()\nend\n\n\n" LINE_TAG = "--*******************\n" reload(sys) sys.setdefaultencoding('utf-8') def readExcel(sArgs): sFilePath = sys.path[0] + "\\" + sArgs + ".xlsx" #组装路径读取xlsx表文件 sResultPath = sys.path[0] + "\\" + sArgs + ".lua" if not os.path.isfile(sFilePath): print("File is not Exist", sFilePath) return oFileData = xlrd.open_workbook(sFilePath) lName = oFileData.sheet_names() #获取文件中表的个数 oSheet = oFileData.sheet_by_name(lName[0]) #只导表sheet1 lTitle = [] dData = LINE_TAG + "--*线以下导表文件,请勿手动修改\nfunction " + sArgs + ":getData()\nreturn {" if oSheet: for sValue in oSheet.row(0): lTitle.append(sValue.value) for index in range(oSheet.nrows): if index > 0: #首行标题不处理 dOneLine = "{\n\t\t" for idx, oCell in enumerate(oSheet.row(index)): if idx < oSheet.ncols: #不能大于列数 if oCell.ctype == 2: #number 0-empty, 1-string, 3-date 4-boolean 5-error dOneLine = dOneLine + lTitle[idx] + " = " + str(int(oCell.value)) + ",\n\t\t" else: #string dOneLine = dOneLine + lTitle[idx] + " = \"" + str(oCell.value) + "\",\n\t\t" dData = dData + "[" + str(index) + "] = " + dOneLine + "}," dData = dData + "\n\t" dData = dData + "}\nend\nreturn " + sArgs else: print "File read error", lName sTmp = "" if os.path.isfile(sResultPath): sResultFile = open(sResultPath, "rb") for sLine in sResultFile.readlines(): if LINE_TAG == sLine: print "----------Find Line----------" break else: sTmp = sTmp + sLine sResultFile.close() else: dData = DEFAULT % (sArgs, sArgs, sArgs) + dData sResultFile = open(sResultPath, "wb+") sResultFile.write(sTmp + dData) sResultFile.close() if __name__ == "__main__": print("打印参数", sys.argv) readExcel(sys.argv[1])直接可以跑,参数传的是导表名字,