下午好。我需要以excel格式从数据库下载数据。下面,当我尝试通过 Firefox 下载文件时,我的代码及其工作正常,但是使用 chrome 这个视图返回包含一些openpyxl文件的存档。Openpyxl- 这个库我用来从excel_download()函数中的数据库填充excel表。你能帮我解决吗?
def excel_download_view(request, **kwargs):
if 'sorting_argument' in kwargs:
queryset = Lot.objects.filter(
Q(bank__slug__iexact=kwargs['sorting_argument']) |
Q(auction_date__iexact=kwargs['sorting_argument']) |
Q(which_time_auction__iexact=kwargs['sorting_argument']) |
Q(fgvfo_number__iexact=kwargs['sorting_argument']) |
Q(prozorro_number__iexact=kwargs['sorting_argument']) |
Q(decision_number__iexact=kwargs['sorting_argument'])
)
else:
queryset = Lot.objects.all()
excel_download(queryset)
data = None
with open('media/excel/lots.xlsx', 'rb') as f:
data = f.read()
return HttpResponse(data, content_type='application/vnd.ms-excel')
Run Code Online (Sandbox Code Playgroud)
通过response-as-a-file-attachment,试试看:
response = HttpResponse(data, content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="lots.xlsx"'
return response
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6448 次 |
| 最近记录: |