5

I am trying to use xlwt to create MS-Excel files from the contents of the database on my django site.

I have seen several solutions here on stackoverflow, in particular this link: django excel xlwt

and this django snippet: http://djangosnippets.org/snippets/2233/

These examples work in firefox, but not in Internet Explorer. Instead of getting prompted to open or save a file, a bunch of wingding junk appears on the screen. It seems that IE thinks the response is html.

Here is my view function:

def exportexcel(request):
    from xlwt import Workbook

    wb = Workbook()
    ws = wb.add_sheet('Sheetname')
    ws.write(0, 0, 'Firstname')
    ws.write(0, 1, 'Surname')
    ws.write(1, 0, 'Hans')
    ws.write(1, 1, 'Muster')

    fname = 'testfile.xls'
    response = HttpResponse(mimetype="application/ms-excel")
    response['Content-Disposition'] = 'attachment; filename=%s' % fname

    wb.save(response)

    return response

I am seeing this behavior in IE 8.

Any suggestions as to why this isn't working in Internet Explorer?

Thanks.

3
  • Try with application/vnd.ms-excel mimetype. Commented May 16, 2011 at 23:00
  • wow, that was fast and it worked. Thank you. Can you explain what 'vnd' does? Commented May 16, 2011 at 23:04
  • Look at my answer for explanation. Commented May 16, 2011 at 23:09

1 Answer 1

4

The mimetype you're using application/ms-excel is invalid for .xls files.

The standard one is application/vnd.ms-excel

Look here Setting mime type for excel document for more informations.

Sign up to request clarification or add additional context in comments.

1 Comment

Nevertheless, the Content-Disposition header should have resulted in a Save prompt. OP should use Fiddler to look at the HTTP response headers to make sure that the header was being sent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.