Skip to content

Instantly share code, notes, and snippets.

@knowankit
Last active January 22, 2023 11:24
Show Gist options
  • Save knowankit/586441393c2a946aa4c530b49bad5849 to your computer and use it in GitHub Desktop.
Save knowankit/586441393c2a946aa4c530b49bad5849 to your computer and use it in GitHub Desktop.
from django.http import HttpResponse
from openpyxl import Workbook
from openpyxl.writer.excel import save_virtual_workbook
def home_page(request):
wb = Workbook()
ws = wb.active
ws.append(['First Name', 'Last Name'])
data = [{
'first_name': 'John',
'last_name': 'Doe'
},
{
'first_name': 'Matt',
'last_name': 'Levy'
}]
for record in data:
ws.append([record['first_name'], record['last_name']])
response = HttpResponse(content=save_virtual_workbook(wb), content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename=myexport.xlsx'
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment