Fakultas Ilmu Komputer UI

Verified Commit bcd02709 authored by Giovan Isa Musthofa's avatar Giovan Isa Musthofa
Browse files

[REFACTOR] Use django-pandas + to_excel combo to write queryset

parent 4182a238
......@@ -5,15 +5,21 @@ from acara_donor.filters import AcaraDonorFilter
def download_acara_donor(modeladmin, request, queryset):
import xlsxwriter
import pandas as pd
from django_pandas.io import read_frame
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = f'attachment; filename={modeladmin.model._meta}.xlsx'
workbook = xlsxwriter.Workbook(response)
worksheet = workbook.add_worksheet()
df = read_frame(queryset)
df['waktu_donor'] = df['waktu_donor'].astype(str).str[:-6] # Strip tzinfo
worksheet.write('A1', 'Hello world')
writer = pd.ExcelWriter(response, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
# worksheet = writer.sheets['Sheet1']
workbook.close()
......
......@@ -6,5 +6,8 @@ Django==3.0.5
django-anymail==7.0.0
djangorestframework==3.11.0
django-cors-headers==3.2.1
django-pandas==0.6.2
gunicorn==20.0.4
numpy==1.18.4
pandas==1.0.4
XlsxWriter==1.2.9
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment