简体   繁体   English

使用 django 上的 xlsxwriter 创建的 excel 文件上的 Remove.xls 扩展名

[英]Remove .xls extension on excel file created with xlsxwriter on django

I wrote a really simple django project to test xlsxwriter.我写了一个非常简单的 django 项目来测试 xlsxwriter。 I can open the excel file, but when I name the file 'filename.xlsx', the file is downloaded as 'filename.xlsx.xls'.我可以打开 excel 文件,但是当我将文件命名为“filename.xlsx”时,该文件被下载为“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: XSLX 是一种 OpenXML 格式,因此 mimetype 不同,它使用:

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

you thus should change this to:因此,您应该将其更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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