56
neat package! i didn’t know about this
According to the doc, the save(filename_or_stream)
method takes either a filename to save on, or a file-like stream to write on.
And a Django response object happens to be a file-like stream! so just do xls.save(response)
. Look the Django docs about generating PDFs with ReportLab to see a similar situation.
edit: (adapted from ShawnMilo’s comment):
def xls_to_response(xls, fname):
response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=%s' % fname
xls.save(response)
return response
then, from your view function, just create the xls
object and finish with
return xls_to_response(xls,'foo.xls')
6
***UPDATE: django-excel-templates no longer being maintained, instead try Marmir http://brianray.github.com/mm/
Still in development as I type this but http://code.google.com/p/django-excel-templates/ Django excel templates project aims to do what your asking.
Specifically look at the tests. Here is a simple case:
#
from django_excel_templates import *
from django_excel_templates.color_converter import *
from models import *
from django.http import HttpResponse
def xls_simple(request):
## Simple ##
testobj = Book.objects.all()
formatter = ExcelFormatter()
simpleStyle = ExcelStyle(vert=2,wrap=1)
formatter.addBodyStyle(simpleStyle)
formatter.setWidth('name,category,publish_date,bought_on',3000)
formatter.setWidth('price',600)
formatter.setWidth('ebook',1200)
formatter.setWidth('about',20000)
simple_report = ExcelReport()
simple_report.addSheet("TestSimple")
filter = ExcelFilter(order='name,category,publish_date,about,bought_on,price,ebook')
simple_report.addQuerySet(testobj,REPORT_HORZ,formatter, filter)
response = HttpResponse(simple_report.writeReport(),mimetype='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename=simple_test.xls'
return response
- [Django]-How to add class, id, placeholder attributes to a field in django model forms
- [Django]-Django Get All Users
- [Django]-VSCode terminal shows incorrect python version and path, launching terminal from anaconda works perfectly
2
You can save your XLS file to a StringIO object, which is file-like.
You can return the StringIO object’s getvalue()
in the response. Be sure to add headers to mark it as a downloadable spreadsheet.
- [Django]-Unresolved reference: 'django' error in PyCharm
- [Django]-Cannot import name patterns
- [Django]-Editing django-rest-framework serializer object before save
2
You might want to check huDjango which comes fith a function called serializers.queryset_to_xls()
do convert a queryset into an downloadable Excel Sheet.
- [Django]-What should I use instead of syncdb in Django 1.9?
- [Django]-How can I unit test django messages?
- [Django]-Exclude a field from django rest framework serializer
- [Django]-Check if model field exists in Django
- [Django]-Can I make STATICFILES_DIR same as STATIC_ROOT in Django 1.3?
- [Django]-How do I subtract two dates in Django/Python?
0
If your data result doesn’t need formulas or exact presentation styles, you can always use CSV. any spreadsheet program would directly read it. I’ve even seen some webapps that generate CSV but name it as .XSL just to be sure that Excel opens it
- [Django]-Django Aggregation – Expression contains mixed types. You must set output_field
- [Django]-Using Django auth UserAdmin for a custom user model
- [Django]-User Registration with error: no such table: auth_user