How to export data in xls format using python django
import xlwt
response = HttpResponse(content_type='application/ms-excel')
row_num = 0
font_style = xlwt.XFStyle()
for col_num in xrange(len(columns)):
font_style = xlwt.XFStyle()
for obj in todo_obj:
wb.save(response)
import datetime , time
file_name = 'Expense-duplicate-'+str(int(round(time.time() * 1000)))+'.xls'
Duplicate = AvOutExpDashDuplicate.objects.values('e_employee_number','report_owner_id').order_by()[:5]
todo_obj=Duplicate
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename=%s' %(file_name)
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet("Todo")
row_num = 0
columns = [ (u"Employee Number", 4000), (u"Report ID", 8000), ]
font_style = xlwt.XFStyle()
font_style.font.bold = True
for col_num in xrange(len(columns)):
ws.write(row_num, col_num, columns[col_num][0], font_style)
# set column width
ws.col(col_num).width = columns[col_num][1]
font_style = xlwt.XFStyle()
font_style.alignment.wrap = 1
for obj in todo_obj:
row_num += 1
row = [
obj['e_employee_number'],
obj['report_owner_id'],
]
for col_num in xrange(len(row)):
ws.write(row_num, col_num, row[col_num], font_style)
wb.save(response)
return response