from flask import Response, send_file
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
output = StringIO()
# 查詢
results = Users.query.all()
# 寫入 csv file
for user in results:
output.write("%s,%s,%s\n" % (user.id, user.name, user.email))
# 輸出
filename = address.csv
response = Response(output.getvalue(), mimetype='text/csv')
response.headers['Content-Disposition'] = 'attachment; filename=' + filename
output.seek(0)
return send_file(output, as_attachment=True, attachment_filename=filename)
References :
flask note - 我的Java生活 - ITeye技术网站
Returning MySQL query data in a csv file with Flask
沒有留言:
張貼留言