0

I have usecase where I have to generate and send the excel file having records upto 4M ,since the file can be large I want to stream the response while I am generating the file

https://docs.djangoproject.com/en/2.0/howto/outputting-csv/#streaming-large-csv-files : this is exactly what I want but for excel file with records upto 4M

here is the template code for csv format of what I want to achieve in excel format

def get_stuff():
def fetch_from_server_cursor(cursor, cursor_name, fetch_size=10000):
    while True:
        cursor.execute("FETCH {fetch_size} FROM {cursor_name}".format(fetch_size = fetch_size, cursor_name = cursor_name))
        chunk = cursor.fetchall()
        if not chunk:
            return
        yield from chunk

with transaction.atomic(), connection.cursor() as cursor:
    cursor_name = "my_cursor"
    cursor.execute(
        """
        DECLARE {cursor_name} CURSOR FOR
        SELECT field1,field2
        FROM {table_name}
        WHERE field3 = XX
        """.format(cursor_name = cursor_name,table_name = MyModel.objects.model._meta.db_table)
    )
    yield from fetch_from_server_cursor(cursor, cursor_name)

def stream_csv_view(request):
"""A view that streams a large CSV file."""
# Generate a sequence of rows. The range is based on the maximum number of
# rows that can be handled by a single sheet in most spreadsheet
# applications.
pseudo_buffer = Echo()
writer = csv.writer(pseudo_buffer)
response = StreamingHttpResponse((writer.writerow(row) for row in get_stuff()),
                                 content_type="text/csv")
response['Content-Disposition'] = 'attachment; filename="test.csv"'
return response

Also can someone look into get_stuff() function and recommend me a better way to access database,this one is failing

Any help with this?

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34

1 Answers1

0

You can use django-queryset-csv module that can export csv from queryset also it uses StreamingHttpResponse instead of the normal HttpResponse by default.

tz01x
  • 51
  • 5