def export_table():
ILLEGAL_CHARACTERS_RE = re.compile('[\000-\010]|[\013-\014]|[\016-\037]')
table_name = request.args.get('table_name')
if os.path.isfile(current_app.root_path + "/xlsx/%s.xlsx" % table_name):
pass
else:
conn = g.db.conn
cur = conn.cursor()
sql = "select * from %s" % table_name
cur.execute(sql)
data = cur.fetchall()
wbk = Workbook()
sheet = wbk.create_sheet(table_name, 0)
sheet.cell(row=1, column=1).value = 'uid'
sheet.cell(row=1, column=2).value = 'name'
sheet.cell(row=1, column=3).value = 'address'
sheet.cell(row=1, column=4).value = 'tag'
sheet.cell(row=1, column=5).value = 'small tag'
sheet.cell(row=1, column=6).value = 'location'
sheet.cell(row=1, column=7).value = 'tel'
sheet.cell(row=1, column=8).value = 'pro_name'
sheet.cell(row=1, column=9).value = 'pro_center'
sheet.cell(row=1, column=10).value = 'city_name'
sheet.cell(row=1, column=11).value = 'city_center'
sheet.cell(row=1, column=12).value = 'district_name'
sheet.cell(row=1, column=13).value = 'district_center'
sheet.cell(row=1, column=14).value = 'photo_exists'
sheet.cell(row=1, column=15).value = 'photo1'
sheet.cell(row=1, column=16).value = 'photo2'
sheet.cell(row=1, column=17).value = 'photo3'
i = 2
for each in data:
for m in range(1, 18):
tmp = ILLEGAL_CHARACTERS_RE.sub('', each[m])
sheet.cell(row=i, column=m).value = tmp
i += 1
wbk.save(current_app.root_path + "/xlsx/%s.xlsx" % table_name)
response = make_response(send_from_directory(current_app.root_path + "/xlsx", table_name+".xlsx"))
response.headers["Content-Disposition"] = "attachment; filename=%s.xlsx;" % table_name
return response
# if os.path.isfile(os.path.join('./xlsx', table_name+".xlsx")):
# return send_from_directory('./xlsx', table_name+".xlsx", as_attachment=True)
# else:
# return "fail"
评论列表
文章目录