Login

Excel Spreadsheet Export

Author:
MasonM
Posted:
July 24, 2008
Language:
Python
Version:
.96
Tags:
export
Score:
5 (after 5 ratings)

This is an example of a view that returns an Excel spreadsheet; the last ~7 lines are the relevant ones. It relies on Excel's ability to automatically import HTML (see http://support.microsoft.com/kb/165499 for more info). The spreadsheet.html template is just has one big <table> tag with all the data as table cells (no <html> or other surrounding tags are necessary).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def generate_spreadsheet(request):
    """
    Generates an Excel spreadsheet for review by a staff member.
    """
    election = Election.objects.latest()

    ballots = election.ballots.all()
    ballots = SortedDict([(b, b.candidates.all()) for b in ballots])
    # Flatten candidate list after converting QuerySets into lists
    candidates = sum(map(list, ballots.values()), [])
    votes = [(v, v.get_points_for_candidates(candidates))
             for v in election.votes.all()]
    response = render_to_response("spreadsheet.html", {
        'ballots': ballots.items(),
        'votes': votes,
    })
    filename = "election%s.xls" % (election.year_num)
    response['Content-Disposition'] = 'attachment; filename='+filename
    response['Content-Type'] = 'application/vnd.ms-excel; charset=utf-8'

    return response

More like this

  1. Template tag to render collections.Counter as an html table by asfaltboy 1 week, 4 days ago
  2. Generic CSV Export by zbyte64 7 years ago
  3. Group sequence into rows and columns for a TABLE by davidwtbuxton 4 years, 4 months ago
  4. Python Calendar wrapper template tag by dokterbob 6 years, 1 month ago
  5. Simple "html email with images" sender by andres_torres_marroquin 4 years, 4 months ago

Comments

youell (on July 25, 2008):

I like it. I've used this same approach in ASP for many years. Do you know if OpenOffice can do the same trick as Excel?

#

MasonM (on July 26, 2008):

Do you know if OpenOffice can do the same trick as Excel?

Yep. I just tried it in OpenOffice 2.4 and it worked fine. It took a pretty long time to import on my system, but that's probably because the election.xls spreadsheet that the above view returns is huge (>200KB).

#

phxx (on August 12, 2008):

Sorry, but i don't get where the excel file is generated. If I'm right it just attaches a file to the html response?

#

MasonM (on August 13, 2008):
Sorry, but i don't get where the excel file is generated. If I'm right it just attaches a file to the html response?

There's no such thing as an "HTML response" in Django; you must mean "HTTP response". The HTTP response this view generates contains an HTML file marked as an attachment with a MIME type of "vnd.ms-excel". Excel recognizes HTML and auto-imports it, as I said in the description.

#

phxx (on August 13, 2008):

ok, I thought it's just a reponse with html content but a different mimetype. And as you explained: It's just that. But that your spreadsheet app imports html is cool =)

Thanks for making things clear.

#

Please login first before commenting.