def import_set(dset, in_stream, headers=True): """Returns databook from XLS stream.""" dset.wipe() xls_book = xlrd.open_workbook(file_contents=in_stream) sheet = xls_book.sheet_by_index(0) dset.title = sheet.name for i in xrange(sheet.nrows): if (i == 0) and (headers): dset.headers = sheet.row_values(0) else: dset.append(sheet.row_values(i))
def import_book(dbook, in_stream, headers=True): """Returns databook from XLS stream.""" dbook.wipe() xls_book = xlrd.open_workbook(file_contents=in_stream) for sheet in xls_book.sheets(): data = tablib.Dataset() data.title = sheet.name for i in xrange(sheet.nrows): if (i == 0) and (headers): data.headers = sheet.row_values(0) else: data.append(sheet.row_values(i)) dbook.add_sheet(data)
def export_xls(modeladmin, request, queryset): import xlwt response = HttpResponse(content_type='application/ms-excel') response['Content-Disposition'] = 'attachment; filename=participants.xls' wb = xlwt.Workbook(encoding='utf-8') ws = wb.add_sheet("Participants") row_num = 0 columns = [ (u"ID", 2000), (u"Name", 6000), (u"Email", 8000), (u"Phone Number", 8000), (u"College", 8000), (u"Stream", 8000), (u"Subregion", 8000), (u"Zone", 8000), (u"dob", 8000), (u"gender", 8000), (u"fee_status", 8000), (u"amount", 8000), (u"responsible_person", 8000), (u"responsible_person_contact", 8000), # (u"Dob", 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 queryset: row_num += 1 row = [ obj.pk, obj.name, obj.email, obj.phoneNumber, obj.college, obj.stream, obj.subregion, obj.zone, obj.dob, obj.gender, obj.fee_status, obj.amount, obj.responsible_person, obj.responsible_person_contact, ] for col_num in xrange(len(row)): ws.write(row_num, col_num, row[col_num], font_style) wb.save(response) return response