2👍
✅
You need to save the workbook to the response to put the actual file into the HTTP response:
def download_xls(self, request):
os.chdir('/tmp')
xls_name = re.sub('\s+', '_', "%s_%s_%s.xlsx" % (
request.user.username.lower(),
self.report.name.lower(),
datetime.now().strftime('%y%m%d_%H%I%S')))
output = io.BytesIO()
workbook = Workbook(output, {'in_memory': True})
worksheet1 = workbook.add_worksheet()
worksheet2 = workbook.add_worksheet()
worksheet1.write("A1", "hello1")
worksheet2.write("A1", "hello2")
workbook.close()
output.seek(0)
response = HttpResponse(content_type='application/ms-excel')
response['Content-disposition'] = "attachment; filename=%s" % xls_name
return response
You can also try the xlwt package which seems more straightforward:
workbook.save(response)
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = "attachment; filename=%s" % xls_name
You can see an example on one of my projects here
Source:stackexchange.com