Search code examples
djangodjango-modelsexport-to-excel

Django: exporting model data to excel file messes up character set


I'm trying to export model data to a Microsoft Excel file type (.xls) by using this view:

def generate_spreadsheet(request):
    alumnos = Alumno.objects.all()
    response = render_to_response("spreadsheet.html", {'alumnos': alumnos})
    filename = "alumnoss.xls"
    response['Content-Disposition'] = 'attachment; filename='+filename
    response['Content-Type'] = 'application/vnd.ms-excel; charset=utf-16'
    return response

As you can see, I define the character set as utf-16 which should include all of the extra characters like áéíóú, etc. But when I open the excel document, instead of reading

Vélez

you read:

Vélez

Any help would be appreciated :)


Solution

  • You can set what charset is going to be used for rendering defining DEFAULT_CHARSET in your settings.py file:

    http://docs.djangoproject.com/en/1.3/ref/settings/#default-charset