Flask导出txt和ecxel
前端使用 a 超连接指向这个url
使用xlwt导出excel
ws = xlwt.Workbook(encoding='utf-8')
w = ws.add_sheet("sheet1")
w.write(0, 0, "卡密")
row = 1
# cards = CardQuery().get_list(CardModel, {'card_stat': 2}, field=("card_id", ), limit=1000)
# 这行取数据库记录
for i in cards['data']:
w.write(row, 0, i['card_id'])
row += 1
output = BytesIO()
ws.save(output)
output.seek(0)
filename = "cards.xls"
fv = send_file(output, as_attachment=True, attachment_filename=filename)
fv.headers['Content-Disposition'] += "; filename*=utf-8''{}".format(filename)
return fv
导出为txt
# cards = CardQuery().get_list(CardModel, {'card_stat': 2}, field=("card_id", ), limit=1000)
# 这行取数据库记录
text = "\n".join([i['card_id'] for i in cards['data']])
output = BytesIO()
output.write(text.encode())
output.seek(0)
filename = "cards.txt"
fv = send_file(output, as_attachment=True, attachment_filename=filename)
fv.headers['Content-Disposition'] += "; filename*=utf-8''{}".format(filename)
return fv