Last active
January 22, 2023 11:24
-
-
Save knowankit/586441393c2a946aa4c530b49bad5849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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