Python 下载excel

 

上面源码

View:result_list是一个list为数据,当为list时,用enumerate可以获取到list的值和值所在序号
xlsx = openpyxl.Workbook()
table = xlsx.active
table.title = 'data'
for row_index, line in enumerate(result_list):
    table.append(line)

xlsxfile = io.BytesIO()
xlsx.save(xlsxfile)
return HttpResponse(xlsxfile.getvalue(),
    content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

URL:注意必须指定文件格式,以及文件名字。
url(r'^file_down/alldatas.xlsx', File_Down, name= 'file_down')

模板:
<a id="downfile" href="{% url 'file_down' %}"> 

 

posted on 2017-03-21 11:02  星河赵  阅读(3236)  评论(0编辑  收藏  举报

导航