Django提供生成的excel文件 [英] Django to serve generated excel file

查看:494
本文介绍了Django提供生成的excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的代码中,我想要一个新鲜的服务器生成的excel文件驻留在我的应用程序目录中的文件夹名为files

  excelFile = ExcelCreator.ExcelCreator(test)
excelFile.create()
response = HttpResponse(content_type ='application / vnd.ms-excel')
response ['Content-Disposition'] ='attachment;文件名=test.xls'
返回响应

所以当我点击按钮运行这部分代码,它向用户发送一个空文件。通过查看我的代码,我可以理解这个行为,因为我没有在我的回复中指向该文件。



我看到有些人使用文件包装我不太明白使用)。所以我这样做:

  response = HttpResponse(FileWrapper(excelFile.file),content_type ='application / vnd.ms- excel')

但是,我从服务器收到错误消息:发生服务器错误。请联系管理员。



感谢您帮助我进行Django任务,我的所有宝贵意见越来越好!

解决方案

首先,您需要了解这是如何工作的,您将获得一个空文件,因为这是您正在做的,实际上:

  response = HttpResponse(content_type ='application / vnd.ms-excel')
response ['Content-Disposition'] ='attachment;文件名=test.xls'

HttpResponse首先接收响应的内容,看看它的构造器:

  def __init __(self,content ='',mimetype = None,status = None,content_type = None ):

所以你需要用你想要的内容创建响应,在这种情况下,您的.xls文件的内容。



您可以使用任何方法来做到这一点,只需确保内容在那里。



这里有一个示例:

  import StringIO 
output = StringIO.StringIO()
#读取您的内容并将其放在输出中var
out_content = output.getvalue()
output.close()
response = HttpResponse(out_content,mimetype ='application / vnd.ms-excel ')
response ['Content-Disposition'] ='attachment; filename =test.xls'


I looked at the various questions similar to mine, but I could not find anything a fix for my problem.

In my code, I want to serve a freshly generated excel file residing in my app directory in a folder named files

excelFile = ExcelCreator.ExcelCreator("test")
excelFile.create()
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="test.xls"'
return response

So when I click on the button that run this part of the code, it sends to the user an empty file. By looking at my code, I can understand that behavior because I don't point to that file within my response...

I saw some people use the file wrapper (which I don't quite understand the use). So I did like that:

response = HttpResponse(FileWrapper(excelFile.file),content_type='application/vnd.ms-excel')

But then, I receive the error message from server : A server error occurred. Please contact the administrator.

Thanks for helping me in my Django quest, I'm getting better with all of your precious advices!

解决方案

First, you need to understand how this works, you are getting an empty file because that is what you are doing, actually:

response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="test.xls"'

HttpResponse receives as first arg the content of the response, take a look to its contructor:

def __init__(self, content='', mimetype=None, status=None, content_type=None):

so you need to create the response with the content that you wish, is this case, with the content of your .xls file.

You can use any method to do that, just be sure the content is there.

Here a sample:

import StringIO
output = StringIO.StringIO()
# read your content and put it in output var
out_content = output.getvalue()
output.close()
response = HttpResponse(out_content, mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="test.xls"'

这篇关于Django提供生成的excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆