vue+Django实现excel文件下载
vue端
<template>
<div id="home">
<a :href="'http://localhost:8000/download/?way_id=' + id"> 下载记录</div>
</template>
<script>
export default {
name: 'home',
data() {
return {
id: '',
}
},
}
</script>
django端
class Downloadrecord(View):
def get(self,request):
way_id = request.GET.get('way_id')
waybill_Form_class = Waybill_Form.objects.get(id=way_id)
waybill_temperature_all = waybill_Form_class.temperature_all
waybill_temperature_all = eval(waybill_temperature_all)
with open('data.csv','w',encoding='gbk') as csvf:
writer = csv.writer(csvf,delimiter=',')
writer.writerow(['时间','温度'])
for i in waybill_temperature_all:
time = i['time']
temp = i['temp']
with open('data.csv','a',encoding='gbk') as csvf:
writer = csv.writer(csvf,delimiter=',')
writer.writerow([time,temp])
file_path = 'data.csv'
with open(file_path, "rb") as f:
res = HttpResponse(f)
res["Content-Type"] = "application/octet-stream"
res["Content-Disposition"] = 'filename="{}"'.format(file_path)
return res
到此ok完工