从django shell导出到Excel

时间:2015-08-21 09:07:54

标签: python django excel shell

我有一个从服务器获取数据的函数,我已经以字典格式获取了它。现在我想在django shell中运行该函数并在我的本地驱动器中生成该文件。 这是我生成excel的程序,它有HttpResponse,我不需要这样做,我需要做些什么更改?

def to_excel(excelStr, filename):  

""" To Generate Excel Sheets """

response = HttpResponse(excelStr, mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=%s.xls' \
    % str(filename)
return response

1 个答案:

答案 0 :(得分:3)

with open('%s.xls' % (filename,)) as f:
    f.write(excelStr)