def build_excel_response(field_names,data,filename): try: wb = Workbook(write_only=True) ws = wb.create_sheet() ws.append(field_names) for item in data: ws.append(item.values()) s = StringIO() wb.save(s) response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response['Content-Disposition'] = u'attachment; filename={}.xlsx'.format(filename) response['Content-Length'] = s.len response.write(s.getvalue()) s.close() return response except Exception, exc: return build_exception_response()
def download(self, request): try: serializer = self.get_serializer_class()(self.get_queryset(), many=True) return build_excel_response(serializer.child.fields.keys(),serializer.data,'controllo_pilomat') except Exception, exc: return build_exception_response()