1

I am using openpylx to create a spreadsheet and return it in an HttpResponse object in Django (1.7). Currently, this is working except that the spreadsheet is not downloaded; it is displayed in the browser as a bunch of characters (pic related).

enter image description here

Here is my code:

from openpyxl import Workbook
from openpyxl.writer.excel import save_virtual_workbook

workbook = Workbook()
...
#Create the workbook here
...    
filestream = save_virtual_workbook(workbook)
response = HttpResponse(filestream,
                        content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="my_sheet.xlsx"'
return response

This code is pretty much identical to several other solutions offered in other questions here on SO. Unfortunately, I can't figure out what the difference is. Maybe Django version?

I know that I am missing someething stupid here. What is it?

EDIT: I know that the issue is not with the spreadsheet itself. If I save the page with the characters as "my_sheet.xlsx", then open it in Excel, all of the proper data is present.

kbuilds
  • 991
  • 11
  • 19
  • 1
    code looks right to me too, but have a look at a couple of things here: http://stackoverflow.com/a/2938188/202168 try the longer content type (since it's an xlsx) and try removing the double quotes around the filename (apparently helps in MSIE, though looks like you're on Chrome/Android) – Anentropic Aug 02 '15 at 16:25
  • @Anentropic: Thanks for the reply. Tried making the recommended changes. No dice. Also, yes, right this second, I am working on Android/Chrome, but I have replicated the issue in Ubuntu/Chrome as well – kbuilds Aug 02 '15 at 16:32

0 Answers0