问题描述
我需要将Django数据库中的excel格式数据提供给访问者。
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:
- 从数据库中提取数据。
- 用
Workbook
对象包装来自openpyxl
。 - 将其临时保存。
- 以 rb的形式再次读取
- 使用返回视图。
- 删除磁盘上的excel文件。 (现在没用了吧?)
- Extract data from database.
- Wrap it with
Workbook
object fromopenpyxl
. - Save it somewhere temporarily.
- Read it again as 'rb'.
- return view with excel's mime type.
- Delete excel file on disk. (It's useless now right?)
应该这样做。但是,我认为还有另一种更好的方法。我的意思是
也许有一种方法可以直接将 openpyxl
对象作为 HttpResponse
返回
而没有中间文件
That should do it. But, I think there's another better way to do this. I meanmaybe there's a way to return openpyxl
object as HttpResponse
directlywithout intermediate file medium.
所以,我的问题是:是否可以返回 openpyxl
的 Worbook
对象? (我是 openpyxl
的新手)
So, my question here is: is it possible to return openpyxl
's Worbook
object? (I'm new with openpyxl
)
推荐答案
您不会实际需要将数据保存在磁盘上的任何位置;尽管没有很好的文档记录,openpyxl可以做到这一点。很久以前,我使用xlwt创建了,但最近我还构建了一些东西在框架中,使用openpyxl。
You don't actually need to save the data anywhere on disk; openpyxl has a way to do this, although it's not very well documented. A long while back, I created something like this using xlwt, but I recently also built something similar in the Falcon framework, using openpyxl.
两者在一起,您的代码将看起来像这样:
Putting these two together, your code would look something along the lines of:
from django.http import HttpResponse
from openpyxl import Workbook
from openpyxl.writer.excel import save_virtual_workbook
workbook = Workbook()
worksheet = workbook.active
# ... worksheet.append(...) all of your data ...
response = HttpResponse(content=save_virtual_workbook(workbook), mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=myexport.xlsx'
return response
如果要生成更大的文件,我会建议您考虑使用StreamingHttpResponse,但我相信这至少可以帮助您。
If you are generating larger files, I would recommend looking into using StreamingHttpResponse, but I believe this will at least get you going.
这只是基于两个项目合并的现成代码段我做了工作,所以可能不完全正确。它应该非常接近。 Falcon中的输出如下所示:
This is just an off-the-cuff snippet based on a merge of the two project I worked on, so it may not be exactly correct. It should be pretty close though. The output in Falcon looked like:
response.content_type = 'application/octet-stream;'
response.set_header('Content-Disposition', 'attachment; filename=myexport.xlsx')
response.body = save_virtual_workbook(workbook)
更新:
现在,这很容易,因为我完全重写了旧的 django-excel-response
库,使用openpyxl!现在可以在此处找到:
UPDATE:This is now much easier, as I did a complete rewrite of my old django-excel-response
library, using openpyxl! It can now be found here: https://github.com/tarkatronic/django-excel-response
您可以使用 pip install django-excel-response
进行安装,并开始将其用作替代Django的 HttpResponse
!包含的文档很少,并且欢迎进行改进/建议。 :)
You can install it with pip install django-excel-response
, and start using it as an alternative to Django's HttpResponse
! There is minimal documentation included, and improvements/suggestions are welcome. :)
这篇关于在Django中将openpyxl工作簿对象作为HttpResponse返回。可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!