from flask import Flask, send_from_directory, make_response
import tablib
app = Flask(__name__)
@app.route('/upload')
def upload():
# data = 'a,c,v,s,s \r' 'e,r,et,t,e' 转csv
js = [{"a": "111", "b": "222"}, {"a": "333", "b": "444"}]
js_2 = [{"a": "aaa", "b": "bbb"}, {"a": "nnn", "b": "mmm"}]
header = tuple(js[0].keys())
data = []
# 循环里面的字典,将value作为数据写入进去
for row in js:
body = []
for v in row.values():
body.append(v)
data.append(tuple(body))
data_2 = []
for row in js_2:
body = []
for v in row.values():
body.append(v)
data_2.append(tuple(body))
data = tablib.Dataset(*data, headers=header, title='aaa')
data_2 = tablib.Dataset(*data_2, headers=header, title='bbb')
excel = tablib.Databook([data, data_2]).export('xls')
# filename = "核武库" + startDate.strftime("%Y%m%d") + "-" + endDate.strftime("%Y%m%d") + ".xls"
response = make_response(excel)
response.headers["Content-Disposition"] = "attachment; filename=myfilename.xls" # 浏览器作为附件下载
response.headers['Content-Type'] = 'xls'
return response
if __name__ == '__main__':
app.run(debug=True)
网友评论