Skip to content
Advertisement

How to export excel file in django

I need help with exporting data using a template. I installed django-import-export and added it to admin panel, now I can only export data from the admin panel. I want to know how can i export excel file using template.

Advertisement

Answer

This should get you started:

import StringIO
import xlsxwriter
from django.http import HttpResponse

def export_page(request):
    # create our spreadsheet.  I will create it in memory with a StringIO
    output = StringIO.StringIO()
    workbook = xlsxwriter.Workbook(output)
    worksheet = workbook.add_worksheet()
    worksheet.write('A1', 'Some Data')
    workbook.close()

    # create a response
    response = HttpResponse(content_type='application/vnd.ms-excel')

    # tell the browser what the file is named
    response['Content-Disposition'] = 'attachment;filename="some_file_name.xlsx"'

    # put the spreadsheet data into the response
    response.write(output.getvalue())

    # return the response
    return response
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement