简体   繁体   中英

Remove .xls extension on excel file created with xlsxwriter on django

I wrote a really simple django project to test xlsxwriter. I can open the excel file, but when I name the file 'filename.xlsx', the file is downloaded as 'filename.xlsx.xls'. How can I fix this?

from django.shortcuts import render
from django.http import HttpResponse
from .excel import get_excel


def home_view(request):
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename=filename.xlsx'
    excel_data = get_excel()
    response.write(excel_data)
    return response

XSLX is an OpenXML format, so the mimetype is different, it uses:

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

you thus should change this to:

def home_view(request):
    response = HttpResponse(
        
    )
    response['Content-Disposition'] = 'attachment; filename=filename.xlsx'
    excel_data = get_excel()
    response.write(excel_data)
    return response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM