我正在尝试使用Django创建和服务excel文件。我有一个jar文件,它可以获取参数,并根据参数生成一个excel文件,它可以正常工作。但是,当我试图获取生成的文件并将其提供给用户下载时,该文件会被破坏。它有0kb大小。这是我用来生成和服务excel的代码。def generateExcel(request,id):
if os.path.exists('./%s_Report.xlsx' % id):
excel = open("%s_Report.xlsx" % id, "r")
output = StringIO.StringIO(excel.read())
out_content = output.getvalue()
output.close()
response = HttpResponse(out_content,content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s_Report.xlsx' % id
return response
else:
args = ['ServerExcel.jar', id]
result = jarWrapper(*args) # this creates the excel file with no problem
if result:
excel = open("%s_Report.xlsx" % id, "r")
output = StringIO.StringIO(excel.read())
out_content = output.getvalue()
output.close()
response = HttpResponse(out_content,content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s_Report.xlsx' % id
return response
else:
return HttpResponse(json.dumps({"no":"excel","no one": "cries"}))
我已经寻找了可能的解决方案,并试图使用文件包装器也,但结果没有改变。我想我在将xlsx文件读入StringIO对象时遇到了问题。但不知道怎么解决