Skip to content

Instantly share code, notes, and snippets.

@Afeez1131
Forked from rg3915/dj-export-excel.py
Created April 3, 2023 14:30
Show Gist options
  • Save Afeez1131/29b7be67ca22180f0451095bd454bad3 to your computer and use it in GitHub Desktop.
Save Afeez1131/29b7be67ca22180f0451095bd454bad3 to your computer and use it in GitHub Desktop.
Export data with django-excel
''' Exporta planilhas em Excel '''
from datetime import datetime
import django_excel as excel
from django.http import HttpResponseBadRequest
from .models import Person
MDATA = datetime.now().strftime('%Y-%m-%d')
def export_data_person(request, atype):
if atype == "sheet":
return excel.make_response_from_a_table(
Person, 'xls', file_name="contatos")
elif atype == "custom":
query_sets = Person.objects.all()
column_names = ['id', 'name', 'email']
return excel.make_response_from_query_sets(
query_sets,
column_names,
'xls',
file_name="contatos-" + MDATA
)
else:
return HttpResponseBadRequest(
"Bad request. please put one of these " +
"in your url suffix: sheet, book or custom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment