2

i have this in my template :

<table>
<tr><td>book_id</td><td>book name</td><td>book_author</td></tr>
{% for book in books %}
<tr><td>{{ book.book_id }}</td><td>{{ book.book_name }}</td><td>{{ book.book_author }}</td></tr>
{% endfor %}
</table>
<a href="/export">Export to Excel !</a>

my view seems to be like this:

def export_excel(request):
    books = Book.objects.all()
    response = HttpResponse(books , content_type='application/vnd.ms-excel;charset=utf-8')
    response['Content-Disposition'] = 'attachment; filename="books.xls"'
    return response

And here is mu url in urls.py:

url(r'^export$', 'export_excel', name='export_excel'),

It export books in file that named books.xls and the probleme here is that it export them as "Book objects" in the first square (A1)

what should i do if i want to make every "book_attribute" in separate square and every "book" in separate line ?

5 Answers 5

6

You're sending something called "books.xls", and correctly signalling that it's an Excel spreadsheet... but it isn't. You've completely missed the step of actually creating an Excel spreadsheet containing your data (which is probably 80% of the work here).

Try searching the web for how to create an excel spreadsheet in Python.

Sign up to request clarification or add additional context in comments.

Comments

3

Working Example using tablib, an excellent tabular dataset library.

    from django.http import HttpResponse
    import tablib

            headers = ('Book', 'Author')
            data = []
            data = tablib.Dataset(*data, headers=headers)
            books = Book.objects.all()
            for book in books:
                data.append((book.book_name, book.author))
            response = HttpResponse(data.xls, content_type='application/vnd.ms-excel;charset=utf-8')
            response['Content-Disposition'] = "attachment; filename=export.xls"

        return response

Comments

0

I think the problem is that you just simply pass a list of Book object to your export file.

books = Book.objects.all()

This line just returns a list of objects.

I think you may want to iterate each property of Book object. Replace each Book Object with a tuple with all field displayed. The hard way is to do something like books_list = map(lambda x: (x.book_id, x.book_name, ....all the field of Book), books)

And you pass the books_list to export file instead of books.

Comments

0
def export_excel(request):
    books = Book.objects.all()
    response = HttpResponse(books , content_type='application/vnd.ms-excel;charset=utf-8')
    response['Content-Disposition'] = 'attachment; filename="books.xls"'

    writer = csv.writer(response)
    writer.writerow(['Book', 'Author'])
    for book in books:
        writer.writerow([book.book_name, book.author])

    return response

Comments

0

A solution using my plugin: django_excel

import djang_excel as excel
import pyexcel.ext.xls # in order to handle 'xls' format
# import pyexcel.ext.xlsx # in order to handle 'xlsx' format
# import pyexcel.ext.ods # in order to handle 'ods' format


def export_excel(self, request):
    # Book as django model, 'xls' as file format
    response = excel.make_response_from_a_table(Book, 'xls')
    response['Content-Disposition'] = 'attachment; filename="books.xls"' 
    return response

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.