Home > Software engineering >  Django excel export, file is not downloading
Django excel export, file is not downloading

Time:08-30

I am trying to perform excel export functionality in Django with xls. But When I am trying to perform that file is not downloading and there is no error also.

Here is my code.

def excelExport(request):
      response = HttpResponse(content_type='application/vnd.ms-excel')
      response['Content-Disposition'] = 'attachment;filename="InitalRegistaration.xls"'
      work_book = xlwt.Workbook(encoding='utf-8')
      uc = u"".join(chr(0x0410   i) for i in range(32)) # some Cyrillic characters
      u8 = uc.encode("UTF-8")
      work_sheet = work_book.add_sheet('Client Registration')
      work_sheet.write(0,0,"Clients")
      work_book.save(response)
      return response

I don't know what's wrong with my code but the file is not getting downloaded nor there is the error coming from the code.

CodePudding user response:

In python3 something like this should work:

from io import BytesIO
from tempfile import NamedTemporaryFile

with NamedTemporaryFile() as tmp:
    work_book.save(tmp.name)
    content = BytesIO(tmp.read())
    
response = HttpResponse(content, content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = 'attachment; filename=%s' % dname
return response
  • Related