Reputation: 5448
I am getting an error in Excel say:
excel cannot open the file because the file format or file extension is not valid
Hi I have a excel file that is being generated each morning. I want the user to be able to download the Excel file.
def export_daily_report(request):
output = pd.read_excel('crontab_files/daily.xlsx')
response = HttpResponse(content_type='application/vnd.ms-excel')
# tell the browser what the file is named
response['Content-Disposition'] = 'attachment;filename="daiy_export.xlsx"'
# put the spreadsheet data into the response
response.write(output)
# return the response
return response
I want to open the excel and then render the file so that the user can download it.
Upvotes: 0
Views: 551
Reputation: 21807
output
is a pandas DataFrame
as implied in this line:
output = pd.read_excel('crontab_files/daily.xlsx')
Next you try to write this dataframe to the response, and get an error at the client:
response.write(output)
Instead you should simply return a FileResponse
object [Django docs]:
from django.http import FileResponse
def export_daily_report(request):
return FileResponse(open('crontab_files/daily.xlsx', 'rb'), as_attachment=True, filename="daiy_export.xlsx")
Upvotes: 1