Search code examples
pythonhtmldjangoexport-to-excelxlrd

The Zero's '0' are getting removed automatically when i am converting my HTML template to excel sheets.


The whole process is that i am passing lists to the html template in Django as :-

def download_workbook(request):
    lists = family.objects.all()
    # your excel html format
    template_name = "sample_excel.html"

    response = render_to_response(template_name, {'lists': lists})
    # this is the output file
    filename = "sample.xls"

    response['Content-Disposition'] = 'attachment; filename='+filename
    response['Content-Type'] = 'application/vnd.ms-excel; charset=utf-16'
    return response

My template is sample_excel.html:-

PERSONNEL_NUMBERR   DEPENDT_FIRST_NAME1     DEPENDT_FAMILY_NAME DEPENDANT_TYPE  DEPENDT_BIRTH_DATE  DEPENDANT_SEQUENCE  FAMILY_ID
{% autoescape off %} {% for list in lists %}{{ str(list.member.personal_number) }}  {{ list.dependent_first_name1 }}    {{ list.dependent_family_name }}    {{ list.dependent_type }}   {{ list.dependent_birthdate }}  {{ list.dependent_sequence }}   {{ str(list.family_uid) }}
{% endfor %}{% endautoescape %}

there are two of the fields in database, i.e., personal_number and family_uid whose sample data is as :-

personal_number 000029
family_uid 0000290001

But when the excel sheet is downloaded these number gets changed.

personal_number 29
family_uid 290001

Solution

  • Try using xlwt, it worked for me, i also faced the same problem. By using this u need not to have any html template, python itself will convert ur integer into string and write the resulting into excel sheet. Follow this link. http://djangotricks.blogspot.in/2013/12/how-to-export-data-as-excel.html