add excel nodes
This commit is contained in:
parent
8613041cff
commit
31c4434ba1
17
lib/excel.py
17
lib/excel.py
@ -47,9 +47,13 @@ def upload_excel(request):
|
||||
|
||||
# 加载excel文件
|
||||
def loadData(file_name, sheet_index):
|
||||
# 根据文件名加载图片
|
||||
wb = load_workbook('../upload' + file_name)
|
||||
# 根据参数获取sheet
|
||||
ws = wb[wb.sheetnames[sheet_index]]
|
||||
# 计数器
|
||||
index = 0
|
||||
# 数据表
|
||||
data_list = {}
|
||||
for row in ws.rows:
|
||||
# 排除表头
|
||||
@ -58,6 +62,7 @@ def loadData(file_name, sheet_index):
|
||||
keywords = row[0].value
|
||||
# 内容
|
||||
content = row[1].value
|
||||
# 这样就是 key:value的形式
|
||||
data_list[keywords] = content
|
||||
index += 1
|
||||
return data_list
|
||||
@ -103,25 +108,37 @@ def createWorkBook(data_list):
|
||||
# 激活 worksheet
|
||||
ws = wb.active
|
||||
index = 1
|
||||
# 遍历数据表
|
||||
for keywords in data_list:
|
||||
# 第一列放关键词
|
||||
ws.cell(row=index, column= 1).value = keywords
|
||||
# 第二列放内容
|
||||
ws.cell(row=index, column= 2).value = data[keywords]
|
||||
index += 1
|
||||
# 获取一个唯一的文件名
|
||||
new_filename = create_uuid() + '.xlsx'
|
||||
# 保存到指定地址,用nginx暴露这个文件夹映射到/excel/get
|
||||
wb.save('../saveExcel' + new_filename)
|
||||
# 返回给前端的相对地址
|
||||
addr = '/excel/get/' + new_filename
|
||||
except Exception as e:
|
||||
return {'errcode': 900, 'errmsg': '创建工作簿失败'}
|
||||
return {'errcode': 200, 'errmsg': 'ok', 'addr': addr}
|
||||
|
||||
# 获取当前数据库中的信息
|
||||
def getkeywordsList():
|
||||
# 获取所有记录
|
||||
res_list = getAllKeywordList()
|
||||
# 获取失败拦截器
|
||||
if res_list['errcode'] != 200:
|
||||
return res_list
|
||||
# 获取到记录集合
|
||||
arr = res_list['arr']
|
||||
# 最终数据集
|
||||
data_list = {}
|
||||
for i in arr:
|
||||
data = arr[i]
|
||||
# 形成键值对方便遍历
|
||||
data_list[data['keywords']] = data['content']
|
||||
return {'errcode': 200, 'errmsg': 'ok', 'data_list': data_list}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user