from django.utils.encoding import escape_uri_path # 用于解决中文命名文件乱码问题
def excel(request):
# df=pd.read_excel('测试.xlsx')
# ht=df.to_html()
# with open('./测试.xlsx', 'rb')as f:
# df = f.read()
df=open('./测试.xlsx) # 这里需要用open打开,如果用with open 打开的话会造成读取失败,
name = "测试.xlsx"
response = FileResponse(df)
response['Content-Type'] = 'application/octet-stream' # 让浏览器知道这是一个下载文件
# 解决文件下载中文命名出现乱码的情况
response["Content-Disposition"] = "attachment; filename={0}".format(escape_uri_path(name))
return response
在url中加入一条路由即可直接使用,亲测有效
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有