Django导出数据为Excel文件,使用浏览器下载

300 阅读3分钟
1. 环境
  Django (2.1.10) + Python3.6 + xlwt (1.3.0)
  操作系统使用的为:Windows 7
2. 接口代码
[Python]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def now_export(request):
data_list = CIP.objects.all() # 获取数据的查询集
if not data_list:
return HttpResponse(json.dumps({"state": "1", "msg": "查无数据,导出失败"}), content_type="application/json")
ws = Workbook(encoding='utf-8') # 设置Excel为UTF-8的编码格式
w = ws.add_sheet(u"正在清洗的CIP报表") # 设置sheet名称
w.write(0, 0, "id")
w.write(0, 1, u"清洗线路")
w.write(0, 2, u"操作者")
w.write(0, 3, u"清洗目标")
w.write(0, 4, u"清洗类型")
w.write(0, 5, u"CIP状态")
w.write(0, 6, u"开始时间")
# 把需要导出的数据写到文件中
excel_row = 1
for obj in data_list:
w.write(excel_row, 0, excel_row)
w.write(excel_row, 1, obj.CIPNo)
w.write(excel_row, 2, obj.oper)
w.write(excel_row, 3, obj.ccid)
w.write(excel_row, 4, obj.mode)
w.write(excel_row, 5, "CIP程序启动")
w.write(excel_row, 6, datetime.strftime(obj.start_time, '%Y-%m-%d %H:%M:%S'))
excel_row += 1
sio = BytesIO() # 写出到IO
ws.save(sio)
sio.seek(0) # 重新定位到开始
response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel') # 设置文件格式为Excel
   # attachment(意味着消息体应该被下载到本地;大多数浏览器会呈现一个“保存为”的对话框,将filename的值预填为下载后的文件名)
   response['Content-Disposition'] = 'attachment; filename=test.xls'
   response.write(sio.getvalue())
   return response

3. 前端请求
注意:但需要注意的是,如果想要用这种方式下载文件,不能使用AJAX的方式,而是应该新建一个<a>标签,模拟点击下载。原因为处于安全性考虑,JavaScript无法与磁盘进行交互,因此AJAX得到的内容将被保留在内存中,而不是磁盘上。
3.1 请求方式为GET时:
  此时直接在A标签的href属性中指定路由即可
[HTML]
纯文本查看
复制代码
1
<a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" title="导出报表" href="/CIP/now_export/">导出报表</a>
3.2 请求方式为POST时:
[HTML]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" title="导出报表"><br>导出报表</a>
<form action="/CIP/draft_export/" method="post" id="form" enctype="multipart/form-data">
<div class="row cl">
<div class="formControls col-xs-8 col-sm-9">
<label class="form-label col-xs-4 col-sm-2">开始日期:</label>
<input type="text" style="width: 200px" <br>id="startdate" class="input-text Wdate" name="startdate">
</div>
<div class="formControls col-xs-8 col-sm-9" style="width: 400px">
<label class="form-label col-xs-4 col-sm-2">结束日期:</label>
<input type="text" style="width: 200px" <br>id="enddate" class="input-text Wdate" name="enddate">
</div>
</div>
<br>
<div class="row cl">
<div class="formControls col-xs-8 col-sm-9">
<label class="form-label col-xs-4 col-sm-2">线路:</label>
<span class="select-box">
<select class="select" id="line" name="line">
<option>all</option>
</select>
</span>
</div>
<div class="formControls col-xs-8 col-sm-9">
<label class="form-label col-xs-4 col-sm-2">目标:</label>
<span class="select-box">
<select class="select" id="goal" name="goal">
<option>all</option>
</select>
</span>
</div>
<a name="" id="" class="btn btn-success"><i class="Hui-iconfont" ></i> 搜索</a>
</div>
</form>

更多技术资讯可关注:itheimaGZ获取