Return openpyxl workbook object as HttpResponse in django. Is it possible?

后端 未结 3 687
你的背包
你的背包 2021-02-07 08:52

I need to provide an excel formatted data from django\'s database to visitors.

The only way I can think of is with these steps:

  1. Extract data from database.
3条回答
  •  误落风尘
    2021-02-07 09:13

    this worked for me

    from openpyxl import Workbook, load_workbook
    from openpyxl.writer.excel import save_virtual_workbook
    
    wb = Workbook()
    ...
    
    response = HttpResponse(content=save_virtual_workbook(wb), content_type='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=Inform.xlsx'
    return response
    

提交回复
热议问题