在日常的开发中, 或多或少会遇到将大量的, 需要初始化的数据制作到luatable里面,
将花费很多的精力, 还往往由于打错字、打错标点符号浪费了大量的时间.
经过一段时间的思考, 决定编写一个工具来配合excel表进行制作这类的大量的重复的数据
由于现在项目都是android和ios并行开发, 隧使用python语言进行工具的编写, 下面贴出python代码:
(如果缺少xlrd库的话 pip install xlrd)

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -- coding: utf-8 --
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

import os
import xlrd
import re

cur_path = os.path.abspath(sys.argv[0] + "/../")

# 分析一个表的内容
# sheetObject 工作表的对象
def parseOneSheet(excel_sheet):
# excel data dict
excel_data_dict = {}

# col name list
col_name_list = []

#col val type list
col_val_type_list = []

# ctype: 0 empty, 1 string, 2 number, 3 date, 4 boolean, 5 error
#value名字 比如 id desc
for col in range(0, excel_sheet.ncols):
cell = excel_sheet.cell(1, col)
col_name_list.append(str(cell.value))
assert cell.ctype == 1, "found a invalid col name in col [%d] !~" % (col)

#value的类型 比如int string boolean啊
for col in range(0, excel_sheet.ncols):
cell = excel_sheet.cell(2, col)
col_val_type_list.append(str(cell.value))
assert cell.ctype == 1, "found a invalid col val type in col [%d] !~" % (col)

#遍历所有有效的行
for row in range(3, excel_sheet.nrows):
cell_id = excel_sheet.cell(row, 0)

assert cell_id.ctype == 2, "found a invalid id in row [%d] !~" % (row)

if cell_id.value in excel_data_dict:
print('[警告] 配置了相同的"%d"物品, 请做检查' % (cell_id.value))
print('[警告] 配置了相同的"%d"物品, 请做检查' % (cell_id.value))
print('[警告] 配置了相同的"%d"物品, 请做检查' % (cell_id.value))

# row data list
row_data_list = []

for col in range(0, excel_sheet.ncols):
cell = excel_sheet.cell(row, col)
k = col_name_list[col]
cell_val_type = col_val_type_list[col]

# ignored the string that start with '_'
if str(k).startswith('#'):
continue

if cell_val_type == 'string':
if cell.ctype == 0:
v = ''''
else:
v = ''%s'' % (cell.value)
elif cell_val_type == 'int':
if cell.ctype == 0:
v = -1
else:
v = int(cell.value)
elif cell_val_type == 'float':
if cell.ctype == 0:
v = -1
else:
v = float(cell.value)
elif cell_val_type == 'table':
if cell.ctype == 0:
v = '{}'
else:
v = cell.value
else:
v = cell.value

row_data_list.append([k, v])

excel_data_dict[cell_id.value] = row_data_list

return excel_data_dict


def excel2lua(src_excel_path, tgt_lua_path):
# print('[file] %s -> %s' % (src_excel_path, tgt_lua_path))
# load excel data
excel_data_src = xlrd.open_workbook(src_excel_path, encoding_override = 'utf-8')
for name in excel_data_src.sheet_names():
print("Worksheet name %s " % name)
data_dict = parseOneSheet(excel_data_src.sheet_by_name(name))
lua_path = tgt_lua_path + name + ".lua"
lua_export_file = open(lua_path, "w")
lua_export_file.write('local %s = {n' % name)

for k, v in data_dict.items():
lua_export_file.write(' [%d] = {n' % k)
for row_data in v:
lua_export_file.write(' {0} = {1},n'.format(row_data[0], row_data[1]))
lua_export_file.write(' },n')

lua_export_file.write('}n')
lua_export_file.write('return %s' % name)

lua_export_file.close()

if __name__ == '__main__':
in_path = ""
out_path = ""
if len(sys.argv) == 1:
print('未检测到内容,采用默认路径')
in_path = cur_path + "/in/"
out_path = cur_path + "/out/"
else:
# excel文件的文件夹地址 默认为同目录下的in文件夹
in_path = sys.argv[1]
# 输出到lua的文件夹地址 默认为同目录下的in文件夹
out_path = sys.argv[2]

print("输入目录为 %s, 输出目录为%sn" % (in_path, out_path))
for root, dirs, files in os.walk(in_path):
for name in files:
if( ".xls" in name and ".xlsx" in name ):
print(root + name)
excel2lua(root + name, out_path)

exit(0)

代码从一位网友的blog找到的, 权侵删.